0% found this document useful (0 votes)
38 views23 pages

AI WK 11 Lec 21 22 Student

This document discusses the student code for solving mathematical equations. It describes the main functions like solve_equations, isolate, and reverse_op. It provides examples of how these functions work and the logic behind isolating variables on the left side of each equation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views23 pages

AI WK 11 Lec 21 22 Student

This document discusses the student code for solving mathematical equations. It describes the main functions like solve_equations, isolate, and reverse_op. It provides examples of how these functions work and the logic behind isolating variables on the left side of each equation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

CASE STUDY 3: STUDENT

(Solve Equations Module)

Dr. Umair Abdullah


Student Modules
1. Main student functions
1. Student
2. Remove_noise
2. Translate_to_expression

3. Solve_equations
Student Code Summary
Main student functions Solve_equation
1. from patmatch import * 1. solve_equations(eqs)
2. Variables: student_rules,
letters, operators, numbers
2. solve(eqs,solved=[])
3. student 3. solve_equation(var, eq)
4. remove_noise 4. find_one_unknowneq(eqs)
5. print_equations(eqs) 5. one_unknown2(equation)
Translate_to_expression 6. in_exp(itm, lst)
6. toeqstr(elst)
7. isolate(var, eq)
7. replace_item(n,o,lst)
8. translate_to_exp(eqwlst) 8. reverse_op(op)
Learning Objectives
• Solution of assignment # 10
• Module 2. Solving Mathematical Equations
• To Discuss Logic of Translate to expression module
• solve_equations(eqs)
• solve(eqs,solved=[])
• solve_equation(var, eq)
• find_one_unknowneq(eqs)
• one_unknown2(equation)
• in_exp(itm, lst)
• isolate(var, eq)
• reverse_op(op)
• Example tracing of Solve Equations Module
Student Code
def student():
userinput = input("Enter Statement:")
userinput = userinput.lower()
engEqs = userinput.split('.')
eqs = []
for engeq in engEqs:
engeq = engeq.split()
eqwlst = remove_noise(engeq)
eqs.append(translate_to_exp(eqwlst))
eqs = [eq for eq in eqs if eq != []]
solve_equations(eqs)
solve_equations(eqs)
def solve(eqs,solved=[]):
def solve_equations(eqs):
if len(eqs) == 0:
print("UNSOLVED EQUATIONS") return solved
print_equations(eqs) ans = find_one_unknowneq(eqs)
print('SOLVED EQUATIONS') var = ans[0]
solved = solve(eqs) eq = ans[1]
print_equations(solved) if eq != [] and var is not None:
sol=solve_equation(var,eq)
value = sol[1]
eqs.remove(eq)
eqs = replace_item(value, var,eqs)
solved.append([var,'=',value])
temp= solve(eqs,solved)
return temp
return solved
Isolate Variable on Left Hand Side and
Evaluate Right Hand Side
def solve_equation(var, eq):
eq2 = isolate(var, eq)
return eq2[0], str(eval(toeqstr(eq2[2])))
Finding equation with one variable
def one_unknown2(equation):
def find_one_unknowneq(eqs): c=0
for eq in eqs: var = None
ans = one_unknown2(eq) for item in equation:
if type(item) is list:
count = ans[1] ans= one_unknown2(item)
var = ans[0] c += ans[1]
if ans[1] != 0:
if count == 1: var = ans[0]
return var,eq else:
return None,[] flag = False
for ch in item:
if ch in letters:
flag = True
if flag == True:
c += 1
var = item
return var,c
Finding a variable
>>> one_unknown2([['a','+',[ 'd','+','3']],'=', ['4','+','bc']])
('bc', 3)
>>> one_unknown2([['a','+',[ 'd','+','3']],'=', ['4','+','5']])
('d', 2)
>>> one_unknown2([['7','+',[ 'd','+','3']],'=', ['4','+','5']])
('d', 1)
Searching a variable, Reversing Operator
def in_exp(itm, lst): def reverse_op(op):
if type(lst) is str: if op == '+':
return itm == lst return '-‘
elif lst == []: elif op == '-':
return False return '+‘
elif type(lst) is list: elif op == '*':
return in_exp(itm, lst[0]) return '/‘
or in_exp(itm, elif op == '/':
lst[1:]) return '*‘
elif op == '=':
return '='
Logic of Isolating a Variable
1. X = A STOP
2. A=X X=A
3. X+A=B X= B-A
4. X - A = B X = B + A
5. X*A=B X= B/A
6. X / A = B X = B * A
7. A+X=B X=B–A
8. A*X=B X=B/A
9. A–X=BX=A–B
10. A/X=BX=A/B
Isolating a variable
def isolate(var, eq):
lhs= eq[0] 1. X = A STOP
op = eq[1]
2. A=X X=A
rhs= eq[2]
if var == lhs: #CASE 1: X = A 3. X+A=B X= B-A
return eq 4. X-A=B X= B+A
elif var == rhs:
5. X*A=B X= B/A
return [rhs, op, lhs]
elif type(lhs) is str: 6. X/A=B X= B*A
return isolate(var, [rhs,op,lhs]) 7. A+X=B X=B–A
elif type(rhs) is list and op == '=' and in_exp(var,rhs):
return isolate(var, [rhs,op,lhs])
8. A*X=B X=B/A
elif in_exp(var,lhs[0]): 9. A–X=BX=A–B
return isolate(var, [lhs[0], '=', [rhs, reverse_op(lhs[1]), lhs[2]]]) 10. A/X=BX=A/B
elif lhs[1] == '+' or lhs[1] == '*':
return isolate(var, [lhs[2], '=', [rhs, reverse_op(lhs[1]), lhs[0]]])
else:
return isolate(var, [lhs[2], '=', [lhs[0], lhs[1], rhs]])
Isolate Sample Calls
>>> e1 = ['A', '=', ['2', '+', ['2', '*', '3']]]
>>> isolate('A', e1) 1. X = A STOP
['A', '=', ['2', '+', ['2', '*', '3']]] 2. A=X X=A
>>> e2 = [['2', '+', ['2', '*', '3']], '=', 'A'] 3. X+A=B X= B-A
>>> isolate('A', e2) 4. X-A=B X= B+A
['A', '=', ['2', '+', ['2', '*', '3']]] 5. X*A=B X= B/A
>>> e3 = ['4', '=', ['2', '+', ['A', '*', '3']]] 6. X/A=B X= B*A
>>> isolate('A', e3) 7. A+X=B X=B–A
['A', '=', [['4', '-', '2'], '/', '3']] 8. A*X=B X=B/A
>>> e4 = [['2', '+', ['2', '/', 'A']], '=', '4']
9. A–X=BX=A–B
>>> isolate('A', e4)
10. A/X=BX=A/B
['A', '=', ['2', '/', ['4', '-', '2']]]
>>> e5 = ['4', '=', ['2', '/', ['A', '*', '3']]]
>>> isolate('A', e5)
['A', '=', [['2', '/', '4'], '/', '3']]
Solve_Equations Module
• Repeat the following steps to solve equations:
1. Select equation with one variable
2. Isolate the variable
3. Evaluate the right side of equation to get value of the
variable
4. Replace the variable with its value in all equation
x plus y is equal to z minus 5 . find square of z . 6 minus x is 4 . and 8 divided by y is 1/4

UNSOLVED EQUATIONS SOLVED EQUATIONS

1. (x+y)=(z-5) 1. x=2
2. To-find = ( z * z ) 2. y = 2.0
3. (6-x)=4 3. z = 9.0
4. ( 8 / y ) = 1/4 4. To-find = 81.0
Solve-1: First Call of Solve function

UNSOLVED EQUATIONS SOLVED EQUATIONS


[ []
1. (x+y)=(z-5)
2. To-find = ( z * z )
3. (6-x)=4
4. ( 8 / y ) = 1/4
]
1. selection (equation with one unknown)
eq 3 selected  (6-x)=4
2. isolation  (x = ( 6 - 4))
3. solve-arithmetic  (x = 2)
4. substitution and recursive call
Solve-2: Second Call of Solve function

UNSOLVED EQUATIONS SOLVED EQUATIONS


[ [ (x = 2) ]
1. ( 2 + y ) = ( z - 5 )
2. To-find = ( z * z )
3.
4. ( 8 / y ) = 1/4
]
1. selection (equation with one unknown)
eq 4 selected  (( 8 / y) = 1/4)
2. isolation  (y = 8 / 1 / 4)
3. solve-arithmetic  (y = 2)
4. substitution and recursive call
Solve-3: Third Recursive Call of Solve function

UNSOLVED EQUATIONS SOLVED EQUATIONS


[ [ (x = 2) ,
1. ( 2 + 2 ) = ( z - 5 ) (y = 2) ]
2. To-find = ( z * z )
3.
4.
]
1. selection (equation with one unknown)
eq 1 selected  ( (2 + 2) = (z - 5))
2. isolation  ((z - 5) = (2 + 2))
 (z = ( (2 + 2) + 5))
3. solve-arithmetic  ( z = 9)
4. substitution and recursive call
Solve-4: Fourth Recursive Call of Solve function

UNSOLVED EQUATIONS SOLVED EQUATIONS


[ [ (x = 2) ,
1. (y = 2) ,
2. To-find = ( 9 * 9 ) (z = 9) ]
3.
4.
]
1. selection (equation with one unknown)
eq 2 selected  (To-find = (9 * 9))
2. isolation  (To-find = (9 * 9))
3. solve-arithmetic  (To-find = 81)
4. substitution and recursive call
Solve-5: Fifth Call of Solve function

UNSOLVED EQUATIONS SOLVED EQUATIONS


[ ] [
(x = 2) ,
(y = 2) ,
(z = 9) ,
(To-find = 81)
]
Sample Problems
1. what is twice of square of z . if z is 3
2. The average score is 73 . the maximum score is 97 . what is the square of the
difference between the average and maximum scores ?
3. Tom age is twice of Mary age . James age is half of the difference between Mary
and Tom ages . Mary is 18 years old . how old is James?
4. The price of a radio is 680 rupees . if this price is 15 % less than the marked price .
find the marked price .
5. x plus y is equal to z minus 5 . find square of z . 6 minus x is 4 . and 8 divided by y is
1/4
6. If the number of customers Tom gets is twice of the square of 20 % of the number
of advertisements he runs . and the number of advertisements is 45 . if the profit
Tom receives is 10 times the number of customers he gets . what is the profit ?
Sample Problems
7. Fran age divided by Robin height is one half Kelly IQ . Kelly IQ minus 30 is Robin height. If Robin is 4
feet tall . how old is Fran ?
8. The cost of MCS is equal to admission fee plus tution fee of all semesters. Tution fee is equal to
semester fee multiplied by total number of semesters of MCS program. Total number of semesters
in mcs program is 4. Semester fee is 40000. Admission fee is 12000. Find the cost of mcs program.
9. 75 % attendance is required for every course. attendance can be calculated as sum of class
attendance and lab attendance. percentage can be calculated as attendance of a student multiplied
by 100 divided by total classes held. what is percentage of Asif. if his class attendance is 30 . lab
attendance is 20. While total classes held are 60.
10.Sum of theory and practical marks obtained by a student makes the total marks of the student for a
course. Similarly, Theory marks are calculated by adding Internal marks and marks obtained during
exams. Moreover, marks of exams can be further distributed into Mid exam and Final Exam.
Internal marks can be divided into Quizzes and Assignments. Find total marks obtained by Asif if he
has obtained . 8 Marks in Quizzes. 6 marks in Assignments. 10 marks in Mid . 20 marks in Final
exam. Finally, 12 in practical lab work.
Assignment 11
QUESTION: Show the process how student program will solve the
following problem. Trace both ‘translate_to_expression’ and
‘sovle_equations’ modules for the given input.

“Fran age divided by Robin height is one half of Kelly IQ . Kelly IQ


minus 30 is Robin height. If Robin is 4 feet tall . how old is Fran ?”

You might also like