Skip to content

Instantly share code, notes, and snippets.

@CyberRoute
Last active December 12, 2017 09:57
Show Gist options
  • Save CyberRoute/1772a63a6adac1ad77de2eeeaedf3f79 to your computer and use it in GitHub Desktop.
Save CyberRoute/1772a63a6adac1ad77de2eeeaedf3f79 to your computer and use it in GitHub Desktop.
countdown
import sys
import re
def find_repr(target, numbers):
org_reprs = dict((number, str(number)) for number in numbers)
curr_reprs = org_reprs
while target not in curr_reprs:
old_reprs, curr_reprs = curr_reprs, {}
for x in old_reprs:
for y in org_reprs:
repr_x, repr_y = old_reprs[x], old_reprs[y]
curr_reprs[x + y] = '(%s) + (%s)' % (repr_x, repr_y)
curr_reprs[x - y] = '(%s) - (%s)' % (repr_x, repr_y)
curr_reprs[x * y] = '(%s) * (%s)' % (repr_x, repr_y)
if y > 0 and x % y == 0:
curr_reprs[x // y] = '(%s)/(%s)' % (repr_x, repr_y)
curr_reprs.update(old_reprs)
return curr_reprs[target]
if __name__ == '__main__':
args = sys.argv[1:]
numbers = [int(a) for a in args]
if len(args) != 7:
print('Usage: ./program <arg1> <arg2> <arg3> <arg4> <arg5> <arg6> <arg7>')
else:
result = find_repr(numbers[6], numbers[:6]) + ' = {}'.format(numbers[6])
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment