Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
Add Comment
Please, Sign In to add comment