-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_kyu_Calculating with functions.py
190 lines (148 loc) · 4.4 KB
/
5_kyu_Calculating with functions.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
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
"""5_kyu_Calculating_with_Functions
https://www.codewars.com/kata/525f3eda17c7cd9f9e000b39/train/python
Instructions:
This time we want to write calculations using functions and get the results. Let's have a look at some examples:
seven(times(five())) # must return 35
four(plus(nine())) # must return 13
eight(minus(three())) # must return 5
six(divided_by(two())) # must return 3
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 consist 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. For example, this should return 2, not 2.666666...:
eight(divided_by(three()))
"""
# First try. Learn about nonlocal and global variables in functions
def zero(inner=None):
if inner is None:
global num2
num2 = 0
elif inner:
global num1
num1 = 0
return operator_switch(inner)
def one(inner=None):
if inner is None:
global num2
num2 = 1
elif inner:
global num1
num1 = 1
return operator_switch(inner)
def two(inner=None):
if inner is None:
global num2
num2 = 2
elif inner:
global num1
num1 = 2
return operator_switch(inner)
def three(inner=None):
if inner is None:
global num2
num2 = 3
elif inner:
global num1
num1 = 3
return operator_switch(inner)
def four(inner=None):
if inner is None:
global num2
num2 = 4
elif inner:
global num1
num1 = 4
return operator_switch(inner)
def five(inner=None):
if inner is None:
global num2
num2 = 5
elif inner:
global num1
num1 = 5
return operator_switch(inner)
def six(inner=None):
if inner is None:
global num2
num2 = 6
elif inner:
global num1
num1 = 6
return operator_switch(inner)
def seven(inner=None):
if inner is None:
global num2
num2 = 7
elif inner:
global num1
num1 = 7
return operator_switch(inner)
def eight(inner=None):
if inner is None:
global num2
num2 = 8
elif inner:
global num1
num1 = 8
return operator_switch(inner)
def nine(inner=None):
if inner is None:
global num2
num2 = 9
elif inner:
global num1
num1 = 9
return operator_switch(inner)
def plus(num1=int, num2=None):
if num2 is None:
return 'plus'
else:
return num1 + num2
def minus(num1=int, num2=None):
if num2 is None:
return 'minus'
else:
return num1 - num2
def times(num1=int, num2=None):
if num2 is None:
return 'times'
else:
return num1 * num2
def divided_by(num1=int, num2=None):
if num2 is None:
return 'divided_by'
else:
return int(num1 // num2)
def operator_switch(operator):
if operator == 'times':
return times(num1, num2)
elif operator == 'plus':
return plus(num1, num2)
elif operator == 'minus':
return minus(num1, num2)
elif operator == 'divided_by':
return divided_by(num1, num2)
print(seven(minus(zero())))
print(four(plus(nine())))
print(six(divided_by(two())))
print('-------')
# Second try. Using lambda functions and oneliners
def zero(outer=None): return 0 if outer is None else outer(0)
def one(outer=None): return 1 if outer is None else outer(1)
def two(outer=None): return 2 if outer is None else outer(2)
def three(outer=None): return 3 if outer is None else outer(3)
def four(outer=None): return 4 if outer is None else outer(4)
def five(outer=None): return 5 if outer is None else outer(5)
def six(outer=None): return 6 if outer is None else outer(6)
def seven(outer=None): return 7 if outer is None else outer(7)
def eight(outer=None): return 8 if outer is None else outer(8)
def nine(outer=None): return 9 if outer is None else outer(9)
def plus(inner): return lambda x: x + inner
def minus(inner): return lambda x: x - inner
def times(inner): return lambda x: x * inner
def divided_by(inner): return lambda x : x // inner
print(seven(minus(zero())))
print(four(plus(nine())))
print(six(divided_by(two())))