In: Computer Science
IN PYTHON Write a program to do the following:
Solve the a set of equations as mentioned in "Zybook 5.20 LAB: Brute force equation solver". Instead of the arithmetic operators use your own function defined in a module named calc.
You must provide two files (calc.py, brute_force_solver.py) :-
1) calc.py:-
2) Use test_calc.py and run it in python to make sure your functions are implemented correctly. (do not modify) [15pts]
You should see an output as shown below. If you see an error try to understand the error and fix your function in calc.py
Ran 7 tests in 0.028s OK
3) brute_force_solver.py:
A sample run must look like this:
Equation 1 (enter ax+b=c as 'a b c'):8 7 38 Equation 2 (enter ax+b=c as 'a b c'):3 -5 -1 3 2 Equation 1 (enter ax+b=c as 'a b c'):5 2 3 Equation 2 (enter ax+b=c as 'a b c'):4 2 9 No solution Equation 1 (enter ax+b=c as 'a b c'):1 -1 6 Equation 2 (enter ax+b=c as 'a b c'):1 1 8 7 1 Equation 1 (enter ax+b=c as 'a b c'):1s jkj Equation 2 (enter ax+b=c as 'a b c'):hj k Quitting
calc.py
Test_Calc.py
brute_force_solver.py
Output
brute_force_solver.py code
import sys
sys.stdin=open("input.txt","r")
import calc
def solve_eqs(eq1,eq2):
if len(eq1)!=3 or len(eq2)!=3:
return 'Quit'
else:
x=calc.difference(calc.product(eq2[2],eq1[1]),calc.product(eq1[2],eq2[1]))/calc.difference(calc.product(eq1[1],eq2[0]),calc.product(eq2[1],eq1[0]))
y=calc.divide(calc.difference(eq1[2],calc.product(eq1[0],x)),eq1[1])
return x,y
while True:
try:
eq1=list(map(int,input("Equation 1 (enter ax+by=c as 'a b c'):").split()))
eq2=list(map(int,input("Equation 2 (enter ax+by=c as 'a b c'):").split()))
ans=solve_eqs(eq1,eq2)
if ans=='Quit':
print('Quitting')
break
else:
print(ans[0],ans[1])
except:
print("Quitting")
break
Proof for above value of x and y