10

I have a question about my Stack Overflow post: Functions for a search tree where each node has three children (similar to BST) - Python

I want to know if my above question has been written properly. I have written all kinds of testing and example inputs/outputs and I have separated them, however, the question then becomes really long as you can see and may put people off from answering.

Also, is it okay to write testing code and normal code the same way since it can easily be confused?

Lastly, if my question is to do with three functions within one problematic class (i.e. a problem regarding one class), then is my question focused enough?

25
  • 10
    Beware of the Meta Effect! Nov 26, 2021 at 17:17
  • 2
    Thank you for the heads up :) I would appreciate feedback if it means getting help to my posts now and in future
    – user17466489
    Nov 26, 2021 at 17:25
  • 13
    Doesn't look too long or wordy to me. Won't comment on the code, because I've developed a pathological hatred of Python I'm struggling to get over. Nov 26, 2021 at 18:09
  • 7
    There's nothing wrong with long questions, as long as they are focused. A huge description of a program with no attempt is bad. A question focusing on a specific error with many details is good. Nov 26, 2021 at 18:58
  • 20
    In my humble opinion, the question is too broad. It says "The functions produce errors to do with left/right attribute not being recognised. As well as that, the functions themselves are not working as I would hope.": this should be focusing either on a specific error (one!) or a specific behaviour that you hoped for and are not getting. It should not be about several errors and several misbehaviours. Also, the specifics of the problem should not (only) be explained as code-comments, but better be presented in the text itself.
    – trincot
    Nov 26, 2021 at 21:22
  • 1
    I've made some changes based on your feedback. On the idea of behaviour, hoped for behaviours were addressed by test code. True, the question does partly rely on someone reproducing the errors and running given code to test functions such as add, etc. and see themselves the left/right attribute error. On the matter of being specific, as you can see from my test code (and in most implementations of trees) the functions often work together (difficult to isolate as one issue).
    – user17466489
    Nov 26, 2021 at 22:40
  • 4
    An editor recently changed my question's tags so that it now contains only binary-search tree which I am slightly confused about as my aim is to implement three children per node and my understanding is that this is not typical of a BST although the implementation is similar. Would you say ternary (ternary tree) is more accurate?
    – user17466489
    Nov 26, 2021 at 22:40
  • 6
    Oh, poor @user4581301. Why do you hate Python? :)
    – user17242583
    Nov 26, 2021 at 23:10
  • 5
    I had a thorough look at your question, solving the assignment. But doing that confirmed that (sorry to say) most of your code in _searchNode, count, remove and max is wrong. You surely can focus on one thing, like for example how to make max work correctly. You don't need count, _searchNode, nor remove for that functionality. Simplify the test case to just test that feature in a way that it illustrates max is giving the wrong output. Remove any code that is not used.
    – trincot
    Nov 26, 2021 at 23:12
  • 6
    Traumatic experience inheriting an incomprehensible a collection of cut-n-pasted shit cobbled together by a cargo-cultist. That's not the fault of the language, you can do the same with any of them, but wading through thousands of lines of excrement while people scream at you for taking too long is the absolute worst way to learn a language. Pretty sure the dude left because he knew the jig was up and wanted out before he actually had to do work and wanted to get a new job before he couldn't use our firm as a reference. Nov 26, 2021 at 23:34
  • 1
    It would help to be given an idea of what is causing the functions to be wrong. For example, with count, I feel like surely there must be a way to avoid having to check all the nodes' data before deciding on a count and even if this was the case, how do you traverse a three-children-node? I have seen someone suggest two midpoints.
    – user17466489
    Nov 26, 2021 at 23:44
  • 1
    And with max, why is it not enough to look through right-hand side when finding the lexicographically largest string in the tree? With remove I see I am currently only taking an index rather than a string contained in one of the tree nodes, this would need to be implemented somehow but even so there is a link between count and remove both requiring correct traversing that at the moment my code is not able to do. This is why I was suggesting that the functions are not independent (although I understand your point about isolating/limiting testing), these functions together make up a great base.
    – user17466489
    Nov 26, 2021 at 23:44
  • 10
    Paul, all these questions can be answered, but the problem is that they are questions, with an s at the end. If you are serious about getting answers, then ask one specific question only, and only present the related code to reproduce the problem. Then when you have the answer to that, see if you can use it to solve some more problems. If you cannot solve the rest (later), ask a separate question about the next problem, each time focusing the code and test only on that aspect.
    – trincot
    Nov 27, 2021 at 8:41
  • 3
    I don't see an actual question anywhere in that Q&A. It seems to imply a request for general code review, which is usually not specific enough for SO. Nov 27, 2021 at 9:10
  • 3
    I answered your question, but it is only because it is discussed here, and to show to all that your question is not a quality question at all. Your remove function has little to nothing in it that relates to your data structure and looks like copied from a linked list context. I find it really disappointing, as even the simplest code review shows the complete disconnect between that function and your data structure. It shows no effort of basic testing, debugging as even the attribute/property names do not correspond to anything in your data structure.
    – trincot
    Nov 27, 2021 at 15:38

2 Answers 2

33

Asking a long question is okay, if perhaps not the most effective to get people to answer. However, this isn't a long question – it is long but neither one question nor even a question at all.

  • Make sure to ask an actual question.
    • Do not just imply a question by listing requirements and some evidence that suggests the requirements are not met. Concretely ask how to approach a specific problem.
    • Name the problem you want solved. Do not rely on people to compare descriptions and outputs to find the problem in the first place.
  • There should be one – and preferably only one – obvious question to answer.
    • Focus on one problem per question. If you have three methods misbehaving in separate ways, that's three separate questions.
    • It does not generally matter that the problems are "within one problematic class". All problems are within one problematic class, module, program, application, system, ... if you just look broadly enough. Unless you are sure the problems boil down to the same issue, they are separate.
  • If the content is hard to present, it's likely bad content.
    • When you have trouble to "write [different] code the same way since it can easily be confused", prefer to simplify the code than to pretty up the presentation. Remember that our desire for a Minimal, Reproducible Example is there to help you as well.
    • Focus on correctness over presentation. A simple code block with a caption is more useful than a fancy mockup of an interactive prompt.

As some actionable advice:

  • Start with a copy of your actual code or build a simplified example of it.
  • Pick the first problem you see going wrong.
  • Write test code for only this problem.
  • Remove all the example code not required to trigger the problem.

At that point, either you figured out how to solve the problem yourself or you have a proper debugging question: Ask only about that specific problem with the specific test code and minimal example code.

5
  • 22
    Emphasis on the last paragraph. I can't count how many times I have personally started to type out a question on StackOverflow and, in the course of creating a minimal example, found the problem and fixed it myself. It's good debugging advice, even in the absence of this website. Nov 27, 2021 at 20:08
  • The question has been changed with an addition to the end of the original post. If anyone thinks the question is now better, feel free to re-open.
    – user17466489
    Nov 28, 2021 at 13:43
  • 2
    @Paul It's not closed. It's deleted by you. Undeleting it is your job. But I don't think you should. The question is still very messy IMO.
    – klutt
    Nov 29, 2021 at 10:53
  • 2
    @Paul For instance, you have what it should produce, but not what it is actually producing. And it's a very long test code for the purpose of asking for help.
    – klutt
    Nov 29, 2021 at 11:04
  • 2
    @Paul To be a bit blunt, your post have the smell of "Oh no! I have written a piece of code that is to complex for me to debug. Could you please do the debugging for me?"
    – klutt
    Nov 29, 2021 at 11:08
1

I have written all kinds of testing and example inputs/outputs and I have separated them, however, the question then becomes really long as you can see and may put people off from answering.

Here's how you can reduce your code to a minimal reproducible example:

  1. Go through your testing code to find the first printed output that is incorrect compared to your expected output.
  2. Delete all of the test code after that, since it can't affect the printed output.
  3. Delete the prints which happen before that, because their outputs are correct and therefore unnecessary to reproduce the issue. If there is any other code in the test which isn't necessary to set up the failing test output, delete this too.
  4. You now have a single test which fails. Delete all of the code that isn't used by that one test. For example, if your test only needs the add method then you can delete the remove and count methods, and the test will still reproduce the issue properly.
  5. Rewrite your expected output to be just the result for that one test.
  6. Also write the actual output that you are getting from that one test, and explain in text what the difference is to your expected output (unless it's really blatant).

Yes, this is a lot of work, but as the person writing the code and asking the question, this work is your responsibility. If you don't do this work yourself, then effectively you are asking other people to do it for you; the more work it takes for someone to answer your question, the less likely anyone will bother to answer it. On the other hand, doing that work yourself will improve your debugging skills and make you more capable as a programmer, and you might even solve the problem yourself in the process.

You must log in to answer this question.