-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDay_02.py
More file actions
370 lines (280 loc) · 6.27 KB
/
Day_02.py
File metadata and controls
370 lines (280 loc) · 6.27 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
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
#!/usr/bin/env python
# coding: utf-8
# # Question 4
#
# ### **Question:**
#
# > **_Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.Suppose the following input is supplied to the program:_**
#
#
# 34,67,55,33,12,98
#
#
# > **_Then, the output should be:_**
#
#
# ['34', '67', '55', '33', '12', '98']
# ('34', '67', '55', '33', '12', '98')
#
#
# ### Hints:
#
# > **_In case of input data being supplied to the question, it should be assumed to be a console input.tuple() method can convert list to tuple_**
#
# ---
#
#
#
# **Solutions:**
# In[1]:
lst = input().split(",")
# the input is being taken as string and as it is string it has a built in
# method name split. ',' inside split function does split where it finds any ','
# and save the input as list in lst variable
tpl = tuple(lst) # tuple method converts list to tuple
print(lst)
print(tpl)
# ---
#
# # Question 5
#
# ### **Question:**
#
# > **_Define a class which has at least two methods:_**
# >
# > - **_getString: to get a string from console input_**
# > - **_printString: to print the string in upper case._**
#
# > **_Also please include simple test function to test the class methods._**
#
# ### Hints:
#
# > **_Use **init** method to construct some parameters_**
#
# ---
#
#
#
# **Solutions:**
# In[2]:
class IOstring:
def __init__(self):
pass
def get_string(self):
self.s = input()
def print_string(self):
print(self.s.upper())
xx = IOstring()
xx.get_string()
xx.print_string()
# ---
#
# # Question 6
#
# ### **Question:**
#
# > **_Write a program that calculates and prints the value according to the given formula:_**
#
# > **_Q = Square root of [(2 _ C _ D)/H]_**
#
# > **_Following are the fixed values of C and H:_**
#
# > **_C is 50. H is 30._**
#
# > **_D is the variable whose values should be input to your program in a comma-separated sequence.For example
# > Let us assume the following comma separated input sequence is given to the program:_**
#
#
# 100,150,180
#
#
# > **_The output of the program should be:_**
#
#
# 18,22,24
#
#
# ---
#
# ### Hints:
#
# > **_If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26).In case of input data being supplied to the question, it should be assumed to be a console input._**
#
# ---
#
#
#
# **Solutions:**
# In[3]:
from math import sqrt # import specific functions as importing all using *
# is bad practice
C, H = 50, 30
def calc(D):
return sqrt((2 * C * D) / H)
D = [int(i) for i in input().split(",")] # splits in comma position and set up in list
D = [int(i) for i in D] # converts string to integer
D = [calc(i) for i in D] # returns floating value by calc method for every item in D
D = [round(i) for i in D] # All the floating values are rounded
D = [
str(i) for i in D
] # All the integers are converted to string to be able to apply join operation
print(",".join(D))
# **OR**
# In[4]:
from math import sqrt
C, H = 50, 30
def calc(D):
return sqrt((2 * C * D) / H)
D = input().split(",") # splits in comma position and set up in list
D = [
str(round(calc(int(i)))) for i in D
] # using comprehension method. It works in order of the previous code
print(",".join(D))
# **OR**
# In[5]:
from math import sqrt
C, H = 50, 30
def calc(D):
return sqrt((2 * C * D) / H)
print(",".join([str(int(calc(int(i)))) for i in input().split(",")]))
# **OR**
# In[6]:
from math import * # importing all math functions
C, H = 50, 30
def calc(D):
D = int(D)
return str(int(sqrt((2 * C * D) / H)))
D = input().split(",")
D = list(map(calc, D)) # applying calc function on D and storing as a list
print(",".join(D))
# ---
#
# # Question 7
#
# ### **Question:**
#
# > **_Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i _ j.\***
#
# > **_Note: i=0,1.., X-1; j=0,1,¡Y-1. Suppose the following inputs are given to the program: 3,5_**
#
# > **_Then, the output of the program should be:_**
#
#
# [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
#
#
# ---
#
# ### Hints:
#
# > **_Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form._**
#
# ---
#
#
#
# **Solutions:**
# In[7]:
x, y = map(int, input().split(","))
lst = []
for i in range(x):
tmp = []
for j in range(y):
tmp.append(i * j)
lst.append(tmp)
print(lst)
# **OR**
# In[8]:
x, y = map(int, input().split(","))
lst = [[i * j for j in range(y)] for i in range(x)]
print(lst)
# ---
#
# # Question 8
#
# ### **Question:**
#
# > **_Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically._**
#
# > **_Suppose the following input is supplied to the program:_**
#
#
# without,hello,bag,world
#
#
# > **_Then, the output should be:_**
#
#
# bag,hello,without,world
#
#
# ---
#
# ### Hints:
#
# > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
#
# ---
#
#
#
# **Solutions:**
# In[9]:
lst = input().split(",")
lst.sort()
print(",".join(lst))
# ---
#
# # Question 9
#
# ### **Question:**
#
# > **_Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized._**
#
# > **_Suppose the following input is supplied to the program:_**
#
#
# Hello world
#
# Practice makes perfect
#
#
# > **_Then, the output should be:_**
#
#
# HELLO WORLD
#
# PRACTICE MAKES PERFECT
#
#
# ---
#
# ### Hints:
#
# > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
#
# ---
#
#
#
# **Solutions:**
# In[ ]:
lst = []
while input():
x = input()
if len(x) == 0:
break
lst.append(x.upper())
for line in lst:
print(line)
# **OR**
# In[ ]:
def user_input():
while True:
s = input()
if not s:
return
yield s
for line in map(str.upper, user_input()):
print(line)
# ---