this assignment is about solving problems using recursion. For each question, write a recursive function that solves the problem asked in that question. Your solutions don't have to be efficient, but they do have to use recursion in a non-trivial way. Put all your code in a file called a9.py.
tower(1) 2 >>> tower(2) 4 >>> tower(3) 16 >>> tower(4) 65536
def is_palindrome(s): if len(s) <= 1: return True else: if s[0] != s[-1]: return False else: return is_palindrome(s[1:-1]) def find_min(a): if len(a) == 1: return a[0] else: small = find_min(a[1:]) if a[0] < small: small = a[0] return small def reverse(a): if len(a) == 0: return [] else: lst = reverse(a[1:]) lst.append(a[0]) return lst def tower(x): if x == 0: return 1 else: return 2 ** (tower(x - 1)) def is_prime(n, i=2): if n <= 1: return False elif n == i: return True else: return n % i != 0 and is_prime(n, i + 1)
Get Answers For Free
Most questions answered within 1 hours.