-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathproblem_27_alternative.py
More file actions
43 lines (33 loc) · 1.15 KB
/
problem_27_alternative.py
File metadata and controls
43 lines (33 loc) · 1.15 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
'''
Write a program that maps a list of words into a list of
integers representing the lengths of the correponding words.
Write it in three different ways:
1) using a for-loop,
2) using the higher order function map()
3) using list comprehensions
'''
def size_of_words_1(list_a):
size = []
for item in list_a:
count = 0
for i in item:
count += 1 # no need extra for loop
size.append(count) # could be .append(len(item))
return size
#####################################################
def size_of_words_2(list_b):
def size(string):
count = 0
for i in string:
count += 1 # no need extra function
return count # shortcut - map(len,list_b)
return list(map(size ,list_b)) # only map doesn't work
# list(map(f,seq))
#####################################################
##### List comprehension is new to me, gd shortcut :)
def size_of_words_3(list_c):
return [len(item) for item in list_c]
ab = ['mithila','prokriti','amrita']
print(size_of_words_1(ab))
print(size_of_words_2(ab))
print(size_of_words_3(ab))