Lecture Notes on 01 August 2011 # recursive solution to Towers of Hanoi def towers (n, source, spare, dest): if (n == 1): print "Move disk from ", source, "to", dest else: towers (n - 1, source, dest, spare) print "Move disk from ", source, "to", dest towers (n - 1, spare, source, dest) # gives all subsets or combinations of a given list def combine (a, b, idxA): if (idxA == len(a)): print b return else: c = b[:] b.append (a[idxA]) idxA = idxA + 1 combine (a, b, idxA) combine (a, c, idxA) # gives all permutation of a given list def permute (a, lo, hi): if (lo == hi): print a else: for i in range (lo, hi): a[i], a[lo] = a[lo], a[i] permute (a, lo + 1, hi) a[i], a[lo] = a[lo], a[i] def main(): # test towers of hanoi n = input ("Enter number of disks: ") towers (n, 'A', 'B', 'C') # test permutation a = ['A', 'B', 'C'] permute (a, 0, len(a)) # test combination b = [] combine (a, b, 0) main()