|
| 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) |
0 commit comments