Recursion
Trace recursive calls, then write multiply, merge, and isSorted recursively, practicing base cases and the leap of faith.
- Lessons
- Recursion
This discussion goes with Lesson 7. For every recursive function, say out loud: what is the base case, what smaller problem does the recursive call solve, and how does its answer become this call’s answer?
Q1: What would Swift print?
What would Swift print?
func trace(_ n: Int) -> Int {
print("in", n)
if n == 0 {
return 0
}
let result = n + trace(n - 1)
print("out", n)
return result
}
print(trace(2))Show answer and explanation
Answerin 2\nin 1\nin 0\nout 1\nout 2\n3
Each call prints in on the way down. The out lines come on the way back
up, innermost first, once each recursive call has returned. The result is
2 + 1 + 0.
What would Swift print?
func f(_ n: Int) -> Int {
n < 10 ? n : f(n / 10) + f(n % 10)
}
print(f(2026))Show answer and explanation
Answer10
f splits a number into everything but the last digit, and the last digit.
Each part is handled the same way until only single digits remain, so it
adds up the digits: 2 + 0 + 2 + 6.
Q2: Multiply without multiplying
Write multiply(_:_:) for non-negative integers using recursion and only
addition and subtraction.
func multiply(_ m: Int, _ n: Int) -> Int {
// your code here
}
Show solution
func multiply(_ m: Int, _ n: Int) -> Int {
n == 0 ? 0 : m + multiply(m, n - 1)
}
assert(multiply(5, 3) == 15)
assert(multiply(0, 4) == 0)
assert(multiply(7, 0) == 0)The leap of faith: if multiply(m, n - 1) is m × (n − 1), adding one more m
gives m × n. The base case is m × 0 = 0.
Q3: Merge
Two numbers each have their digits in decreasing order from left to right.
Write merge(_:_:), which returns a single number containing all the digits
of both, still in decreasing order. merge(31, 42) is 4321.
func merge(_ a: Int, _ b: Int) -> Int {
// your code here
}
Show solution
func merge(_ a: Int, _ b: Int) -> Int {
if a == 0 {
return b
}
if b == 0 {
return a
}
if a % 10 < b % 10 {
return merge(a / 10, b) * 10 + a % 10
}
return merge(a, b / 10) * 10 + b % 10
}
assert(merge(31, 42) == 4321)
assert(merge(21, 0) == 21)
assert(merge(21, 31) == 3211)The smallest digit overall must go last, and it is one of the two last digits. Remove it from its number, merge what is left, and put it back on the end.
Q4: Sorted digits
Write isSorted(_:), which returns true if the digits of a non-negative
number never decrease from left to right. Use recursion, no loops.
func isSorted(_ n: Int) -> Bool {
// your code here
}
Show solution
func isSorted(_ n: Int) -> Bool {
if n < 10 {
return true
}
let last = n % 10
let secondToLast = n / 10 % 10
return secondToLast <= last && isSorted(n / 10)
}
assert(isSorted(1123))
assert(!isSorted(132))
assert(isSorted(7))&& short-circuits, so the recursion stops as soon as one pair is out of
order.
Q5: Which base case?
This function is meant to count the digits of a non-negative integer.
Explain why digits(0) returns the wrong answer, and fix it with the
smallest change.
func digits(_ n: Int) -> Int {
if n == 0 {
return 0
}
return 1 + digits(n / 10)
}
print(digits(0), digits(2026))
Show solution
It prints 0 4, but 0 has one digit. The base case n == 0 is really “no
digits left”, which is right when reached from a larger number but wrong as
a starting input. Stopping at single digits instead fixes it:
func digits(_ n: Int) -> Int {
if n < 10 {
return 1
}
return 1 + digits(n / 10)
}
assert(digits(0) == 1)
assert(digits(9) == 1)
assert(digits(2026) == 4)