Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Simple solver
- import os
- os.system("clear")
- print("Simple Solver")
- print("Enter a problem in the following format using integers only: ")
- print("1 2 3 == 7")
- print("Type q to quit")
- def solver(x, y, z, answer, o1 = "+", o2 = "+"):
- # Define list of possible operators
- operations = ["+", "-", "*", "/", "%"]
- # Base case
- equation = "{} {} {} {} {}".format(x, o1, y, o2, z)
- if eval(equation) == answer:
- print("\n{} = {}".format(equation, answer))
- return True
- if o1 == "%" and o2 == "%":
- return False
- # Next operation
- if o2 == "%":
- o1 = operations[operations.index(o1) + 1]
- o2 = "+"
- else:
- o2 = operations[operations.index(o2) + 1]
- return solver(x, y, z, answer, o1, o2)
- while True:
- # Get the problem from the user
- problem = input("\n> ")
- # Split into tokens
- tokens = problem.split()
- if tokens[0] == "q":
- break
- try:
- # Convert to integers
- x = int(tokens[0])
- y = int(tokens[1])
- z = int(tokens[2])
- answer = int(tokens[-1])
- except:
- print("Invalid input")
- continue
- solution_found = solver(x, y, z, answer)
- # Check if solution not found
- if not solution_found:
- print("No solution found")
- print("\nThe end")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement