-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy path02ex_fundamentals.py
More file actions
285 lines (180 loc) · 6.58 KB
/
02ex_fundamentals.py
File metadata and controls
285 lines (180 loc) · 6.58 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
print("\nExercise 1\n")
'''
1\. **Global variables**
Convert the function $f$ into a function that doesn't use global variables and that does not modify the original list
'''
x = 5
def f(alist):
alist = [1, 2, 3]
for i in range(x):
alist.append(i)
return alist
alist = [1, 2, 3]
ans = f(alist)
print(ans)
print(alist)
print("\nExercise 2\n")
'''
2\. **List comprehension**
Write the following expression using a list comprehension:
`ans = list(map(lambda x: x * x, filter(lambda x: x % 2 == 1, range(10))))`
'''
ans = [x*x for x in range(10) if x % 2 == 1]
print(ans)
print("\nExercise 3\n")
'''
3. Filter list
Using the filter() hof, define a function that takes a list of words and an integer n as arguments, and returns a list of words that are shorter than n.
'''
from functools import partial
n=4
def is_short_n(words,n):
return len(words)<n
words = ["python", "c++", "java", "ruby", "c#" ]
print(list(filter(lambda x: is_short_n(x, n), words)))
print("\nExercise 4\n")
'''
4\. **Map dictionary**
Consider the following dictionary:
`lang = {"Python" : 3, "Java" : '', "Cplusplus" : 'test', "Php" : 0.7}`
Write a function that takes the above dictionary and uses the `map()` higher order function to return a list that contains the length of the keys of the dictionary.
'''
lang = {"Python" : 3, "Java" : '', "Cplusplus" : 'test', "Php" : 0.7}
def length_key(*lang):
length = []
for key, value in lang:
length.append(len(key))
return length
x = list( map(length_key,lang.items()) )
print(x)
print("\nExercise 5\n")
'''
5. Lambda functions
Write a Python program that sorts the following list of tuples using a lambda function, according to the alphabetical order of the first element of the tuple:
language_scores = [('Python', 97), ('Cplusplus', 81), ('Php', 45), ('Java', 32)]
Hint: use the method sort() and its argument key of the list data structure.
'''
language_scores = [('Python', 97), ('Cplusplus', 81), ('Php', 45), ('Java', 32)]
addit = lambda x: sorted(x)
print(addit(language_scores))
print("\nExercise 6\n")
'''
6. Nested functions
Write two functions: one that returns the square of a number, and one that returns its cube.
Then, write a third function that returns the number raised to the 6th power, using only the two previous functions.
'''
def square(x):
return x * x
def cube(x):
return x * x * x
def sixth_pow(a, b, n):
print(a(n)*b(n)*n)
n =2
sixth_pow(square,cube,n)
print("\nExercise 7\n")
'''
7. Decorators
Write a decorator named hello that makes every wrapped function print “Hello World!” each time the function is called.
The wrapped function should look like:
@hello
def square(x):
return x*x
'''
def hello(func):
def wrapper():
print("Before")
func()
print("After")
return wrapper
@hello
def say_hello():
print("Hello World!")
say_hello()
print("\nExercise 8\n")
'''
8. The Fibonacci sequence (part 2)
Calculate the first 20 numbers of the Fibonacci sequence using a recursive function.
'''
lisst = []
def recursiveFibonacci(x):
if x <=1:
return 1
return recursiveFibonacci(x - 1)+recursiveFibonacci(x-2)
fib_num = []
for i in range(0,20):
fib_num.append(recursiveFibonacci(i))
print(fib_num)
print("\nExercise 9\n")
'''
9. The Fibonacci sequence (part 3)
Run both the Fibonacci recursive function from the previous exercise, and the Fibonacci function from 01ex that use only for and while loops.
Measure the execution code of the two functions with timeit (link to the doc), for example:
%timeit loopFibonacci(20)
%timeit recursiveFibonacci(20)
which one is the most efficient implementation? By how much?
'''
import timeit
def loopFibonacci(n):
if n <= 1:
return n
fib = [0, 1]
for i in range(2, n+1):
fib.append(fib[-1] + fib[-2])
return fib[n]
starttime = timeit.default_timer()
print("The start time is :",starttime)
loopFibonacci(20)
print("The time difference is :", timeit.default_timer() - starttime)
starttime = timeit.default_timer()
print("The start time is :",starttime)
recursiveFibonacci(20)
print("The time difference is :", timeit.default_timer() - starttime)
print("\nExercise 10\n")
'''
10. Class definition
Define a class polygon. The constructor has to take a tuple as input that contains the length of each side. The (unordered) input list does not have to have a fixed length, but should contain at least 3 items.
Create appropriate methods to get and set the length of each side
Create a method perimeter() that returns the perimeter of the polygon
Create a method getOrderedSides(increasing = True) that returns a tuple containing the length of the sides arranged in increasing or decreasing order, depending on the argument of the method
Test the class by creating an instance and calling the perimeter() and getOrderedSides(increasing = True) methods.
'''
import math
class polygon:
def __init__(self, components):
self.x = components # a list is expected as input
def getLength(self):
return len(self.x)
def setLength(self, n,xi):
if n< len(self.x):
self.x[n] = xi
def perimeter(self):
sum_polygon = sum(self.x)
print("Perimeter is",sum_polygon)
def getOrderedSides(self,increasing):
if increasing==True:
print("Ascending order:",tuple(sorted(self.x,reverse=False)))
else:
print("Descending order:",tuple(sorted(self.x,reverse=True)))
length = polygon([1,2,3,6,4])
print("The length of the polygon:",length.getLength())
length.perimeter()
length.getOrderedSides(True)
print("\nExercise 11\n")
'''
11. Class inheritance
Define a class rectangle that inherits from polygon. Modify the constructor, if necessary, to make sure that the input data is consistent with the geometrical properties of a rectangle.
Create a method area() that returns the area of the rectangle.
Test the rectangle class by creating an instance and passing an appropriate input to the constructor.
'''
class rectangle(polygon): # class 'Vector3D' inherits from class 'VectorND'
# The constructor here is optional, and can be inherited from the parent class if omitted
def __init__(self, components):
if len(components) == 4 and sorted(components)[0] == sorted(components)[1] and sorted(components)[2] == sorted(components)[3] :
self.x = components # a list is expected as input
else:
print("Error: number of components is not 4 or it is not rectanle")
# New methods that only belong to the child class
def area(self):
print("Area of rectangle:",sorted(self.x)[0] * sorted(self.x)[-1])
rectangle = rectangle([3,1,1,3])
rectangle.area()