-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel2T.py
More file actions
143 lines (108 loc) · 3.95 KB
/
level2T.py
File metadata and controls
143 lines (108 loc) · 3.95 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
"""
Transform all names to uppercase using [normal list - list comprehension - functional programming]
"""
Names = ['mahmoud','farida','ali','hassan','mohamed','khaled','taha']
def uppercase_names():
print("Task 1 - Normal List:")
upper_names_normal = []
for name in Names:
upper_names_normal.append(name.upper())
print(upper_names_normal)
print("Task 1 - List Comprehension:")
upper_names_list_comprehension = [name.upper() for name in Names]
print(upper_names_list_comprehension)
print("Task 1 - Functional Programming:")
upper_names_functional = list(map(str.upper, Names))
print(upper_names_functional)
print('-'*40)
"""
Get the names that contains ‘a’ in a list using [normal list - list comprehension - functional
programming]
"""
def names_with_a():
print("Task 2 - Normal List:")
names_with_a_normal = []
for name in Names:
if 'a' in name:
names_with_a_normal.append(name)
print(names_with_a_normal)
print("Task 2 - List Comprehension:")
names_with_a_list_comprehension = [name for name in Names if 'a' in name]
print(names_with_a_list_comprehension)
print("Task 2 - Functional Programming:")
names_with_a_functional = list(filter(lambda name: 'a' in name, Names))
print(names_with_a_functional)
print('-'*40)
"""
Get the names that starts with ‘t’ in a list using [normal list - list comprehension - functional
programming]
"""
def names_starting_with_t():
print("Task 3 - Normal List:")
names_with_t_normal = []
for name in Names:
if name.startswith('t'):
names_with_t_normal.append(name)
print(names_with_t_normal)
print("Task 3 - List Comprehension:")
names_with_t_list_comprehension = [name for name in Names if name.startswith('t')]
print(names_with_t_list_comprehension)
print("Task 3 - Functional Programming:")
names_with_t_functional = list(filter(lambda name: name.startswith('t'), Names))
print(names_with_t_functional)
print('-'*40)
"""
Get the names that contains 2 or more ‘a’ letter using [normal list - list comprehension - functional
programming]
"""
def names_with_2_or_more_a():
print("Task 4 - Normal List:")
names_with_2a_normal = []
for name in Names:
if name.count('a') >= 2:
names_with_2a_normal.append(name)
print(names_with_2a_normal)
print("Task 4 - List Comprehension:")
names_with_2a_list_comprehension = [name for name in Names if name.count('a') >= 2]
print(names_with_2a_list_comprehension)
print("Task 4 - Functional Programming:")
names_with_2a_functional = list(filter(lambda name: name.count('a') >= 2, Names))
print(names_with_2a_functional)
print('-'*40)
"""Task 5: Print a list contains the len of each word in the list"""
def name_lengths():
print("Task 5 - Normal List:")
lengths_normal = []
for name in Names:
lengths_normal.append(len(name))
print(lengths_normal)
print("Task 5 - List Comprehension:")
lengths_list_comprehension = [len(name) for name in Names]
print(lengths_list_comprehension)
print("Task 5 - Functional Programming:")
lengths_functional = list(map(len, Names))
print(lengths_functional)
print('-'*40)
"""
Unpack the list in
"""
def unpack_list():
print("Task 6, 7, 8, 9: Unpack the list in a,b")
"""Task 7 - a= the first index , b = rest of the list"""
print("Task 7 - a= the first index , b = rest of the list")
a, *b = Names
print("a:", a)
print("b:", b)
print('-'*40)
"""Task 8 - a = the first index , b = the last index"""
print("Task 8 - a = the first index , b = the last index")
a, *_, b = Names
print("a:", a)
print("b:", b)
print('-'*40)
"""Task 9 - a = the first index , b = the second index"""
print("Task 9 - a = the first index , b = the second index")
a, b, *_ = Names
print("a:", a)
print("b:", b)
print('-'*40)