SHOW:
|
|
- or go back to the newest paste.
1 | op_order = ['+', '-', '/', '*'] | |
2 | - | t = s.rsplit('+', 1) |
2 | + | |
3 | - | if len(t) > 1: |
3 | + | |
4 | - | return add(t[0], t[1]) |
4 | + | |
5 | for op in op_order: | |
6 | - | t = s.rsplit('-', 1) |
6 | + | t = s.rsplit(op, 1) |
7 | - | if len(t) > 1: |
7 | + | if len(t) == 2: |
8 | - | return subtract(t[0], t[1]) |
8 | + | return ops[op](t[0], t[1]) |
9 | return int(s) | |
10 | - | t = s.rsplit('/', 1) |
10 | + | |
11 | - | if len(t) > 1: |
11 | + | |
12 | - | return divide(t[0], t[1]) |
12 | + | |
13 | return evaluate(l) / evaluate(r) | |
14 | - | t = s.rsplit('*', 1) |
14 | + | |
15 | - | if len(t) > 1: |
15 | + | |
16 | - | return multiply(t[0], t[1]) |
16 | + | |
17 | return evaluate(l) * evaluate(r) | |
18 | ||
19 | ||
20 | def add(l, r): | |
21 | return evaluate(l) + evaluate(r) | |
22 | ||
23 | ||
24 | def subtract(l, r): | |
25 | return evaluate(l) - evaluate(r) | |
26 | ||
27 | ||
28 | ops = { | |
29 | '+': add, | |
30 | '-': subtract, | |
31 | '/': divide, | |
32 | '*': multiply, | |
33 | } |