-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculating_with_functions.py
More file actions
203 lines (169 loc) · 4.17 KB
/
Copy pathcalculating_with_functions.py
File metadata and controls
203 lines (169 loc) · 4.17 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Codewars Kata: Calculating with Functions (5 kyu)
Link: https://www.codewars.com/kata/525f3ed15025e45073000495
Description:
This time we want to write calculations using functions and get the results.
Requirements:
- There must be a function for each number from 0 ("zero") to 9 ("nine").
- There must be a function for each of the following mathematical operations:
plus, minus, times, divided_by.
- Each calculation consists of exactly one operation and two numbers.
- The most outer function represents the left operand, the most inner function represents the right operand.
- Division should be integer division.
Examples:
seven(times(five())) # 35
four(plus(nine())) # 13
eight(minus(three())) # 5
six(divided_by(two())) # 3
"""
# ==============================================================================
# New Solution (Functions returning closures)
# ==============================================================================
# Operation Functions
def plus(num2):
return lambda num1: num1 + num2
def minus(num2):
return lambda num1: num1 - num2
def times(num2):
return lambda num1: num1 * num2
def divided_by(num2):
return lambda num1: num1 // num2
# Number Functions
def zero(operation=lambda num1: num1):
return operation(0)
def one(operation=lambda num1: num1):
return operation(1)
def two(operation=lambda num1: num1):
return operation(2)
def three(operation=lambda num1: num1):
return operation(3)
def four(operation=lambda num1: num1):
return operation(4)
def five(operation=lambda num1: num1):
return operation(5)
def six(operation=lambda num1: num1):
return operation(6)
def seven(operation=lambda num1: num1):
return operation(7)
def eight(operation=lambda num1: num1):
return operation(8)
def nine(operation=lambda num1: num1):
return operation(9)
# ==============================================================================
# Old Solution (Decorators and global state)
# ==============================================================================
#
# operation_list = []
# operation = ""
#
#
# def is_it_three(func):
# def inner(*args, **kwargs):
# global operation
#
# # Before FNC
#
# # FNC
# res = func(*args, **kwargs)
#
# # After FNC
# if len(operation_list) == 2:
# num1 = operation_list[1]
# num2 = operation_list[0]
# op = operation
#
# # Bir sonraki işlem için temizlik
# operation_list.clear()
# operation = ""
#
# if op == "+":
# return num1 + num2
# elif op == "-":
# return num1 - num2
# elif op == "*":
# return num1 * num2
# elif op == "/":
# return num1 // num2
# else:
# return res
#
# return inner
#
#
# @is_it_three
# def zero(a=None):
# operation_list.append(0)
#
#
# @is_it_three
# def one(a=None):
# operation_list.append(1)
#
#
# @is_it_three
# def two(a=None):
# operation_list.append(2)
#
#
# @is_it_three
# def three(a=None):
# operation_list.append(3)
#
#
# @is_it_three
# def four(a=None):
# operation_list.append(4)
#
#
# @is_it_three
# def five(a=None):
# operation_list.append(5)
#
#
# @is_it_three
# def six(a=None):
# operation_list.append(6)
#
#
# @is_it_three
# def seven(a=None):
# operation_list.append(7)
#
#
# @is_it_three
# def eight(a=None):
# operation_list.append(8)
#
#
# @is_it_three
# def nine(a=None):
# operation_list.append(9)
#
#
# def plus(num):
# global operation
# operation = "+"
#
#
# def minus(num):
# global operation
# operation = "-"
#
#
# def times(num):
# global operation
# operation = "*"
#
#
# def divided_by(num):
# global operation
# operation = "/"
# ==============================================================================
# Driver & Tests
# ==============================================================================
if __name__ == "__main__":
print(seven(times(five()))) # 35
print(four(plus(nine()))) # 13
print(eight(minus(three()))) # 5
print(six(divided_by(two()))) # 3
print(one(times(zero()))) # 0