Created
February 11, 2017 20:32
-
-
Save monkeymantra/c6773cb68cae947580a5b2d37ab6bf84 to your computer and use it in GitHub Desktop.
Joel's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
operators = ['+', '-', '*', '/'] | |
class Expression(): | |
def __init__(self, input_ss): | |
self.input_ss = input_ss | |
def eval(self): | |
for op in operators: | |
if op in ['+', '-']: | |
result = 0 | |
if op in ['*', '/']: | |
result = 1 | |
if len(self.input_ss.split(op)) > 1: | |
sub_expression_strings = self.input_ss.split(op) | |
for ss in sub_expression_strings: | |
result = do_op(result, op, Expression(ss).eval()) | |
return result | |
# this is only called if the substring had no operators | |
return int(self.input_ss) * 1.0 | |
def do_op(initial, operator, number): | |
if operator == '+': | |
return initial + number | |
if operator == '-': | |
return initial - number | |
if operator == '*': | |
return initial * number | |
if operator == '/': | |
return initial / number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment