Created
December 30, 2013 13:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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