-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day_10.py
272 lines (202 loc) · 4.66 KB
/
Day_10.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
#!/usr/bin/env python
# coding: utf-8
# # Question 31
#
# ### **Question:**
#
# > **_Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys._**
#
# ---
#
# ### Hints:
#
#
# Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.
#
#
# ---
#
#
#
# **Solutions:**
# In[ ]:
def printDict():
dict = {i: i ** 2 for i in range(1, 21)} # Using comprehension method and
print(dict)
printDict()
# ---
#
# # Question 32
#
# ### **Question:**
#
# > **_Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only._**
#
# ---
#
# ### Hints:
#
#
# Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
#
#
# ---
#
#
#
# **Solutions:**
# In[1]:
def printDict():
dict = {i: i ** 2 for i in range(1, 21)}
print(dict.keys()) # print keys of a dictionary
printDict()
# ---
#
# # Question 33
#
# ### **Question:**
#
# > **_Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included)._**
#
# ---
#
# ### Hints:
#
#
# Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.
#
#
# ---
#
#
#
# **Solutions:**
# In[2]:
def printList():
lst = [i ** 2 for i in range(1, 21)]
print(lst)
printList()
# ---
#
# # Question 34
#
# ### **Question:**
#
# > **_Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list._**
#
# ---
#
# ### Hints:
#
#
# Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list
#
#
# ---
#
#
#
# **Solutions:**
# In[3]:
def printList():
lst = [i ** 2 for i in range(1, 21)]
for i in range(5):
print(lst[i])
printList()
# ---
# In[3]:
"""Solution by: yuan1z && edited by: apurvmishra99"""
func = lambda: ([i ** 2 for i in range(1, 21)][:5])
print(*(func()), sep="\n")
# ---
#
# # Question 35
#
# ### **Question:**
#
# > **_Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list._**
#
# ---
#
# ### Hints:
#
#
# Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list
#
#
# ---
#
#
#
# **Solutions:**
# In[4]:
def printList():
lst = [i ** 2 for i in range(1, 21)]
for i in range(19, 14, -1):
print(lst[i])
printList()
# ---
#
# # Question 36
#
# ### **Question:**
#
# > **_Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list._**
#
# ---
#
#
# Hints: Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list
#
#
# ---
#
#
#
# **Solutions:**
# In[5]:
def printList():
lst = [i ** 2 for i in range(1, 21)]
for i in range(5, 20):
print(lst[i])
printList()
# ---
#
# # Question 37
#
# ### **Question:**
#
# > **_Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included)._**
#
# ---
#
# ### Hints:
#
#
# Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use tuple() to get a tuple from a list.
#
#
# ---
#
#
#
# **Solutions:**
# In[6]:
def printTupple():
lst = [i ** 2 for i in range(1, 21)]
print(tuple(lst))
printTupple()
# ---
# In[7]:
"""
Solution by: Seawolf159
"""
def square_of_numbers():
return tuple(i ** 2 for i in range(1, 21))
print(square_of_numbers())
# ---
#
# ### Comment
#
# **_Problems of this section is very much easy and all of those are of a modification of same type problem which mainly focused on using some commonly used function works with list,dictionary, tupple.In my entire solutions, I havn't tried to solve problems in efficient way.Rather I tried to solve in a different way that I can.This will help a beginner to know how simplest problems can be solved in different ways._**
# In[ ]: