-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDay_11.py
275 lines (204 loc) · 4.08 KB
/
Day_11.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
#!/usr/bin/env python
# coding: utf-8
# # Question 38
#
# ### **Question:**
#
# > **_With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line._**
#
# ---
#
# ### Hints:
#
# > **_Use [n1:n2] notation to get a slice from a tuple._**
#
# ---
#
#
#
# **Solutions:**
# In[1]:
tpl = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
for i in range(0, 5):
print(tpl[i], end=" ")
print()
for i in range(5, 10):
print(tpl[i], end=" ")
# **OR**
# In[2]:
tpl = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
lst1, lst2 = [], []
for i in range(0, 5):
lst1.append(tpl[i])
for i in range(5, 10):
lst2.append(tpl[i])
print(lst1)
print(lst2)
# **OR**
# In[3]:
"""
Solution by: CoffeeBrakeInc
"""
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
lt = int(len(tup) / 2)
print(tup[:lt], tup[lt:])
# ---
#
# **OR**
# In[4]:
"""
Solution by: AasaiAlangaram
"""
tp = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print("The Original Tuple:", tp)
[
print("Splitted List :{List}".format(List=tp[x : x + 5]))
for x in range(0, len(tp), 5)
]
# ---
#
# # Question 39
#
# ### **Question:**
#
# > **_Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10)._**
#
# ---
#
# ### Hints:
#
# > **_Use "for" to iterate the tuple. Use tuple() to generate a tuple from a list._**
#
# ---
#
#
#
# **Solutions:**
# In[5]:
tpl = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tpl1 = tuple(i for i in tpl if i % 2 == 0)
print(tpl1)
# **OR**
# In[6]:
tpl = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tpl1 = tuple(
filter(lambda x: x % 2 == 0, tpl)
) # Lambda function returns True if found even element.
# Filter removes data for which function returns False
print(tpl1)
# ---
#
# # Question 40
#
# ### **Question:**
#
# > **_Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No"._**
#
# ---
#
# ### Hints:
#
# > **_Use if statement to judge condition._**
#
# ---
#
#
#
# **Solution: Python 3**
# In[7]:
"""
Solution by: Seawolf159
"""
text = input("Please type something. --> ")
if text == "yes" or text == "YES" or text == "Yes":
print("Yes")
else:
print("No")
# ---
# In[8]:
"""
Solution by: AasaiAlangaram
"""
input = input("Enter string:")
output = "".join(
["Yes" if input == "yes" or input == "YES" or input == "Yes" else "No"]
)
print(str(output))
# ---
#
# # Question 41
#
# ### **Question:**
#
# > **_Write a program which can map() to make a list whose elements are square of elements in [1,2,3,4,5,6,7,8,9,10]._**
#
# ---
#
# ### Hints:
#
# > **_Use map() to generate a list.Use lambda to define anonymous functions._**
#
# ---
#
#
#
# **Solutions:**
# In[9]:
# No different way of code is written as the requirment is specificly mentioned in problem description
li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squaredNumbers = map(lambda x: x ** 2, li) # returns map type object data
print(list(squaredNumbers)) # converting the object into list
# ---
#
# # Question 42
#
# ### **Question:**
#
# > **_Write a program which can map() and filter() to make a list whose elements are square of even number in [1,2,3,4,5,6,7,8,9,10]._**
#
# ---
#
# ### Hints:
#
# > **_Use map() to generate a list.Use filter() to filter elements of a list.Use lambda to define anonymous functions._**
#
# ---
#
#
#
# **Solutions:**
# In[10]:
def even(x):
return x % 2 == 0
def squer(x):
return x * x
li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
li = map(
squer, filter(even, li)
) # first filters number by even number and the apply map() on the resultant elements
print(list(li))
# ---
#
# # Question 43
#
# ### **Question:**
#
# > **_Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included)._**
#
# ---
#
# ### Hints:
#
# > **_Use filter() to filter elements of a list.Use lambda to define anonymous functions._**
#
# ---
#
#
#
# **Solutions:**
# In[11]:
def even(x):
return x % 2 == 0
evenNumbers = filter(even, range(1, 21))
print(list(evenNumbers))
# ---