-
Notifications
You must be signed in to change notification settings - Fork 0
/
363.py
39 lines (28 loc) · 983 Bytes
/
363.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
"""
Problem:
Write a function, add_subtract, which alternately adds and subtracts curried arguments.
Here are some sample operations:
add_subtract(7) -> 7
add_subtract(1)(2)(3) -> 1 + 2 - 3 -> 0
add_subtract(-5)(10)(3)(9) -> -5 + 10 - 3 + 9 -> 11
"""
from __future__ import annotations
class CallableInt(int):
def __init__(self, value: int) -> None:
int.__init__(value)
self.should_add = True
def __call__(self, value: int) -> CallableInt:
if self.should_add:
result = CallableInt(self + value)
else:
result = CallableInt(self - value)
result.update_should_add(not self.should_add)
return result
def update_should_add(self, should_add: bool) -> None:
self.should_add = should_add
def add_subtract(value: int) -> CallableInt:
return CallableInt(value)
if __name__ == "__main__":
print(add_subtract(7))
print(add_subtract(1)(2)(3))
print(add_subtract(-5)(10)(3)(9))