fork download
  1. import re
  2. from random import shuffle
  3.  
  4. def main(eq):
  5. terms = re.split(' \+ | = ', eq)
  6. digits = list(range(1, 10))
  7. for term in terms:
  8. for num in term:
  9. if num.isdigit():
  10. digits.remove(int(num))
  11. solved = False
  12. x = 0
  13. while not solved:
  14. temp_eq = str(eq)
  15. temp_list = list(digits)
  16. shuffle(temp_list)
  17. while "x" in temp_eq:
  18. n = str(temp_list.pop())
  19. temp_eq = temp_eq.replace("x", n, 1)
  20. temp_terms = re.split(' \+ | = ', temp_eq)
  21. if(int(temp_terms[0]) + int(temp_terms[1]) == int(temp_terms[2])):
  22. print(eq)
  23. print("Solved in " + str(x) + " tries")
  24. print(temp_eq +"\n\n")
  25. solved = True
  26. x += 1
  27.  
  28. main("xxx + x81 = 9x4")
  29. main("xxx + 5x1 = 86x")
  30. main("xxx + 39x = x75")
Success #stdin #stdout 0.02s 12760KB
stdin
Standard input is empty
stdout
xxx + x81 = 9x4
Solved in 62 tries
673 + 281 = 954


xxx + 5x1 = 86x
Solved in 150 tries
293 + 571 = 864


xxx + 39x = x75
Solved in 61 tries
284 + 391 = 675