Guest User

base10 game

a guest
May 7th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. from itertools import permutations
  2.  
  3. def find_sum_equal_n():
  4. n = 0
  5. num = range(10)
  6. solutions = set()
  7. for perm in permutations(num):
  8. tple = product_sum(perm)
  9. if product_num(tple) == n:
  10. tple = well_ordered(tple)
  11. solutions.add(tple)
  12. return list(solutions)
  13.  
  14. def product_num(tple):
  15. total = tple[0]*tple[1] - tple[2]*tple[3]
  16. return total
  17.  
  18. def product_sum(perm):
  19. num1 = 100*perm[0] + 10*perm[1] + perm[2]
  20. num2 = 10*perm[3] + perm[4]
  21. num3 = 100*perm[5] + 10*perm[6] + perm[7]
  22. num4 = 10*perm[8] + perm[9]
  23. return (num1, num2, num3, num4)
  24.  
  25. def well_ordered(tple):
  26. a = max(tple[0], tple[1])
  27. b = min(tple[0], tple[1])
  28.  
  29. c = max(tple[2], tple[3])
  30. d = min(tple[2], tple[3])
  31.  
  32. if a > c:
  33. tple = (a,b,c,d)
  34. else:
  35. tple = (c,d,a,b)
  36. return tple
  37.  
  38.  
  39. def display_solutions(lst):
  40. print '============================================'
  41. for solution in lst:
  42. display_sum(solution)
  43. print '============================================'
  44.  
  45.  
  46. def display_sum(tple):
  47. print ' ' + str_num(tple[0], 3) + ' x ' + str_num(tple[1], 2)
  48. print ' - ' + str_num(tple[2], 3) + ' x ' + str_num(tple[3], 2)
  49. print ' = ', product_num(tple)
  50.  
  51.  
  52. def str_num(num, length):
  53. str_num = str(num)
  54. diff = max(length - len(str_num), 0)
  55. string = ' '*diff
  56. string += str_num
  57. return string
  58.  
  59. if __name__ == '__main__':
  60.  
  61. lst = find_sum_equal_n()
  62. display_solutions(lst)
  63. print len(lst)
Advertisement
Add Comment
Please, Sign In to add comment