Lecture Notes on 22 Feb 2023 # gets all subsets of a list a def sub_sets (a, b, idx): hi = len(a) if (idx == hi): print (b) return else: c = b[:] b.append(a[idx]) sub_sets (a, b, idx + 1) sub_sets (a, c, idx + 1) def main(): a = ['A', 'B', 'C', 'D', 'E'] b = [] sub_sets (a, b, 0) main() # gets all permutations of a list a def permute (a, idx): hi = len(a) if (idx == hi): print (a) else: for i in range (idx, hi): a[idx], a[i] = a[i], a[idx] permute (a, idx + 1) a[idx], a[i] = a[i], a[idx] def main(): a = ['A', 'B', 'C', 'D'] permute (a, 0) main() # use memoization to obtain the fibonacci sequence def fib_memo (n, memo): if (n == 0) or (n == 1): return n else: if (n >= len(memo)): f = fib_memo (n - 1, memo) + fib_memo( n - 2, memo) memo.append (f) return f else: return memo[n] # use recursion to obtain the fibonacci sequence def fib_rec (n): if (n == 0) or (n == 1): return n else: return fib_rec (n - 1) + fib_rec (n - 2) def main(): ''' for i in range (50): print (i, ' ', fib_rec(i)) ''' memo = [0, 1] for i in range (500): print (i, ' ', fib_memo (i, memo)) main()