Satellite
From Exercism PythonMITOriginal pagePython1 class period1 exercise9 graded tests
Satellite
Instructions
Imagine you need to transmit a binary tree to a satellite approaching Alpha Centauri and you have limited bandwidth. Since the tree has no repeating items it can be uniquely represented by its pre-order and in-order traversals.
Write the software for the satellite to rebuild the tree from the traversals.
A pre-order traversal reads the value of the current node before (hence "pre") reading the left subtree in pre-order. Afterwards the right subtree is read in pre-order.
An in-order traversal reads the left subtree in-order then the current node and finally the right subtree in-order. So in order from left to right.
For example the pre-order traversal of this tree is [a, i, x, f, r]. The in-order traversal of this tree is [i, a, f, x, r]
a
/ \
i x
/ \
f r
Note: the first item in the pre-order traversal is always the root.
Exception messages
Sometimes it is necessary to raise an exception. When you do this, you should always include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the built in error types, but should still include a meaningful message.
This particular exercise requires that you use the raise statement to "throw" a ValueError if the preorder and inorder arguments are mis-matched length-wise, element-wise, or the elements are not unique. The tests will only pass if you both raise the exception and include a message with it.
To raise a ValueError with a message, write the message as an argument to the exception type:
## if preorder and inorder are not the same length
raise ValueError("traversals must have the same length")
## if preorder and inorder do not share the same elements
raise ValueError("traversals must have the same elements")
## if element repeat (are not unique)
raise ValueError("traversals must contain unique items")
Open the
01folder and editmain.py. Run Tests checks it.
For teachers
Satellite
Students solve Exercism's “Satellite” Python exercise in CS Room, with Exercism's own tests.
- Time: 1 class period
- Tests: 9, set up for you when you import the lesson
- Practices: other-comprehensions