donnemartin / interactive-coding-challenges

120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.
Other
29.44k stars 4.45k forks source link

Added __eq__ method to ensure invert_tree_challenge tests pass. #179

Closed gmazzola closed 5 months ago

gmazzola commented 7 years ago

The invert_tree_challenge Jupyter notebook has a nose unit-test that looks like this:

assert_equal(result, root)

Where result and root are Node objects. I wrote a solution to this challenge that involved creating a new Node object versus re-using the existing one, but this failed because there was no __eq__ method defined in the Node class to enable comparison with other Nodes.

This minor pull request addresses three things:

  1. Changes the __repr__ method to aid debugging.
  2. Adds an __eq__ method to compare nodes in a tree.
  3. Adds a newline at the end of the file.

Thanks!

donnemartin commented 7 years ago

Hi @gmazzola thanks for the PR! I hope to have this reviewed within the next few days.