Eval¶
Let’s consider the evaluation of expressions formed from the 4 arithmetic operadors on float vales. For example, the result of evaluating 0.0+5.0-1.0+2.0-3.0 is 3.0.
You are required to implement the following functions in the module eval_module (file eval_module.py):
The first function is
- eval_op(v1, op, v2)¶
such that
given v1, v2 both
floatand op astrreturns two values:
a
boolwhich isTrueif and only if op one of the followingstr:'+','-','*','/'and, if it is'/'v2 must not be0.0, and isFalseotherwise.a
floatrounded to 1 decimal with either:
the result from applying the operator op to
v1iv2, if the previous value isTrueor
v1, if the previous value isFalse.
For example:
>>> eval_op(0.0, '+', 5.0) (True, 5.0) >>> eval_op(8.0, '/', 3.0) (True, 2.7) >>> eval_op(8.0, '/', 0.0) (False, 8.0) >>> eval_op(8.0, '//', 3.0) (False, 8.0)
Doctests are available in the eval_op.test file.
Once eval_op is ready, the second function required is
- eval(val0, op1, val1, op2, val2, op3, val3, op4, val4)¶
such that
given 9 parameters where all valX are
floatand all opX arestrreturns two values:
a
boolsuch that isTrueif all the operations succeeded andFalseotherwise.a
floatsuch that it is either:
val0if the previous value isFalsethe result from operating
val0with the 4 arithmetic operations and the value next to it in left to right order.This function must call the previous function.
Per exemple:
>>> eval(0.0, '+', 5.0, '-', 1.0, '+', 2.0, '-', 3.0) (True, 3.0) >>> eval(0.0, '+', 5.0, '-', 1.0, '*', 2.0, '/', 3.0) (True, 2.7) >>> eval(0.0, '+', 0.0, '-', 0.0, '*', 0.0, '/', 0.0) (False, 0.0) >>> eval(0.0, '+', 5.0, '-', 1.0, '*', 2.0, '//', 3.0) (False, 0.0)
Doctests are available in the eval.test file.