Guest User

Untitled

a guest
Dec 12th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. import sys
  2. import re
  3.  
  4.  
  5. def find_repr(target, numbers):
  6. org_reprs = dict((number, str(number)) for number in numbers)
  7. curr_reprs = org_reprs
  8. while target not in curr_reprs:
  9. old_reprs, curr_reprs = curr_reprs, {}
  10. for x in old_reprs:
  11. for y in org_reprs:
  12. repr_x, repr_y = old_reprs[x], old_reprs[y]
  13. curr_reprs[x + y] = '(%s) + (%s)' % (repr_x, repr_y)
  14. curr_reprs[x - y] = '(%s) - (%s)' % (repr_x, repr_y)
  15. curr_reprs[x * y] = '(%s) * (%s)' % (repr_x, repr_y)
  16. if y > 0 and x % y == 0:
  17. curr_reprs[x // y] = '(%s)/(%s)' % (repr_x, repr_y)
  18. curr_reprs.update(old_reprs)
  19. return curr_reprs[target]
  20.  
  21.  
  22. if __name__ == '__main__':
  23.  
  24. args = sys.argv[1:]
  25. numbers = [int(a) for a in args]
  26. if len(args) != 7:
  27. print('Usage: ./program <arg1> <arg2> <arg3> <arg4> <arg5> <arg6> <arg7>')
  28. else:
  29. result = find_repr(numbers[6], numbers[:6]) + ' = {}'.format(numbers[6])
  30. print(result)
Add Comment
Please, Sign In to add comment