Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def compute(s, part2=False):
- def eval_term(i):
- return eval_seq(i + 1, eat=1) if s[i] == '(' else (int(s[i]), i + 1)
- def eval_seq(i, eat=0):
- value, i = eval_term(i)
- while i < len(s) and s[i] != ')':
- is_mul = s[i + 1] == '*'
- value2, i = (eval_seq if part2 and is_mul else eval_term)(i + 3)
- value = value * value2 if is_mul else value + value2
- return value, i + eat
- return eval_seq(0)[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement