-
Notifications
You must be signed in to change notification settings - Fork 102
/
decorator_1_solution.py
60 lines (39 loc) · 1.13 KB
/
decorator_1_solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Register mathematical functions using decorators.
"""
import functools
import re
FUNCTIONS = {}
RE_OPERATION = re.compile(r"\((?P<operation>\w+) (?P<v1>\d+) (?P<v2>\d+)\)")
def register(func):
@functools.wraps(func)
def wrapped(v1: int, v2: int) -> int:
result = func(v1, v2)
print(f"result: {result}")
return result
FUNCTIONS[func.__name__] = wrapped
return wrapped
@register
def add(v1: int, v2: int) -> int:
return v1 + v2
@register
def sub(v1: int, v2: int) -> int:
return v1 - v2
@register
def mul(v1: int, v2: int) -> int:
return v1 * v2
@register
def div(v1: int, v2: int) -> int:
return v1 / v2
def compute(expression: str) -> int:
"""Compute a Polish Notation expression."""
m = RE_OPERATION.match(expression)
operation, v1, v2 = m.group("operation"), int(m.group("v1")), int(m.group("v2"))
return FUNCTIONS[operation](v1, v2)
def test_compute() -> None:
assert compute("(add 1 2)") == 3
assert compute("(mul 1 2)") == 2
assert compute("(div 4 2)") == 2
assert compute("(sub 5 2)") == 3
if __name__ == "__main__":
test_compute()