Lab 2: Base Conversion
Students build a block to display a number in a specified base. The work begins in base 2 and extends to any base, and also involves converting back to base 10. While this problem can be solved using iteration, the recursive solution is more elegant.
Pacing
MARY: Is this up to date? --MF, 5/16/20
The 2 lab pages could be split across 1–2 days (
45–90 minutes). Expected times to complete follow:
Lab Pages
-
Page 1: Binary Conversion.
-
Learning Goals:
- Practice recursive functions.
- Learn a more elegant approach to base conversion.
-
Tips:
- Some students may get confused by the word base here, thinking it connects to base cases. These are two different uses of the same word.
- Give students a chance to try the binary conversion problem directly, but chances are they will need the hints from the second page. Solutions that students construct without the hints are often iterative instead of recursive. Some students also find the general base conversion problem (or a specific one like base 7) to be easier than binary conversion, so if a student continues to be stuck, switch them to a different base.
- Some solutions will involve lists; students may not know about the
list->word
block (added by using Import tools) that can solve this problem; it is also possible to write a recursive version using join
. The provided solution uses join
as part of the recursive step, so it is done as part of the process.
- Watch for errors in base cases when students move beyond binary conversion, since more values should be part of the base case. This is a common mistake.
-
Page 2: Base Conversion.
- Learning Goal: Generalize from binary to base conversion in general.
-
Discussion: How is recursion structured in Base Conversion?
- What are the base cases?
- What potential problems could happen if the base cases are not well specified?
- What parts of the algorithm and base cases change when extending beyond binary conversion?
- How does the
from base
conversion algorithm compare to the base
conversion algorithm?
-
Tips:
- Students may need to search to find the
block, which is very helpful for the from base
block.
- You can use a counter variable to keep track of the number of calls if you are interested in talking about how the Base Conversion algorithm is linear time.
Solutions