Guest User

AskMath Basic arithmetic operations script

a guest
Nov 5th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. # Online Python compiler (interpreter) to run Python online.
  2. # Write Python 3 code in this online editor and run it.
  3. def has_duplicates(seq):
  4.     return len(seq) != len(set(seq))
  5.  
  6. nums = list(map(int, input("Numbers to calculate on: ").split()))
  7. target = int(input("Target number: "))
  8.  
  9. operations = ['+', '-', '*', '/']
  10.  
  11. for op1 in operations:
  12.     for op2 in operations:
  13.         for op3 in operations:
  14.             for op4 in operations:
  15.                 if has_duplicates([op1, op2, op3, op4]):
  16.                     continue
  17.                
  18.                 equationStr = f"{nums[0]}{op1}{nums[1]}{op2}{nums[2]}{op3}{nums[3]}{op4}{nums[4]}"
  19.                
  20.                 ans = eval(f"{nums[0]}{op1}{nums[1]}")
  21.                 ans = eval(f"{ans}{op2}{nums[2]}")
  22.                 ans = eval(f"{ans}{op3}{nums[3]}")
  23.                 ans = eval(f"{ans}{op4}{nums[4]}")
  24.                
  25.                 if ans == target:
  26.                     print(equationStr)
  27.                     print("Answer:", op1, op2, op3, op4)
  28.                     break
  29.  
  30. print("Program end")
Advertisement
Add Comment
Please, Sign In to add comment