I am currently working on the Python “binary_search” exercise. The instructions do a decent job of explaining binary trees. But when I started working on the exercise I find two functions, data and sorted_data, which are not mentioned in the instructions. In previous situations like this I have often been able to figure out how the mystery functions are supposed to work. But this time, I have been unable to figure out what these functions are supposed to return: a string, an object, etc. Working in my local environment I have code that calls print on the return values and the print output matches what is “expected” so I think my code is pretty solid. But I am at a loss trying to figure out why the same code fails all but one of the tests when run in Exercism. Please help!
How are we supposed to help you without seeing your code?
Although I could state an opinion: binary-search-tree looks to be about 80% of the way through the track. I would expect by this time you have gained some experience reading the test suite and writing your solution accordingly.
The data method expects your BinarySearchTree instance to emit its deeply-nested TreeNode structure.
The sorted_data method expects you to walk the tree in an in-order fashion and collect the data value of each node.
What I am looking for is an explanation of what the “data” and “sorted_data” are supposed to return. Since I do not what they are supposed to return and since my guesses are not working, I don’t see how looking at my code would help answer the question. I am hoping that someone who has prior knowledge of the exercise or who can look at the test data for the exercise can figure out the answer to my question. FWIW: here is the current (1-line) code for my data function: “return self.head” where self.head is the root TreeNode.
How does one “emit its deeply-nested TreeNode structure”? I am returning the root node but that is not working.
Same:
def data(self):
return self.root
The key issue is what you do in the constructor with the incoming data.
Let’s look at the 2nd test:
def test_smaller_number_at_left_node(self):
expected = TreeNode("4", TreeNode("2", None, None), None)
self.assertTreeEqual(BinarySearchTree(["4", "2"]).data(), expected)
- we construct a tree
BinarySearchTree(["4", "2"]) - this tree looks like:
The root node has value “4”. It has a “left” node whose value is “2”. The root has no “right” node.4 / 2 - the test expects the
data()method to returnTreeNode("4", TreeNode("2", None, None), None)
Thanks for persevering in your efforts to help!
As far as I can tell, my code is building the tree properly and if I run it in my local Python environment, it works well when I feed it the same data from the Exercism testing. When I call BinarySearchTree(["4", "2"]).data() locally, return self.head and print the results I get exactly “TreeNode(“4”, TreeNode(“2”, None, None), None)”. But when I run the same code within Exercism, this same test fails because it got “TreeNode(“4”, None, None)”, i.e. just the root node, as a result!?
So I am still stuck. What is the data() function supposed to return? In the example you cited, how is ‘TreeNode(“4”, TreeNode(“2”, None, None), None)’ to be returned, as a string, as the root object of the tree, other?
It should return an instance of type TreeNode()
If you share your code, we could probably spot the issue and/or replicate it ![]()
I am returning an instance of TreeNode, the root node of the Tree.
That should work, then ;)
We strongly recommend you don’t implement your own tests but (locally) use the actual Exercism tests. That way you can locally test what the website tests. See working locally for instructions on fetching the tests and testing for instructions on running the Python tests.
If you say you’re doing what the tests expect, then the tests should pass. If the tests fail, you’re not doing what they expect. Without seeing your code, we can’t tell you where your code differs from the expected behavior.
I do get the tests I run locally from Exercism.
I don’t say that I’m doing what the tests expect. I say I’m doing what I’m guessing that the tests expect. And I have to guess because nowhere does the exercise explain what the “data()” function does or what it is supposed to return.
I usually do my initial coding and testing in my own Python environment, not Exercism’s. I copy the initial code from Exercism and copy various tests from Exercism’s tests. Once I think I have it working, I copy the code back to Exercism’s web editor and run full set of tests against it.
I tried Exercism’s “working locally” but I found that its instructions were lacking and I could not figure out how to make it work. But your suggestion has motivated me to try it again.
In the meantime, I will post my code once I know how. How do you post code to this forum?
If you run the full Exercism test suite locally, you should get the same exact results locally and on the website. I have yet to see anyone get different results locally vs on the website when actually running the full test suite.
If you have trouble with it, we’re happy to help!
Copy, paste, then put ``` above and below your code (on their own line).
```
your code here
```
Success!!
After installing the CLI interface, incorporating pytest into VS Code and using it to debug the tests (to see why they were failing), I was able to find my bug!
As it turns out I was focused on the wrong code. My data() function was fine and my code to build the tree was fine. The problem was with the code to construct the “expected” TreeNode (with one or more left’s or rights’s included).
I can’t help but think that if the exercise made it clear that it was supposed to return the root TreeNode or if the tests indicated that the mismatch in results was due to an error in the “expected” value (not the data() return value) then I could have found my bug much more quickly.
Many thanks to those who helped me work this out!