Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Week # 14 Lecture # 27 & 28

CASE STUDY-3: STUDENT SOLVE EQUATIONS MODULE

RELATED VIDEOS ON YOUTUBE

Sr. Channel Video Name URL

Lecture Objectives https://youtu.be/488tG5BOML8


1 Dr Umair Abdullah
Assignment 10 solution https://youtu.be/TL2q_CldUr4
Solve Equation module https://youtu.be/OT6Ay8b4Ifc
2 Dr Umair Abdullah Finding One Variable Equation https://youtu.be/fSw-nH7Lomw
Isolate function https://youtu.be/KKB7ZUYDrWc

3 Dr Umair Abdullah Tracing of Solve Recursive Module https://youtu.be/pWSvQue4rlQ

Learning Objectives
1. Solution of assignment # 10
2. Module 2. Solving the list of equations
3. Steps to solve equations
4. Code of Solve
5. Logic of isolate
6. Code of isolate
7. Printing of equations

PRE-REQUISITS

1. You have learned basics of Python programming


2. You have been able to successfully execute the translate_to_expression module

1
SOLUTION (Assignment # 10)

QUESTION: Show the process how translate_to_expression module of student program will
convert following paragraph into equations
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.

SOLUTION:

['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', '']
Input: " 75 % attendance is required for every course "
Rule: " ?*a is ?*b " , [ ?a = ?b ]
Binding: {'a': ['75', '%', 'attendance'], 'b': ['required', 'for', 'every', 'course']}
Input: " 75 % attendance "
Input: " required for every course "
Output: ['75', '=', 'required']
Input: " attendance can be calculated as sum class attendance and lab attendance "
Rule: " ?*a can be calculated as ?*b " , [ ?a = ?b ]
Binding: {'a': ['attendance'], 'b': ['sum', 'class', 'attendance', 'and', 'lab', 'attendance']}
Input: " attendance "
Input: " sum class attendance and lab attendance "
Rule: " sum ?*a and ?*b " , [ ?a + ?b ]
Binding: {'a': ['class', 'attendance'], 'b': ['lab', 'attendance']}
Input: " class attendance "
Input: " lab attendance "
Output: ['class', '+', 'lab']
Output: ['attendance', '=', ['class', '+', 'lab']]
Input: " percentage can be calculated as attendance student multiplied by 100 divided by total
classes held "
Rule: " ?*a can be calculated as ?*b " , [ ?a = ?b ]
Binding: {'a': ['percentage'], 'b': ['attendance', 'student', 'multiplied', 'by', '100', 'divided', 'by', 'total',
'classes', 'held']}
Input: " percentage "
Input: " attendance student multiplied by 100 divided by total classes held "
Rule: " ?*a divided by ?*b " , [ ?a / ?b ]
Binding: {'a': ['attendance', 'student', 'multiplied', 'by', '100'], 'b': ['total', 'classes', 'held']}

2
Input: " attendance student multiplied by 100 "
Rule: " ?*a multiplied by ?*b " , [ ?a * ?b ]
Binding: {'a': ['attendance', 'student'], 'b': ['100']}
Input: " attendance student "
Input: " 100 "
Output: ['attendance', '*', '100']
Input: " total classes held "
Output: [['attendance', '*', '100'], '/', 'total']
Output: ['percentage', '=', [['attendance', '*', '100'], '/', 'total']]
Input: " what is percentage Asif "
Rule: " ?*a is ?*b " , [ ?a = ?b ]
Binding: {'a': ['what'], 'b': ['percentage', 'Asif']}
Input: " what "
Input: " percentage Asif "
Output: ['what', '=', 'percentage']
Input: " class attendance is 30 "
Rule: " ?*a is ?*b " , [ ?a = ?b ]
Binding: {'a': ['class', 'attendance'], 'b': ['30']}
Input: " class attendance "
Input: " 30 "
Output: ['class', '=', '30']
Input: " lab attendance is 20 "
Rule: " ?*a is ?*b " , [ ?a = ?b ]
Binding: {'a': ['lab', 'attendance'], 'b': ['20']}
Input: " lab attendance "
Input: " 20 "
Output: ['lab', '=', '20']
Input: " total classes held are 60 "
Rule: " ?*a are ?*b " , [ ?a = ?b ]
Binding: {'a': ['total', 'classes', 'held'], 'b': ['60']}
Input: " total classes held "
Input: " 60 "
Output: ['total', '=', '60']
Input: " "
UNSOLVED EQUATIONS
75 = required
attendance = ( class + lab )
percentage = ( ( attendance * 100 ) / total )

3
what = percentage
class = 30
lab = 20
total = 60
SOLVED EQUATIONS
required = 75
class = 30
lab = 20
attendance = 50
total = 60
percentage = 83.33333333333333

4
CASE STUDY 3: STUDENT

MODULE 2: Solving Mathematical Equations

 Module 2. Solving the list of equations

Repeat the following steps to solve equations:


1. Select equation with one variable
2. Isolate the variable on left hand side of the equation
3. Evaluate the right side of equation to get value of the variable
4. Replace the variable with its value in all equation

 Code of Solve
def solve(eqs,solved=[]):
# print (solved)
# print (eqs)
if len(eqs) == 0:
return solved
ans = find_one_unknowneq(eqs)
var = ans[0]
eq = ans[1]
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

5
 Logic of isolate
1. Equal lhs unknown then Stop
2. If unknown in rhs then swap sides and recursive call
3. X+A=B X= B-A
X-A=B X= B+A
X*A=B X= B/A
X/A=B X= B*A
4. A+X=B X=B–A
A*X=B X=B/A
5. A–X=BX=A–B
A/X=BX=A/B

 Code of isolate
def isolate(var, eq):
lhs= eq[0]
op = eq[1]
rhs= eq[2]
if var == lhs: #CASE 1: X = A
return eq
elif var == rhs:
return [rhs, op, lhs]
elif type(lhs) is str:
return isolate(var, [rhs,op,lhs])
elif type(rhs) is list and op == '=' and in_exp(var,rhs):
return isolate(var, [rhs,op,lhs])
elif in_exp(var,lhs[0]):
return isolate(var, [lhs[0], '=', [rhs, reverse_op(lhs[1]), lhs[2]]])
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]])

 Sample Calls of Isolate

>>> e1 = ['A', '=', ['2', '+', ['2', '*', '3']]]


>>> e1
['A', '=', ['2', '+', ['2', '*', '3']]]
>>> isolate('A', e1)
['A', '=', ['2', '+', ['2', '*', '3']]]
>>> e2 = [['2', '+', ['2', '*', '3']], '=', 'A']
>>> isolate('A', e2)
['A', '=', ['2', '+', ['2', '*', '3']]]
>>> e3 = ['4', '=', ['2', '+', ['A', '*', '3']]]
>>> isolate('A', e3)
['A', '=', [['4', '-', '2'], '/', '3']]

6
>>> e4 = [['2', '+', ['2', '/', 'A']], '=', '4']
>>> isolate('A', e4)
['A', '=', ['2', '/', ['4', '-', '2']]]
>>> e5 = ['4', '=', ['2', '/', ['A', '*', '3']]]
>>> isolate('A', e5)
['A', '=', [['2', '/', '4'], '/', '3']]
>>> e6 = [['2', '-', ['2', '*', 'A']], '=', '4']
>>> isolate('A', e6)
['A', '=', [['2', '-', '4'], '/', '2']]
>>> e7 = ['4', '=', ['2', '/', ['A', '/', '3']]]
>>> isolate('A', e7)
['A', '=', [['2', '/', '4'], '/', '3']]
>>> e8 = [['2', '/', ['2', '/', 'A']], '=', '4']
>>> isolate('A', e8)
['A', '=', ['2', '/', ['2', '/', '4']]]
>>> e9 = [['4', '+', ['5', '+', '3']], '=', ['4', '+', 'A']]
>>> isolate('A', e9)
['A', '=', [['4', '+', ['5', '+', '3']], '-', '4']]
>>> e10 = [['4', '+', ['A', '/', '3']], '=', ['4', '-', '3']]
>>> isolate('A', e9)
['A', '=', [['4', '+', ['5', '+', '3']], '-', '4']]
>>> e10 = [['4', '+', ['A', '/', '3']], '=', ['8', '-', '3']]
>>> isolate('A', e10)
['A', '=', [[['8', '-', '3'], '-', '4'], '/', '3']]
>>> e11 = [['4', '-', ['5', '-', 'A']], '=', ['8', '+', '3']]
>>> isolate('A', e11)
['A', '=', ['5', '-', ['4', '-', ['8', '+', '3']]]]

7
Steps to solve equations of Example 3

Solve.1
(x + y) = (z - 5), (To-find = (z * z)),
(6 - x) = 4, (8 / y ) = 1/4

1. selection (equation with one unknown) ( 6 - x ) = 4


2. isolation  (x = ( 6 - 4))
3. solve-arithmetic  (x = 2)
4. substitution and recursive call

Solve recursive number 2


(x + y) = (z - 5), (x = 2)
(To-find = (z * z)),
(8 / y ) = 1/4

1. selection (equation with one unknown) (( 8 / y) = 1/4)


2. isolation  (y = 8 / 1 / 4)
3. solve-arithmetic  (y = 2)
4. substitution and recursive call

Solve recursive number 3


(x + y) = (z - 5), (x = 2)
To-find = (z * z), (y = 2)

1. selection (equation with one unknown) ( (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 recursive number 4


(x = 2), (y = 2)
(To-find = (z * z)) (z = 9)

1. selection (equation with one unknown) (To-find = (9 * 9))


2. isolation  (To-find = (9 * 9))
3. solve-arithmetic  (To-find = 81)
4. substitution and recursive call

8
Solve recursive number 5
[] (x = 2), (y = 2),(To-find = 81),(z = 9)

Solve 5.4.3.2.1 Return value:


[(x = 2), (y = 2),(To-find = 81),(z = 9)]

Print-equations function will display the solved equations in on output window.

EXAMPLE 2: TRACING OF COMPLETE STUDENT PROCESS

Enter Statement: 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.

A. TRACING OF TRANSLATE_TO_EXPRESSION MODULE

['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', '']

Input: " sum theory and practical marks obtained by student makes total marks student for
course "
Rule: " ?*a makes ?*b " , [ ?a = ?b ]
Binding: {'a': ['sum', 'theory', 'and', 'practical', 'marks', 'obtained', 'by', 'student'], 'b': ['total',
'marks', 'student', 'for', 'course']}
Input: " sum theory and practical marks obtained by student "
Rule: " sum ?*a and ?*b " , [ ?a + ?b ]
Binding: {'a': ['theory'], 'b': ['practical', 'marks', 'obtained', 'by', 'student']}
Input: " theory "
Input: " practical marks obtained by student "
Output: ['theory', '+', 'practical']
Input: " total marks student for course "
Output: [['theory', '+', 'practical'], '=', 'total']
Input: " theory marks are calculated by adding internal marks and marks obtained during
exams "

9
Rule: " ?*a calculated by ?*b " , [ ?a = ?b ]
Binding: {'a': ['theory', 'marks', 'are'], 'b': ['adding', 'internal', 'marks', 'and', 'marks', 'obtained',
'during', 'exams']}
Input: " theory marks are "
Input: " adding internal marks and marks obtained during exams "
Rule: " adding ?*a and ?*b " , [ ?a + ?b ]
Binding: {'a': ['internal', 'marks'], 'b': ['marks', 'obtained', 'during', 'exams']}
Input: " internal marks "
Input: " marks obtained during exams "
Output: ['internal', '+', 'marks']
Output: ['theory', '=', ['internal', '+', 'marks']]
Input: " marks exams can be distributed into mid exam and final exam "
Rule: " ?*a can be ?*b " , [ ?a = ?b ]
Binding: {'a': ['marks', 'exams'], 'b': ['distributed', 'into', 'mid', 'exam', 'and', 'final', 'exam']}
Input: " marks exams "
Input: " distributed into mid exam and final exam "
Rule: " distributed into ?*a and ?*b " , [ ?a + ?b ]
Binding: {'a': ['mid', 'exam'], 'b': ['final', 'exam']}
Input: " mid exam "
Input: " final exam "
Output: ['mid', '+', 'final']
Output: ['marks', '=', ['mid', '+', 'final']]
Input: " internal marks can be divided into quizzes and assignments "
Rule: " ?*a can be ?*b " , [ ?a = ?b ]
Binding: {'a': ['internal', 'marks'], 'b': ['divided', 'into', 'quizzes', 'and', 'assignments']}
Input: " internal marks "
Input: " divided into quizzes and assignments "
Rule: " divided into ?*a and ?*b " , [ ?a + ?b ]
Binding: {'a': ['quizzes'], 'b': ['assignments']}
Input: " quizzes "
Input: " assignments "
Output: ['quizzes', '+', 'assignments']
Output: ['internal', '=', ['quizzes', '+', 'assignments']]
Input: " find total marks obtained by Asif he has obtained "
Rule: " find ?*a " , [ To-find = ?a ]
Binding: {'a': ['total', 'marks', 'obtained', 'by', 'Asif', 'he', 'has', 'obtained']}
Input: " total marks obtained by Asif he has obtained "
Output: ['To-find', '=', 'total']
Input: " 8 marks in quizzes "
Rule: " ?*a in ?*b " , [ ?a = ?b ]
Binding: {'a': ['8', 'marks'], 'b': ['quizzes']}
Input: " 8 marks "
Input: " quizzes "
Output: ['8', '=', 'quizzes']
Input: " 6 marks in assignments "
Rule: " ?*a in ?*b " , [ ?a = ?b ]
Binding: {'a': ['6', 'marks'], 'b': ['assignments']}
Input: " 6 marks "
Input: " assignments "
Output: ['6', '=', 'assignments']
Input: " 10 marks in mid "

10
Rule: " ?*a in ?*b " , [ ?a = ?b ]
Binding: {'a': ['10', 'marks'], 'b': ['mid']}
Input: " 10 marks "
Input: " mid "
Output: ['10', '=', 'mid']
Input: " 20 marks in final exam "
Rule: " ?*a in ?*b " , [ ?a = ?b ]
Binding: {'a': ['20', 'marks'], 'b': ['final', 'exam']}
Input: " 20 marks "
Input: " final exam "
Output: ['20', '=', 'final']
Input: " 12 in practical lab work "
Rule: " ?*a in ?*b " , [ ?a = ?b ]
Binding: {'a': ['12'], 'b': ['practical', 'lab', 'work']}
Input: " 12 "
Input: " practical lab work "
Output: ['12', '=', 'practical']
Input: " "
UNSOLVED EQUATIONS
( theory + practical ) = total
theory = ( internal + marks )
marks = ( mid + final )
internal = ( quizzes + assignments )
To-find = total
8 = quizzes
6 = assignments
10 = mid
20 = final
12 = practical

B. TRACING SOLVE EQUATIONS MODULE

R.Call SOLVED EQUATIONS UN-SOLVED EQUATIONS


No
1 ( theory + practical ) = total []
theory = ( internal + marks )
marks = ( mid + final )
internal = ( quizzes + assignments )
To-find = total
8 = quizzes
6 = assignments
10 = mid
20 = final
12 = practical

1. Selection : 8 = quizzes
2. Isolation : quizzes = 8
3. Evaluate (RHS) : quizzes = 8
4. Replace

11
2 ( theory + practical ) = total [
theory = ( internal + marks ) quizzes = 8
marks = ( mid + final )
internal = ( 8 + assignments )
To-find = total ]
6 = assignments
10 = mid
20 = final
12 = practical

1. Selected: 6 = assignments
2. Isolation: assignments = 6
3. Evaluate (RHS) : assignments = 6
4. Replace
3 ( theory + practical ) = total [
theory = ( internal + marks ) quizzes = 8
marks = ( mid + final ) assignments = 6
internal = ( 8 + 6 )
To-find = total ]
10 = mid
20 = final
12 = practical

1. Selected: internal = ( 8 + 6 )
2. Isolation: internal = ( 8 + 6 )
3. Evaluate (RHS) : internal = 14
4. Replace
4 ( theory + practical ) = total [
theory = ( 14 + marks ) quizzes = 8
marks = ( mid + final ) assignments = 6
To-find = total internal = 14
10 = mid ]
20 = final
12 = practical

1. Selected: 10 = mid
2. Isolation: mid = 10
3. Evaluate (RHS) : mid = 10
4. Replace
5 ( theory + practical ) = total [
theory = ( 14 + marks ) quizzes = 8
marks = ( 10 + final ) assignments = 6
To-find = total internal = 14
20 = final mid = 10
12 = practical ]

1. Selected: 20 = final
2. Isolation: final = 20
3. Evaluate (RHS) : final = 20
4. Replace

12
6 ( theory + practical ) = total [
theory = ( 14 + marks ) quizzes = 8
marks = ( 10 + 20 ) assignments = 6
To-find = total internal = 14
12 = practical mid = 10
final = 20
1. Selected: marks = ( 10 + 20 ) ]
2. Isolation: marks = ( 10 + 20 )
3. Evaluate (RHS) : marks = 30
4. Replace
7 ( theory + practical ) = total [
theory = ( 14 + 30 ) quizzes = 8
To-find = total assignments = 6
12 = practical internal = 14
mid = 10
1. Selected: theory = ( 14 + 30 ) final = 20
2. Isolation: theory = ( 14 + 30 ) marks = 30
3. Evaluate (RHS) : theory = 44 ]
4. Replace
8 ( 44 + practical ) = total [
To-find = total quizzes = 8
12 = practical assignments = 6
internal = 14
1. Selected: 12 = practical mid = 10
2. Isolation: practical = 12 final = 20
3. Evaluate (RHS) : practical = 12 marks = 30
4. Replace theory = 44
]
9 ( 44 + 12 ) = total [
To-find = total quizzes = 8
assignments = 6
1. Selected: ( 44 + 12 ) = total internal = 14
2. Isolation: total = ( 44 + 12 ) mid = 10
3. Evaluate (RHS) : total = 56 final = 20
4. Replace marks = 30
theory = 44
practical = 12
]
10 To-find = 56 [
quizzes = 8
1. Selected: To-find = 56 assignments = 6
2. Isolation: To-find = 56 internal = 14
3. Evaluate (RHS) : To-find = 56 mid = 10
4. Replace final = 20
marks = 30
theory = 44
practical = 12
total = 56
]
10 [ ] [
quizzes = 8
assignments = 6

13
internal = 14
mid = 10
final = 20
marks = 30
theory = 44
practical = 12
total = 56
To-find = 56
]

SOLVED EQUATIONS
quizzes = 8
assignments = 6
internal = 14
mid = 10
final = 20
marks = 30
theory = 44
practical = 12
total = 56
To-find = 56

14
TEST THE FOLLOWING STATEMENTS AS INPUT TO STUDENT

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 10 % 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 . 8 divided by y is ¼

6. If the number of customers Tom gets is twice of the square of 20 % of the number of
advertisements he runs . the number of advertisements is 45 . if the profit Tom receives is
10 times the number of customers he gets . what is the profit ?

7. 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 ?

8. The cost of MCS is equal to admission fee plus tuition fee of all semesters. Tuition 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.

15
STUDENT COMPLETE CODE
from patmatch import * # or write the patmatch code in this file
operators = "+-*/%"
numbers = "0123456789"
letters = "abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ"
student_rules = [
('find ?*a' , ['To-find' ,'=', '?a']),
('?*a is equal to ?*b' , ['?a' ,'=', '?b']),
('?*a makes ?*b' , ['?a' ,'=', '?b']),
('?*a are calculated by ?*b' , ['?a' ,'=', '?b']),
('?*a calculated by ?*b' , ['?a' ,'=', '?b']),
('?*a can be calculated as ?*b' , ['?a' ,'=', '?b']),
('?*a calculated as ?*b' , ['?a' ,'=', '?b']),
('?*a can be ?*b' , ['?a' ,'=', '?b']),
('?*a equal to ?*b' , ['?a' ,'=', '?b']),
('?*a equals ?*b', ['?a', '=', '?b']),
('?*a is ?*b' , ['?a' ,'=', '?b']),
('?*a are ?*b' , ['?a' ,'=', '?b']),
('?*a in ?*b' , ['?a' ,'=', '?b']),
('?*a plus ?*b', ['?a', '+', '?b']),
('?*a minus ?*b', ['?a', '-', '?b']),
('?*a divided by ?*b', ['?a', '/', '?b']),
('twice ?*a' , ['2' ,'*', '?a']),
('sum ?*a and ?*b' , ['?a' ,'+', '?b']),
('adding ?*a and ?*b' , ['?a' ,'+', '?b']),
('distributed into ?*a and ?*b', ['?a', '+', '?b']),
('divided into ?*a and ?*b', ['?a', '+', '?b']),
('one half ?*a' , ['?a' ,'/', '2']),
('half ?*a' , ['?a' ,'/', '2']),
('square ?*a' , ['?a' ,'*', '?a']),
('?*a times ?*b' , ['?a' ,'*', '?b']),
('?*a multiplied by ?*b' , ['?a' ,'*', '?b']),
('?*a multiplied with ?*b' , ['?a' ,'*', '?b']),
('difference between ?*a and ?*b' , ['?b' ,'-', '?a']),
('?*a % less than ?*b' , ['?b' ,'/', ['1', '-', ['?a', '/', '100']]])
]
def toeqstr(elst):
ans = ''
if type(elst) is str:
return elst
for item in elst:
if type(item) is list:
ans = ans + " ( " + toeqstr(item) + " )"
else:
if ans == '':
ans= item
else:
ans = ans + ' '+ str( item)
return ans
def replace_item(n,o,lst):
if lst == []:
return []
elif o == lst:
return n
elif type(lst) is not list:
return lst
else:
ans = replace_item(n,o,lst[1:])
ans.insert(0,replace_item(n,o,lst[0]))
return ans
16
def translate_to_exp(eqwlst):
print('Input: "', toeqstr(eqwlst),'"')
for pattern, response in student_rules:
pattern = pattern.split()
bindings = match_pattern(pattern, eqwlst)
if bindings:
print('Rule:"',toeqstr(pattern),'",[', toeqstr(response),']')
print('Binding:', bindings)
for var,val in bindings.items():
#print(var)
tval = translate_to_exp(val)
response = replace_item(tval,'?'+var,response)
print('Output:',response)
return response
if eqwlst == []:
return eqwlst
print('Output:','"',eqwlst[0],'"')
return eqwlst[0]

def remove_noise(txt):
nwords =['a','the','number','of','this','if','that',
'$',',and','his' ,'while']
for w in nwords:
if txt.count(w) > 0:
txt = [a for a in txt if a != w]
return txt

def student():
userinput = input("Enter Statement:")
userinput = userinput.lower()
engEqs = userinput.split('.')
eqs = []
for engeq in engEqs:
engeq = engeq.split()
eqwlst = remove_noise(engeq)
eq1 = translate_to_exp(eqwlst)
eqs.append(eq1)
eqs = [eq for eq in eqs if eq != []]
solve_equations(eqs)

def solve_equations(eqs):
print("UNSOLVED EQUATIONS")
print_equations(eqs)
print('SOLVED EQUATIONS')
solved = solve(eqs)
print_equations(solved)

def print_equations(eqs):
for eq in eqs:
print(toeqstr(eq))

17
def solve(eqs,solved=[]):
if len(eqs) == 0:
return solved
ans = find_one_unknowneq(eqs)
var = ans[0]
eq = ans[1]
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

def solve_equation(var, eq):


eq2 = isolate(var, eq)
return eq2[0], str(eval(toeqstr(eq2[2])))

def find_one_unknowneq(eqs):
for eq in eqs:
ans = one_unknown2(eq)
count = ans[1]
var = ans[0]
if count == 1:
return var,eq
return None,[]

def one_unknown2(equation):
c = 0
var = None
for item in equation:
if type(item) is list:
ans= one_unknown2(item)
c += ans[1]
if ans[1] != 0:
var = ans[0]
else:
flag = False
for ch in item:
if ch in letters:
flag = True
if flag == True:
c += 1
var = item
return var,c

def in_exp(itm, lst):


if type(lst) is str:
return itm == lst
elif lst == []:
return False
elif type(lst) is list:
return in_exp(itm, lst[0]) or in_exp(itm, lst[1:])

18
def reverse_op(op):
if op == '+':
return '-'
elif op == '-':
return '+'
elif op == '*':
return '/'
elif op == '/':
return '*'
elif op == '=':
return '='

def isolate(var, eq):


lhs= eq[0]
op = eq[1]
rhs= eq[2]
if var == lhs: #CASE 1: X = A
return eq
elif var == rhs:
return [rhs, op, lhs]
elif type(lhs) is str:
return isolate(var, [rhs,op,lhs])
elif type(rhs) is list and op == '=' and in_exp(var,rhs):
return isolate(var, [rhs,op,lhs])
elif in_exp(var,lhs[0]):
return isolate(var, [lhs[0], '=', [rhs, reverse_op(lhs[1]), lhs[2]]])
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]])

student()

------------------- END -------------------

19

You might also like