-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecursion_amok.py
More file actions
29 lines (23 loc) · 846 Bytes
/
recursion_amok.py
File metadata and controls
29 lines (23 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""Although recursion is a very powerful tool, it can easily be misused in various ways.
In this section, we examine several problems in which
a poorly implemented recursion causes drastic inefficiency.
"""
def unique3(S, start, stop):
"""Return ``True`` if there are no duplicate elements in slice ``S[start:Stop].``"""
if stop - start <= 1:
return True # At most one item.
elif not unique3(S, start, stop - 1):
return False # First part has duplicate.
def bad_fibonacci(n):
"""Return the nth Fibonacci number."""
if n <= 1:
return n
else:
return bad_fibonacci(n - 2) + bad_fibonacci(n - 1)
def good_fibonacci(n):
"""Return pair of Fibonacci numbers, ``F(n), F(n-1)``."""
if n <= 1:
return n, 0
else:
a, b = good_fibonacci(n-1)
return a + b, a