Skip to content

Commit 18aff1d

Browse files
committed
Solutions
1 parent c5a7dc3 commit 18aff1d

38 files changed

+1418
-0
lines changed

CH15/Checkpoints

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#15.1
2+
A recursive function is one that invokes itself.
3+
4+
#15.2
5+
6 times.
6+
7+
#15.3
8+
f(n) = 2 if n = 1
9+
f(n) = 2 * f(n-1) if (n > 1)
10+
11+
#15.4
12+
f(x, n) = x if n = 1
13+
f(x, n) = x * f(n-1) if (n > 1)
14+
15+
#15.5
16+
f(n) = 1 if n=1
17+
f(n) = n + f(n-1) if n>1
18+
19+
#15.6
20+
Infinite recursion: if recursion does not reduce the problem in a manner that
21+
allows it to eventually converge into the base case
22+
Direct recursion: a recursion where the recursive function only invokes itself.
23+
Indirect recursion: a recursion where tow or more functions invoke each other in a recursive manner.
24+
This occurs when function A invokes function B, which in turn invokes function A.
25+
26+
#15.7
27+
25 times:
28+
number of time fib is invoked in fib(6) =
29+
1+ number of time fib is invoked in fib(4)+number of time fib is invoked in fib(5) = 1+9+15=25
30+
31+
#15.8
32+
(a)
33+
Output: 15 (5 + 4 + 3 + 2 + 1 = 15)
34+
Base case: if (n == 1)
35+
Recursive call: f(n – 1)
36+
(b)
37+
Output: 7654321
38+
Base case: if (n > 0)
39+
Recursive call: f(n // 10)
40+
41+
#15.9
42+
1) The function is implemented using an if-else or a switch statement that leads to
43+
different cases.
44+
2) One or more base cases (the simplest case) are used to stop recursion.
45+
3) Every recursive call reduces the original problem, bringing it increasingly closer to a
46+
base case until it becomes that case.
47+
48+
#15.10
49+
SKIPPED.
50+
51+
#15.11
52+
(a) 5 4 3 2 1
53+
(b) 1 2 3 4 5
54+
55+
#15.12
56+
n/10 should be n//10
57+
58+
#15.13
59+
A second function that receives additional parameters used with the original recursive function.
60+
61+
#15.14
62+
SKIPPED.
63+
64+
#15.15
65+
■ os.path.isfile(s), which returns True if s is a filename. Recall that this function
66+
was introduced in §13.2.3 to check if a file exists.
67+
■ os.path.getsize(filename), which returns the size of the file.
68+
■ os.listdir(directory), which returns a list of the subdirectories and files
69+
under the directory.
70+
71+
#15.16
72+
31 times.
73+
74+
#15.17
75+
Any recursive functions can be converted into a non-recursive function. (TRUE)
76+
Recursive function usually takes more time and memory to execute than non-recursive functions. (TRUE)
77+
Recursive functions are always simpler than non-recursive functions. (FALSE)
78+
There is always a condition statement in a recursive function to check whether a base case is reached. (TRUE)
79+
80+
#15.18
81+
When a function is invoked, its contents are placed into a stack. If a function is recursively invoked,
82+
it is possible that the stack space is exhausted. This causes stack overflow.
83+
84+
#15.19
85+
A recursive function is said to be tail recursive if there are no pending operations to be performed
86+
on return from a recursive call.
87+
88+
#15.20
89+
Tail recursion may be desirable: Because the function ends when the last recursive call
90+
ends, there is no need to store the intermediate calls in the stack.
91+
92+
#15.21
93+
Yes.
94+
95+
#15.22
96+
# Return the Fibonacci number for the specified index
97+
def fib(index):
98+
return fib1(index, 1, 0)
99+
100+
# Auxiliary tail-recursive function for fib
101+
def fib1(index, next, result):
102+
if (index == 0):
103+
return result
104+
else:
105+
return fib1(index - 1, next + result, next)

CH15/EX15.01.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 15.1 (Sum the digits in an integer using recursion) Write a recursive function that computes
2+
# the sum of the digits in an integer. Use the following function header:
3+
# def sumDigits(n):
4+
# For example, sumDigits(234) returns Write a test program
5+
# that prompts the user to enter an integer and displays its sum.
6+
7+
8+
def sumDigits(n):
9+
if n == 0:
10+
return n
11+
12+
return (n % 10 + sumDigits(n//10))
13+
14+
15+
n = eval(input("Enter a digit: "))
16+
17+
print("The sum of the digits is:", sumDigits(n))

CH15/EX15.02.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 15.2 (Fibonacci numbers) Rewrite the fib function in Listing 15.2 using iterations.
2+
# (Hint: To compute fib(n) without recursion, you need to obtain fib(n - 2)
3+
# and fib(n - 1) first.) Let f0 and f1 denote the two previous Fibonacci numbers.
4+
# The current Fibonacci number would then be f0 + f1. The algorithm can be
5+
# described as follows:
6+
# f0 = 0 # For fibs(0)
7+
# f1 = 1 # For fib(1)
8+
# for i in range(2, n + 1):
9+
# currentFib = f0 + f1
10+
# f0 = f1
11+
# f1 = currentFib
12+
# # After the loop, currentFib is fib(n)
13+
# Write a test program that prompts the user to enter an index and displays its
14+
# Fibonacci number.
15+
16+
n = eval(input("Enter a number: "))
17+
18+
f0 = 0
19+
f1 = 1
20+
fib = 0
21+
for i in range(2, n + 1):
22+
fib = f0 + f1
23+
f0 = f1
24+
f1 = fib
25+
26+
print("The Fibonacci of", n, "is", fib)

CH15/EX15.03.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 15.3 (Compute greatest common divisor using recursion) The gcd(m, n) can also be
2+
# defined recursively as follows:
3+
# ■ If m % n is 0, gcd(m, n) is n.
4+
# ■ Otherwise, gcd(m, n) is gcd(n, m % n).
5+
# Write a recursive function to find the GCD. Write a test program that prompts the
6+
# user to enter two integers and displays their GCD.
7+
8+
def gcd(m, n):
9+
if m % n == 0:
10+
return n
11+
return gcd(n, m % n)
12+
13+
14+
m, n = eval(input("Enter two numbers: "))
15+
16+
print("The GCD of", m, "and", n, "is", gcd(m, n))

CH15/EX15.04.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 15.4 (Sum series) Write a recursive function to compute the following series:
2+
# m(i) = 1 + 1/2 + 1/3 + ... + 1/i
3+
# Write a test program that displays m(i) for i 1, 2, ..., 10.
4+
5+
def sumSeries(i):
6+
if i == 1:
7+
return 1
8+
return 1 / i + sumSeries(i-1)
9+
10+
11+
for i in range(1, 11):
12+
print("The sum series for i=", i, "is", sumSeries(i))

CH15/EX15.05.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 15.5 (Sum series) Write a recursive function to compute the following series:
2+
# m(i) = 1/3 + 2/5 + 3/7 + 4/9 + 5/11 + 6/13 + ... + i/2i + 1
3+
# Write a test program that displays m(i) for i 1, 2, ..., 10.
4+
5+
def sumSeries(i):
6+
if i == 1:
7+
return 1/3
8+
return i / (2 * i + 1) + sumSeries(i - 1)
9+
10+
for i in range(1, 11):
11+
print("The sum series for i=", i, "is", sumSeries(i))

CH15/EX15.06.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 15.6 (Summing series) Write a recursive function to compute the following series:
2+
# m(i) = 1/2 + 2/3 + ... + i/i + 1
3+
# Write a test program that prompts the user to enter an integer for i and displays m(i).
4+
5+
def sumSeries(i):
6+
if i == 1:
7+
return 1 / 2
8+
return i / (i + 1) + sumSeries(i - 1)
9+
10+
11+
for i in range(1, 11):
12+
print("The sum series for i=", i, "is", sumSeries(i))

CH15/EX15.07.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 15.7 (Fibonacci series) Modify Listing 15.2 so that the program finds the number of
2+
# times the fib function is called. (Hint: Use a global variable and increment it
3+
# every time the function is called.)
4+
5+
counter = 0
6+
7+
8+
def fib(index):
9+
global counter
10+
counter += 1
11+
if index == 0: # Base case
12+
return 0
13+
elif index == 1: # Base case
14+
return 1
15+
else: # Reduction and recursive calls
16+
return fib(index - 1) + fib(index - 2)
17+
18+
19+
index = eval(input("Enter an index for a Fibonacci number: "))
20+
fib(index)
21+
print("The Fibonacci function is called", counter, "times")

CH15/EX15.08.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 15.8 (Print the digits in an integer reversely) Write a recursive function that displays
2+
# an integer value reversely on the console using the following header:
3+
# def reverseDisplay(value):
4+
# For example, invoking reverseDisplay(12345) displays 54321. Write a test
5+
# program that prompts the user to enter an integer and displays its reversal.
6+
7+
def reverseDisplay(value):
8+
if value == 0:
9+
print()
10+
else:
11+
print(value % 10, end='')
12+
reverseDisplay(value // 10)
13+
14+
val = eval(input("Enter an integer: "))
15+
reverseDisplay(val)

CH15/EX15.09.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 15.9 (Print the characters in a string reversely) Write a recursive function that displays
2+
# a string reversely on the console using the following header:
3+
# def reverseDisplay(value):
4+
# For example, reverseDisplay("abcd") displays dcba. Write a test program
5+
# that prompts the user to enter a string and displays its reversal.
6+
7+
def reverseDisplay(value):
8+
if value == '':
9+
print()
10+
else:
11+
print(value[-1], end='')
12+
reverseDisplay(value[:len(value) - 1])
13+
14+
15+
val = input("Enter a string: ")
16+
reverseDisplay(val)

0 commit comments

Comments
 (0)