Skip to content

Instantly share code, notes, and snippets.

@suhailpatel
Created December 30, 2013 13:20
Show Gist options
  • Save suhailpatel/8182031 to your computer and use it in GitHub Desktop.
Save suhailpatel/8182031 to your computer and use it in GitHub Desktop.
The Guardian Science Twitter Account posted an interesting problem and I had 10 minutes or so spare to tackle it using Python. The problem is: >> New year conundrum: complete the equation 10 9 8 7 6 5 4 3 2 1 0 = 2014 @AlexBellos http://t.co/EMedtSq9WT
# The Guardian Science Twitter Account posted an interesting problem and
# I had 10 minutes or so spare to tackle it using Python. The problem is:
#
# New year conundrum: complete the equation 10 9 8 7 6 5 4 3 2 1 0 = 2014
# @AlexBellos http://t.co/EMedtSq9WT
#
# Not a very elegant solution but it does the job just fine
numbers = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
target = 2014
first = numbers.pop(0)
combinations = [(str(first), first)]
while numbers:
front = numbers.pop(0)
array = []
for val in combinations:
array.append(("%s + %d" % (val[0], front), val[1] + front))
array.append(("%s - %d" % (val[0], front), val[1] - front))
array.append(("%s * %d" % (val[0], front), val[1] * front))
if front != 0:
array.append(("%s / %d" % (val[0], front), val[1] / front))
combinations = array
# Find the combinations which add up to our target
for val in combinations:
if val[1] == target:
print "%s = %d" % (val[0], target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment