Answered step by step
Verified Expert Solution
Question
1 Approved Answer
My unfinished Code below: class H01: def __init__(self): pass def eval(self, exprn): return exprn.strip() def parse(token): return int(token) def eval(exprn): tokens = exprn.split('+') # print(tokens)
My unfinished Code below:
class H01: def __init__(self): pass
def eval(self, exprn): return exprn.strip()
def parse(token): return int(token)
def eval(exprn): tokens = exprn.split('+') # print(tokens) sum = 0 for token in tokens: token = token.strip() try: sum += parse(token) except: return 'ERROR'
return str(sum)
****
Fix the function eval(self, exprn) so that it can evaluate any expression of sums of non-negative fractions. The function returns the sum as a string (an integer or reduced fraction) if the expression is legal; it returns the string 'ERROR' otherwise. Note: There may be spaces between numbers and symbols in the input expression. The output of eval() does not contain any space. The input expression may contains non-reduced fractions such as 12/20, 70/25, and 40/8. The output must be simplified: either an integer or a reduced fraction. The slash '/' is part of the fractional notation, do NOT perform any division! With a finite number of storage of digits in decimal numbers, it is impossible to convert back and forth between a fraction and its decimal representation. You must solve this problem from the scratch, use only the basic Python string methods. You will parse the expression using a somewhat naive and brute-force approach. Do NOT use regular expressions methods, parsers, finite machines or any libraries. You will learn sophisticated theories and methods to evaluate much more complicated expressions later in this course. Do not change any of the following: class name: H01 name and signature of the constructor: _init(self) name and signature of the 'target' function: eval(self, exprn) ===> Milestone 1: single integers 0 0 4 4 25786 25786 1/2 Milestone 2: single reduced fractions 1/2 4/15 4/15 36/55 36/55 ===> Milestone 3: single non-reduced integers 48/12 ===> 3 80/5 16 5/1 ===> 5 ===> Milestone 4: single non-reduced fractions 4/12 ===> 1/3 36/15 12/5 770/49 110/7 ===> ===> Milestone 5: sum of two terms (integers or fractions) 3 +4 7 2 + 16/20 14/5 32/12 + 105/30 37/6 ===> === ===> Milestone 6: sum of three or more terms 3 + 4 + 10 17 2 + 0 + 4/3 10/3 3/2 + 5/8 + 1/8 + 3/4 3 1/2 + 3/8 + 1/6 + 7/8 71/24 62/59 + 25 + 70/38 + 21/8 273685/8968 4321 + 1/2 + 10/3 + 100/4 + 1000/5 + 10000/6 + 100000/ 7 + 10/98765 === ===> 5669723371/276542 Fix the function eval(self, exprn) so that it can evaluate any expression of sums of non-negative fractions. The function returns the sum as a string (an integer or reduced fraction) if the expression is legal; it returns the string 'ERROR' otherwise. Note: There may be spaces between numbers and symbols in the input expression. The output of eval() does not contain any space. The input expression may contains non-reduced fractions such as 12/20, 70/25, and 40/8. The output must be simplified: either an integer or a reduced fraction. The slash '/' is part of the fractional notation, do NOT perform any division! With a finite number of storage of digits in decimal numbers, it is impossible to convert back and forth between a fraction and its decimal representation. You must solve this problem from the scratch, use only the basic Python string methods. You will parse the expression using a somewhat naive and brute-force approach. Do NOT use regular expressions methods, parsers, finite machines or any libraries. You will learn sophisticated theories and methods to evaluate much more complicated expressions later in this course. Do not change any of the following: class name: H01 name and signature of the constructor: _init(self) name and signature of the 'target' function: eval(self, exprn) ===> Milestone 1: single integers 0 0 4 4 25786 25786 1/2 Milestone 2: single reduced fractions 1/2 4/15 4/15 36/55 36/55 ===> Milestone 3: single non-reduced integers 48/12 ===> 3 80/5 16 5/1 ===> 5 ===> Milestone 4: single non-reduced fractions 4/12 ===> 1/3 36/15 12/5 770/49 110/7 ===> ===> Milestone 5: sum of two terms (integers or fractions) 3 +4 7 2 + 16/20 14/5 32/12 + 105/30 37/6 ===> === ===> Milestone 6: sum of three or more terms 3 + 4 + 10 17 2 + 0 + 4/3 10/3 3/2 + 5/8 + 1/8 + 3/4 3 1/2 + 3/8 + 1/6 + 7/8 71/24 62/59 + 25 + 70/38 + 21/8 273685/8968 4321 + 1/2 + 10/3 + 100/4 + 1000/5 + 10000/6 + 100000/ 7 + 10/98765 === ===> 5669723371/276542Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started