Skip to content

Instantly share code, notes, and snippets.

@jasonbekolay
Last active August 29, 2015 14:13
Show Gist options
  • Save jasonbekolay/3c1c433ab462bbb8779d to your computer and use it in GitHub Desktop.
Save jasonbekolay/3c1c433ab462bbb8779d to your computer and use it in GitHub Desktop.
# See https://twitter.com/octonion/status/555961809141002240 and https://twitter.com/octonion/status/555990175147642880
lightWeight = 10.0
heavyWeight = lightWeight + 1.0
def sortPairs(pair1, pair2, pair3):
heavies = []
balance1 = balance([pair1[0], pair2[0]], [pair1[1], pair3[0]])
if balance1 == 'balanced':
balance2 = balance([pair1[0]], [pair1[1]])
if balance2 == 'left':
heavies = [pair1[0], pair2[1], pair3[0]]
else:
heavies = [pair1[1], pair2[0], pair3[1]]
else:
balance2 = balance([pair2[0], pair3[0]], [pair1[0], pair1[1]])
if balance2 == 'balanced':
if balance1 == 'left':
heavies = [pair1[0], pair2[0], pair3[1]]
else:
heavies = [pair1[1], pair2[1], pair3[0]]
elif balance2 == 'left':
if balance1 == 'left':
heavies = [pair1[0], pair2[0], pair3[0]]
else:
heavies = [pair1[1], pair2[0], pair3[0]]
else:
if balance1 == 'left':
heavies = [pair1[0], pair2[1], pair3[1]]
else:
heavies = [pair1[1], pair2[1], pair3[1]]
return heavies
def balance(leftSide, rightSide):
leftWeight = sum(leftSide)
rightWeight = sum(rightSide)
print "left: " + str(leftWeight) + ", right: " + str(rightWeight)
if (leftWeight > rightWeight):
return 'left'
if (leftWeight < rightWeight):
return 'right'
return 'balanced'
for pair1 in [[lightWeight, heavyWeight], [heavyWeight, lightWeight]]:
for pair2 in [[lightWeight, heavyWeight], [heavyWeight, lightWeight]]:
for pair3 in [[lightWeight, heavyWeight], [heavyWeight, lightWeight]]:
print 'testing pair 1 ' + str(pair1) + ', pair2 ' + str(pair2) + ', pair3 ' + str(pair3)
heavies = sortPairs(pair1, pair2, pair3)
for heavy in heavies:
assert heavy == heavyWeight, 'You are wrong about a heavy. heavies: ' + str(heavies) + ', light: ' + str(lights)
print 'Success!'
#def sortPairsBad(pair1, pair2, pair3):
# leftSide = [pair1[0], pair2[0], pair3[0]]
# rightSide = [pair1[1], pair2[1], pair3[1]]
#
# balance1 = balance(leftSide, rightSide)
# assert balance1 != 'balanced', 'Scale says ' + balance1 + ', my assumption must be wrong! left: ' + str(leftSide) + ', right: ' + str(rightSide)
# heavies = leftSide[:] # copy array with slice
# lights = rightSide[:]
# if (balance1 == 'right'):
# heavies = rightSide[:]
# lights = leftSide[:]
#
# # swap one value
# swapTemp = lights[0]
# lights[0] = heavies[0]
# heavies[0] = swapTemp
#
# balance2 = balance(heavies, lights)
# assert balance2 != 'balanced', 'Scale says ' + balance2 + ', my assumption must be wrong! left: ' + str(heavies) + ', right: ' + str(lights)
# if (balance2 == 'right'):
# # balance changed, meaning we swapped the wrong one
# heavies[0] = lights[0]
# lights[0] = swapTemp
# return (heavies, lights)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment