#Rules of the game: Do not use python built-in integer arithmetic # (its ok to add a constant number of bits though) def Intify(L): # This converts a 0/1-list in a Python integer s = 0 # We use this for checking correctness of our algorithms M = 1 for j in L: if (j != 0): s = s+ M M = M*2 return s def Add(L1,L2): if (len(L1) < len(L2)): L1, L2 = L2, L1 #swaps the pointers to the two lists Out = [] carry = 0 for i in range(len(L1)): if (i < len(L2)): h = carry + L1[i] + L2[i] else: h = carry + L1[i] Out.append(h%2) if h > 1: carry = 1 else: carry = 0 if carry == 1: Out.append(1) return Out def Leading1(L): s = -1 for i in range(len(L)): if L[i] != 0: s = i return s def Multiply(L1,L2): #condition L2 does not represent 0 if Leading1(L2) == -1: return [0] else: H =list(L2) b = H.pop(0) Z = Multiply(L1,H) Z.insert(0,0) if b == 0: return Z else: return Add(Z,L1) L1 = [1,0,1] print L1 , Intify(L1), "\n" L2 = [1, 1,1,1,0,1] print L2 , Intify(L2), "\n" L3 = Add(L1,L2) print L3 , Intify(L3), "\n" print "L1 * L2", Intify(Multiply(L1,L2)), Multiply(L1,L2)