#parallel assignment def swapDumb(a, i, j): """Swap the items at indices i and j in the given list.""" temp=a[i] #this is what parallel assignment avoids a[i]=a[j] a[j]=temp def swap(a, i, j): """Swap the items at indices i and j in the given list.""" a[i], a[j]=a[j], a[i] #tuple unpacking t=((1, 2), 3) ((a, b), c)=t #really cool--don't abuse def mult(n, m): """Mutltiply two numbers by repeated addition.""" assert m>=0 #Why is this necessary? if m==0: #any number times 0 equals 0 return 0 return mult(n, (m-1))+n #==n*m, trust me guys, I'm a math minor def powerSet(a): if not a: #same as if a==[] return [[]] #[] is the only sublist of [], so our results are [[]] head=a[0] tail=a[1:] subPowerSet=powerSet(tail) result=[] for subSet in subPowerSet: result.append(subSet) result.append([head]+subSet) return result def knightsTour(n): visited=[] #make a 2D list keeping track of where the horsie has been-- #initially nowhere (all False) for i in xrange(n): visited.append([False]*n) #moves a knight can make moves=[(2,1), (2,-1), (-2, 1), (-2, -1), (1, 2), (-1, 2), (1, -2), (-1, -2)] return knightsTourRec(0, 0, visited, 0, [], moves) def isOnBoard(x, y, n): return 0<=x