• Home
  • Textbooks
  • The Practice of Computing Using Python
  • Recursion: Another Control Mechanism

The Practice of Computing Using Python

William F. Punch, Richard Enbody

Chapter 17

Recursion: Another Control Mechanism - all with Video Answers

Educators


Chapter Questions

Problem 1

A common task in an editor application, such as WordPad or TextEdit, is parentheses matching. For example, " $(00)$ " is a valid string of parentheses, because each opening bracket has a corresponding closing bracket. On the other hand, " $(0$ " is not a valid string. Another invalid example would be " $)(, "$ as the opening bracket should precede the closing bracket.
Write a recursive function that takes a string as input and returns True if the string is valid with respect to matched parentheses. To simplify matters, begin by considering that the input string has only " " or ")" and no other characters.

Check back soon!

Problem 2

An XML file has entries of the rype:
eabcs
<xyz> Mother $\langle/ x y z\rangle$
<pqr> Pather </pqr>
<1mn> Brothera </Imn>
<def> Sisters </def>
$</ a b c>$
In this example, $\langle a b c>$ is a start tag and $\langle/ a b c>$ is the corresponding end tag, and so on for the others. Your task is to write a recursive function to check whether each start tag has a corresponding end tag in the correct order. Return True if the XML. file is valid; otherwise, return False.
The order is important here - the end tag for any tag cannot occur before its corresponding start tag.

Check back soon!
00:31

Problem 3

When developing the Rational class, we created a function to calculate the greatest common denominator: ged. Refactor the function to be recursive.

Ernest Castorena
Ernest Castorena
Numerade Educator

Problem 4

A recursive function to calculate the Fibonacci numbers was presented in this chapter. However, each time a number was calculated, it recalculated previous Fibonacci numbers. Refactor your recursive function to retain previous, already calculated Fibonacci numbers and reference them instead of recalculating them. A list could be used, but a dictionary works better. The name for this technique is "memoization."

Check back soon!
01:49

Problem 5

A formula exists for calcularing the amount of money in a savings account that begins with an initial value (the initial principal, $P$ ) and earns interest with an annual interest rate $i$, for $n$ years: $P(1+i)^n$.
Write a recursive function that calculates that same value, and check your result against the formula.

Anurag Kumar
Anurag Kumar
Numerade Educator

Problem 6

Converting decimal numbers to binary numbers can be done recursively. Write a function that takes a positive int argument and returns the corresponding binary number as an int composed of only 1 and 0 s. For example, convert (5) returns the int: 101 .
The insight for this problem comes from the fact that the rightmost digit of a decimal $n$ is easy to calculate. It is the remainder when dividing by the base $2: n \% 2$. To get the next rightmost digit, you take the resulting quotient, i.e., $n_2=n / 2$, and find its remainder, $n_2 \% 2$, which is the next digit. Unfortunately, that generates the digits from right to left, and we want them from left to right. We could easily do that nonrecursively using string concatenation or string reversal, but in this exercise you are to use recursion to recursively calculate the digits. Effectively, you are letting recursion reverse the ordering: think in terms of calculating the rightmost digit as described previously, but then let recursion reverse the digits.

Check back soon!
02:17

Problem 7

The United States still uses the traditional English system of measurement. Each inch on a ruler is marked off as fractions, using tick marks that look like this:
(FIGURE CAN'T COPY)
The tallest tick mark is the one-half-inch mark, the next two tallest ticks are the quarter-inch marks and even shorter ones are used to mark the eighths and sixteenths, and so on. Write the following recursive function:
drawRuler( $x, y$, width, height)
where $(x, y)$ represents the lower-left corner of a rectangle that has dimensions width $x$ height. The ruler will be drawn within that rectangle. The function draws a line along the rectangle's bottom edge and then recursively draws vertical tick marks. The rectangle itself is not drawn. The middle-most tick mark is centered in the rectangle and is as tall as the rectangle height. Each smaller tick mark is half the height of the next larger one. Once the tick marks become sufficiently small, the recursion terminates. The recursive insight to this problem is to recognize that the middle-most tick mark subdivides the rectangle into two smaller rectangles, each of which is a smaller ruler of its own. Use Turtle Graphics to draw the figure.

Jay Patel
Jay Patel
Numerade Educator