11.7 Advanced exercises
The following more advanced exercises cover some topics introduced as Advanced aspects (in Section 11.4).
11.7.2 Exercise A2
A rose is a rose…
Most illustrations of recursive functions address mathematical phenomena. As a non-numeric example, consider the literary phrase
“A rose is a rose is a rose is a rose.”
that is based on a poem by Gertrude Stein and became a popular meme (see flowery
of the ds4psy package for 60 variants of the phrase).
The appeal of the phrase is largely due to its recursive aspect — so we should be able to use recursion to program a generator function for it.
Analysis:
- What part of the phrase is recursive? Which one is not?
- What is the stopping condition? What should happen when it is reached?
- What part of the phrase is recursive? Which one is not?
Write a recursive function
get_phrase(x = "rose", n = 3)
that generates the phrase by repeating the partpaste0("is a", x)
n
times.- Note: It’s easy to write a function using repetition, but try writing a truly recursive function.
- Hint: Consider using an auxiliary function that only handles the recursive part of the phrase.
- Note: It’s easy to write a function using repetition, but try writing a truly recursive function.
11.7.3 Exercise A3
Solving the Towers of Hanoi (ToH)
The video clip Recursion ‘Super Power’ (in Python) by Computerphile explains how to recursively solve the Towers of Hanoi problem in Python:
- Watch the video and then solve the problem for \(n\) discs (e.g., by writing a function
ToH(n = 4)
) in R.
Use your
ToH()
function for solving then = 3
case and answer the following questions:How many disc moves does this require?
How often does the function meet the stopping criterion (i.e.,
n == 0
)?
Hint: To determine this, you could print out “ok” (or some counter value) every time you reach this condition.
- The solution shown in the video provides a recipe for solving the problem, but does not explicate which disc is being transferred on each move.
Slightly adjust themove()
helper function and its call in theToH()
function so that the disc being transferred is identified by its sizen
(withn = 1
denoting the smallest disc, andn = n
denoting the largest disk).
11.7.4 Exercise A4
More sorting and measuring
- Watch the following video clip and implement at least one sorting algorithm not yet illustrated in this chapter as an R function.
- 15 Sorting Algorithms in 6 Minutes (by Timo Bingmann): Visualization and “audibilization” of 15 sorting algorithms in 6 minutes. Sorts random shuffles of integers, with both speed and the number of items adapted to each algorithm’s complexity.
- Compare the performance of your new function with those discussed in Section 11.4.2 on a suitable range of problems.
11.7.5 Exercise A5
Vectorizing a function
The base2dec()
function (from the ds4psy package) converts a sequence of numeral digits from some base
notation into numerals in our familiar decimal notation (i.e., base = 10
). Thus, the following numerals (1001
in base = 2
, 101
in base = 3
, etc.) all represent the same numeric value of \(10\):
library(ds4psy)
a <- base2dec(1010, base = 2)
b <- base2dec( 101, base = 3)
c <- base2dec( 22, base = 4)
d <- base2dec( 20, base = 5)
all.equal(a, b, c, d, 10)
#> [1] TRUE
- What happens when using the function with vector inputs?
- Create a vectorized version
base2dec_vec()
of thebase2dec()
function and use it to verify:
- Predict and explain the result of the following expression:
This concludes the more advanced exercises on programming in R.