-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathfunctions.py
276 lines (222 loc) · 6.07 KB
/
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
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
##############################
# INBUILT FUNCTIONS
##############################
"""
The variable x
x = -1.5
and the follwing expreasion are given:
expression = 'x**2 + x'
Using the appropriate function, calculate thevalue of this expression
and print the result to the console
Tip: Use the eval() funtion.
"""
x = -1.5
expression = 'x**2 + x'
print(eval(expression))
"""
The following variables are given:
var1 = 'Python'
var2 = ('Python')
var3 = ('Python',)
var4 = ['Python']
var5 = {'Python'}
Using the appropriate function, chek if the variables are instances
of tuple class. Print the result to the console
Tip: Use the isinstance() built-in function.
"""
var1 = 'Python'
var2 = ('Python')
var3 = ('Python',)
var4 = ['Python']
var5 = {'Python'}
print(isinstance(var1, tuple))
print(isinstance(var2, tuple))
print(isinstance(var3, tuple))
print(isinstance(var4, tuple))
print(isinstance(var5, tuple))
"""
The following list is given:
characters = ['k', 'b', 'c', 'j', 'z', 'w']
Using the built-in functions, return the first and the last letter in
alphabetical order from the list and print the result to the console
Tip: use the min() and max() functions.
Expected result
First: b
Last: z
"""
characters = ['k', 'b', 'c', 'j', 'z', 'w']
print((f'First: {min(characters)}'))
print((f'Last: {max(characters)}'))
"""
Two tuples are given:
ticker = ('TEN', 'PLW', 'CDR')
full_name = ('Ten Square Games', 'Playway', 'CD Projekt')
Using appropriate built-in function, create a list consisting of
tuples (ticker, full_name) and print the result to the console
Tip: use the zip() function.
Expected result:
[('TEN', 'Ten Square Games'), ('PLW', 'Playway'), ('CDR', 'CD Projekt')]
"""
ticker = ('TEN', 'PLW', 'CDR')
full_name = ('Ten Square Games', 'Playway', 'CD Projekt')
result = list(zip(ticker, full_name))
print(result)
"""
Using the appropriate built-in function, verify if all elements of the
following tuple return a logical value True
items = (' ', '0', 0.1, True)
Print the result to the console
Exptected result:
True
"""
items = (' ', '0', 0.1, True)
print(all(items))
"""
Using the appropriate built-in function, verify if any element of the
following tuple returns the boolean value True
items = ('', 0.0, 0, False)
Print the result to the console
"""
items = ('', 0.0, 0, False)
print(all(items))
"""
Count the number of ones in the binary representaion of the number;
number = 234
print the resutl to the console
"""
number = 234
binary = bin(number)
binary = binary[2:] # as the first two digits are representations
print(binary.count('1'))
#################################
# DEFINING YOUR OWN FUNCTIONS
#################################
"""
Implement a function called maximum() that returns the maximum of two
numbers. Use conditional statement
"""
def maximum(x, y):
if x > y:
return x
else:
return y
print(maximum(2,6))
"""
Implement a function called maximum() that returns the maximum of three
numbers. Use conditional statement
"""
def maximum(x, y, z):
if x >= y and x >= z:
return x
elif y >= z:
return y
else:
return z
print(maximum(3,2,5))
"""
Implement a function called multi(), which accepts an iterable
object (list, tuple) as an argument and returns the product of all
elements of this iterable object.
"""
def multi(numbers):
product = 1
for number in numbers:
product *= number
return product
print(multi([3, 2, 4]))
"""
Implement a function map_longest() that accepts the list of words and
reutn the word with the longest length in the list.
"""
def map_longest(words):
dic = {word: len(word) for word in words}
longest = max(dic, key= lambda x: dic[x])
return longest
print(map_longest(['python', 'sql', 'javascript']))
"""
Implement a function called filter_ge_6() that takes a list of word
and returns list of words with the length greater than or equal to
6 characters
"""
def filter_ge_6(words):
fil = [word for word in words if len(word)>=6]
return fil
print(filter_ge_6(['python', 'sql', 'javascript']))
"""
Implement a function factorial() that calculates the factorial for a
given number.
"""
def factorial(num):
#numbers = [n for n in range(1, num+1)]
result = 1
for n in range(1, num+1):
result *=n
return result
print(factorial(10))
"""
Implement a function count_str(), which returns the number of str objects
in an iterable object (list tuple, set).
"""
def count_str(obj):
total = 0
for item in items:
if isinstance(item, str):
total += 1
return total
"""
Implement a function count_str(), which returns the number of str objects
with a lenght more than 2 characters from an iterable object (list tuple, set).
"""
def count_str(obj):
total = 0
for item in items:
if isinstance(item, str):
if len(item) > 2:
total += 1
return total
"""
Implement a function remove_duplicates() that removes duplicates from
the list (the order of the items in the list does not have to be kept)
"""
def remove_duplicates(string):
result = []
for item in string:
if item not in result:
result.append(item)
return result
print(remove_duplicates([1, 1, 1, 1, 1]))
# Solution using set
def remove_duplicates(items):
return list(set(items))
"""
Implement a function is_distinct() to check if the list contains unique
"""
def is_distinct(items):
return len(items) == len(set(items))
"""
Analyse the results of the functions below
"""
def function(idx, l=[]):
for i in range(idx):
l.append(i ** 3)
print(l)
function(3)
function(5, ['a', 'b', 'c'])
function(6)
def function(*args, **kwargs):
print(args, kwargs)
function(3,4)
function(x=3, y=4)
function(1, 2, x=3, y=4)
"""
Implement a function is_palindrome(), which takes as an argument
str object and checks if the object is a palindrome
If so, the function should return True, on the contrary False
"""
def is_palindrome(string):
reverse = string[::-1]
if string == reverse:
return True
else:
return False
print(is_palindrome('level'))