Skip to content

Instantly share code, notes, and snippets.

@bfintal
Created October 7, 2011 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfintal/1271292 to your computer and use it in GitHub Desktop.
Save bfintal/1271292 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from random import randint
from sys import exit
# list of coins containing their weight (2 grams)
coins = [2]*7
# insert a different sized coin (1 or 3 grams randomized)
newcoin = randint(0,1)*2+1
coins.insert(randint(0,7), newcoin)
print 'coins:', coins
# our weighing scale
def weight(list1, list2):
if (type(list1) == type(1)):
ret = list1 == list2
if ret == True:
return ret
if list1 < list2:
return 'less'
return 'more'
ret = sum(list1) == sum(list2)
if ret == True:
return ret
if sum(list1) < sum(list2):
return 'less'
return 'more'
weight1 = weight(coins[2:5], coins[5:8])
if weight1 == True:
weight2 = weight(coins[0], coins[1])
weight3 = weight(coins[0], coins[2])
if weight3 == True and weight2 == 'less':
print 'coin 2 is heavier'
elif weight3 == 'less' and weight2 == 'less':
print 'coin 1 is lighter'
elif weight3 == True:
print 'coin 2 is lighter'
else:
print 'coin 1 is heavier'
exit()
weight2 = weight(coins[4:6], coins[6:8])
if weight2 == True:
weight3 = weight(coins[3], coins[4])
if weight3 == True and weight1 == 'less':
print 'coin 3 is lighter'
elif weight3 == True and weight1 == 'more':
print 'coin 3 is heavier'
elif weight3 == 'less':
print 'coin 4 is lighter'
else:
print 'coin 4 is heavier'
exit()
if weight1 == 'more' and weight2 == 'less':
print 'coin 6 is lighter'
exit()
elif weight1 == 'less' and weight2 == 'more':
print 'coin 6 is heavier'
exit()
weight3 = weight(coins[6], coins[7])
if weight3 == True:
if weight2 == 'less':
print 'coin 5 is lighter'
else:
print 'coin 5 is heavier'
exit()
if weight1 == weight2 and weight2 == weight3:
if weight1 == 'less':
print 'coin 8 is heavier'
else:
print 'coin 8 is lighter'
exit()
if weight3 == 'less':
print 'coin 7 is lighter'
else:
print 'coin 7 is heavier'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment