Question

In: Computer Science

IN PYTHON Write a program to do the following: Solve the a set of equations as...

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:-

  • Add function named 'add' instead of using operator '+' [10pts]
  • Add function named 'difference' instead of using operator '-' [10pts]
  • Add function named 'product' instead of using operator '*' [10pts]
  • Add function named 'divide' instead of using operator '/' but only upto a 5point precision [10pts]
  • Use docstrings in Section 6.16 or lecture slides to comment your functions [10pts]

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:

  • define a function "solve_eqs(eq1, eq2)" that take in two liss eq1 and eq2 each of length 3 as parameter and prints the solution [15pts]
    • for example if equation is ax+by=c, then eq1=[a,b,c]
    • If the length is not 3, then the program exits
  • the solver that uses functions that you implemented instead of "+ * / -"[15pts]
    Hint: Use "import calc" or "from calc import add" to import module or function and use "add" through function calls "calc.add" or "add" into your code (See 11.3 for more details)
  • Keep solving equations until user enters input that results in a list which has a length not equal to 3. [5pts]

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

Solutions

Expert Solution

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


Related Solutions

Write a python program that can solve system of linear equations in three variables using input...
Write a python program that can solve system of linear equations in three variables using input function. Paste your program in a word document or notepad. Note that I am using pycharm. please use a not really complex codes, thanks
Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
SET UP BUT DO NOT SOLVE the following system of linear equations: A company sells three...
SET UP BUT DO NOT SOLVE the following system of linear equations: A company sells three sizes of fruit trays. The small size contains 200 gr of watermelons and 100 gr of grapes. The medium size contains 400 gr of watermelons, 100 gr of pineapples, and 300 gr of grapes. The large size contains 600 gr of watermelons, 200 gr of pineapples, and 400 gr of grapes. Suppose that the company receives an order for 28 kg of watermelons, 6...
(Python) Write a program which accomplishes the following tasks: set a variable to the result of...
(Python) Write a program which accomplishes the following tasks: set a variable to the result of mathematical expression including +, -, * and / and of both Integer and Float values (or variables) set a variable to the result of a combination of string values (or variables) set a variable to the result of a combination of string, Integer and Float values (you may need to use the type casting functions) Using the following variables: a = 1.3 b =...
Write a Python program tat asks the user for the number of equations (n) to be...
Write a Python program tat asks the user for the number of equations (n) to be solved and fill the coefficient matrix A and the right matrix b with random integers. Limit the values of the elements of A to numbers between 0 and 50 and elements of b to 0 and 500. Lastly, put the matrices into an Excel file.
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
Write a RARS assembly language program to solve the following: For a set of integers stored...
Write a RARS assembly language program to solve the following: For a set of integers stored in an array, calculate the sum of the positive numbers and the sum of the negative numbers, storing the results into memory. In the data segment of the program, define an array consisting of 25 integers (words) that are a mix of positive and negative values. You can select any values you wish but try to create a balance of positive and negative numbers....
Write a program IN PYTHON of the JUPYTER NOOTBOOK that keeps getting a set of numbers...
Write a program IN PYTHON of the JUPYTER NOOTBOOK that keeps getting a set of numbers from user until the user enters "done". Then shows the count, total, and average of the entered numbers. This should the answer when finished Enter a number: 55 Enter a number: 90 Enter a number: 12 Enter a number: done You entered 3 numbers, total is 157, average is 52.33333
Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT