All pastes #959307 Raw Edit

Unnamed

public python v1 · immutable
#959307 ·published 2008-03-27 10:25 UTC
rendered paste body
#!/usr/bin/pythondef getWeight(list):   sum = 0   for ball in list:      sum += ball   return sumdef balance(listA, listB):   # weight startA -> endA (inclusive, indexes) startB to endB   weightA = getWeight(listA)   weightB = getWeight(listB)   #print "Weight of ",listA,"is",weightA,"and ",listB,"is", weightB   if weightA == weightB:      return 0   elif weightA > weightB:      return 1   elif weightB > weightA:      return -1   else:      print "SOMETHING IS WRONG!!!"      return -1000   # 0 = balanced   # 1 = A heavier   # -1 = B heavierdef tryBalls(balls):   # your magic algorithm here      return 18balls = []for i in range(0,12):   balls.append(2)for heavy in range(0, 2):   for i in range(0, 12):      if heavy:         balls[i] = 3      else:         balls[i] = 1      # test out the algorithm      if (tryBalls(balls) != i):         if tryBalls(balls) != 18:            print "GOT IT WRONG: returned ", tryBalls(balls), " expected", i            print "balls was",balls      else:         print "Correct for ", i, "balls ",balls      # reset the ball weight      balls[i] = 2