Lecture Notes on 18 Nov 2009 # Gets the combinations of size 'size' def combine (a, b, idx, size): if (idx == len(a)): if (len(b) == size): print b return if (len(b) == size): print b else: c = b[:] b.append(a[idx]) idx = idx + 1 combine (a, c, idx, size) combine (a, b, idx, size) def main (): a = [1, 2, 3, 4, 5] b = [] combine (a, b, 0, 3) main() # Gets all combinations including the trivial combination with no elements def combine (a, b, idx): if (idx == len(a)): print b return else: c = b[:] b.append(a[idx]) idx = idx + 1 combine (a, c, idx) combine (a, b, idx) def main (): a = [1, 2, 3, 4, 5] b = [] combine (a, b, 0) main() # Solution to Problem 5 in the Class Work for 18 Nov 2009 def combine (a, b, idx): if (idx == len(a)): sum = 0 for num in b: sum = sum + num if (sum == 50): print b return else: c = b[:] b.append(a[idx]) idx = idx + 1 combine (a, b, idx) combine (a, c, idx) def main(): a = [15, 9, 30, 21, 19, 3, 12, 6, 25, 27] b = [] combine (a, b, 0) main()