-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-Function Practice Exercises.py
415 lines (220 loc) · 6.55 KB
/
03-Function Practice Exercises.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# coding: utf-8
# # Function Practice Exercises
#
# Problems are arranged in increasing difficulty:
# * Warmup - these can be solved using basic comparisons and methods
# * Level 1 - these may involve if/then conditional statements and simple methods
# * Level 2 - these may require iterating over sequences, usually with some kind of loop
# * Challenging - these will take some creativity to solve
# ## WARMUP SECTION:
# #### LESSER OF TWO EVENS: Write a function that returns the lesser of two given numbers *if* both numbers are even, but returns the greater if one or both numbers are odd
# lesser_of_two_evens(2,4) --> 2
# lesser_of_two_evens(2,5) --> 5
# In[34]:
def lesser_of_two_evens(a,b):
if a%2==0 and b%2==0:
print(min(a,b))
else:
print(max(a,b))
# In[35]:
# Check
lesser_of_two_evens(2,4)
# In[36]:
# Check
lesser_of_two_evens(5,2)
# #### ANIMAL CRACKERS: Write a function takes a two-word string and returns True if both words begin with same letter
# animal_crackers('Levelheaded Llama') --> True
# animal_crackers('Crazy Kangaroo') --> False
# In[37]:
def animal_crackers(text):
wordlist=text.split()
return wordlist[0][0]==wordlist[1][0]
# In[38]:
# Check
animal_crackers('Levelheaded Llama')
# In[39]:
# Check
animal_crackers('Crazy Kangaroo')
# #### MAKES TWENTY: Given two integers, return True if the sum of the integers is 20 *or* if one of the integers is 20. If not, return False
#
# makes_twenty(20,10) --> True
# makes_twenty(12,8) --> True
# makes_twenty(2,3) --> False
# In[53]:
def makes_twenty(n1,n2):
return n1+n2==20 or n1==20 or n2==20
# In[54]:
# Check
makes_twenty(20,10)
# In[55]:
# Check
makes_twenty(2,3)
# # LEVEL 1 PROBLEMS
# #### OLD MACDONALD: Write a function that capitalizes the first and fourth letters of a name
#
# old_macdonald('macdonald') --> MacDonald
#
# Note: `'macdonald'.capitalize()` returns `'Macdonald'`
# In[5]:
def old_macdonald(name):
if len(name) > 3:
return name[:3].capitalize + name[3:].capitalize()
else:
return 'name is dhort'
# In[7]:
# Check
old_macdonald('macdonald')
# #### MASTER YODA: Given a sentence, return a sentence with the words reversed
#
# master_yoda('I am home') --> 'home am I'
# master_yoda('We are ready') --> 'ready are We'
#
# Note: The .join() method may be useful here. The .join() method allows you to join together strings in a list with some connector string. For example, some uses of the .join() method:
#
# >>> "--".join(['a','b','c'])
# >>> 'a--b--c'
#
# This means if you had a list of words you wanted to turn back into a sentence, you could just join them with a single space string:
#
# >>> " ".join(['Hello','world'])
# >>> "Hello world"
# In[8]:
def master_yoda(text):
return ' '.join(text.split()[::-1])
# In[16]:
# Check
master_yoda('I am home')
# In[13]:
# Check
master_yoda('We are ready')
# #### ALMOST THERE: Given an integer n, return True if n is within 10 of either 100 or 200
#
# almost_there(90) --> True
# almost_there(104) --> True
# almost_there(150) --> False
# almost_there(209) --> True
#
# NOTE: `abs(num)` returns the absolute value of a number
# In[24]:
def almost_there(n):
#for i in range(100,200):
return ((abs(100-n) <= 10) or (abs(200-n)<= 10 ))
# In[18]:
# Check
almost_there(104)
# In[27]:
# Check
almost_there(150)
# In[20]:
# Check
almost_there(209)
# # LEVEL 2 PROBLEMS
# #### FIND 33:
#
# Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.
#
# has_33([1, 3, 3]) → True
# has_33([1, 3, 1, 3]) → False
# has_33([3, 1, 3]) → False
# In[ ]:
def has_33(nums):
pass
# In[ ]:
# Check
has_33([1, 3, 3])
# In[ ]:
# Check
has_33([1, 3, 1, 3])
# In[ ]:
# Check
has_33([3, 1, 3])
# #### PAPER DOLL: Given a string, return a string where for every character in the original there are three characters
# paper_doll('Hello') --> 'HHHeeellllllooo'
# paper_doll('Mississippi') --> 'MMMiiissssssiiippppppiii'
# In[ ]:
def paper_doll(text):
pass
# In[ ]:
# Check
paper_doll('Hello')
# In[ ]:
# Check
paper_doll('Mississippi')
# #### BLACKJACK: Given three integers between 1 and 11, if their sum is less than or equal to 21, return their sum. If their sum exceeds 21 *and* there's an eleven, reduce the total sum by 10. Finally, if the sum (even after adjustment) exceeds 21, return 'BUST'
# blackjack(5,6,7) --> 18
# blackjack(9,9,9) --> 'BUST'
# blackjack(9,9,11) --> 19
# In[ ]:
def blackjack(a,b,c):
pass
# In[ ]:
# Check
blackjack(5,6,7)
# In[ ]:
# Check
blackjack(9,9,9)
# In[ ]:
# Check
blackjack(9,9,11)
# #### SUMMER OF '69: Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 9 (every 6 will be followed by at least one 9). Return 0 for no numbers.
#
# summer_69([1, 3, 5]) --> 9
# summer_69([4, 5, 6, 7, 8, 9]) --> 9
# summer_69([2, 1, 6, 9, 11]) --> 14
# In[ ]:
def summer_69(arr):
pass
# In[ ]:
# Check
summer_69([1, 3, 5])
# In[ ]:
# Check
summer_69([4, 5, 6, 7, 8, 9])
# In[ ]:
# Check
summer_69([2, 1, 6, 9, 11])
# # CHALLENGING PROBLEMS
# #### SPY GAME: Write a function that takes in a list of integers and returns True if it contains 007 in order
#
# spy_game([1,2,4,0,0,7,5]) --> True
# spy_game([1,0,2,4,0,5,7]) --> True
# spy_game([1,7,2,0,4,5,0]) --> False
#
# In[ ]:
def spy_game(nums):
pass
# In[ ]:
# Check
spy_game([1,2,4,0,0,7,5])
# In[ ]:
# Check
spy_game([1,0,2,4,0,5,7])
# In[ ]:
# Check
spy_game([1,7,2,0,4,5,0])
# #### COUNT PRIMES: Write a function that returns the *number* of prime numbers that exist up to and including a given number
# count_primes(100) --> 25
#
# By convention, 0 and 1 are not prime.
# In[ ]:
def count_primes(num):
pass
# In[ ]:
# Check
count_primes(100)
# ### Just for fun:
# #### PRINT BIG: Write a function that takes in a single letter, and returns a 5x5 representation of that letter
# print_big('a')
#
# out: *
# * *
# *****
# * *
# * *
# HINT: Consider making a dictionary of possible patterns, and mapping the alphabet to specific 5-line combinations of patterns. <br>For purposes of this exercise, it's ok if your dictionary stops at "E".
# In[ ]:
def print_big(letter):
pass
# In[ ]:
print_big('a')
# ## Great Job!