Skip to content

Commit d8947fb

Browse files
author
zac11
committed
Added Level 2 Excercises solutions
1 parent 9e68ff9 commit d8947fb

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed

Level2/Ex_1.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
3+
Question:
4+
Write a program that calculates and prints the value according to the given formula:
5+
Q = Square root of [(2 * C * D)/H]
6+
Following are the fixed values of C and H:
7+
C is 50. H is 30.
8+
D is the variable whose values should be input to your program in a comma-separated sequence.
9+
Example
10+
Let us assume the following comma separated input sequence is given to the program:
11+
100,150,180
12+
The output of the program should be:
13+
18,22,24
14+
"""
15+
16+
import math
17+
18+
val =[]
19+
c = 50
20+
h = 30
21+
22+
items =[x for x in input().split(',')]
23+
for d in items:
24+
val.append(str(int(round(math.sqrt(2*c*float(d)/h)))))
25+
26+
print(','.join(val))
27+
28+
29+
"""
30+
Output :
31+
32+
100,150
33+
18,22
34+
"""

Level2/Ex_2.py

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
"""
2+
3+
Question:
4+
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.
5+
Note: i=0,1.., X-1; j=0,1,¡­Y-1.
6+
Example
7+
Suppose the following inputs are given to the program:
8+
3,5
9+
Then, the output of the program should be:
10+
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
11+
12+
"""
13+
14+
str_input = input()
15+
dimensions = [int(x) for x in str_input.split(',')]
16+
rowNum = dimensions[0]
17+
columnNum = dimensions[1]
18+
19+
multilist = [[0 for col in range(columnNum)] for row in range(rowNum)]
20+
21+
"""
22+
This creates a 4X6 dimensional array of 0's
23+
24+
0 0 0 0 0 0
25+
0 0 0 0 0 0
26+
0 0 0 0 0 0
27+
0 0 0 0 0 0
28+
29+
"""
30+
31+
print(multilist)
32+
33+
34+
for row in range(rowNum):
35+
for col in range(columnNum):
36+
multilist[row][col]= row*col
37+
38+
print(multilist)
39+
40+
"""
41+
42+
------------------Row 1----------------------
43+
44+
for row in range(0):
45+
for col in range(6):
46+
multilist[0][0]= 1*0 = 0
47+
48+
for row in range(0):
49+
for col in range(6):
50+
multilist[0][1]= 0*1 = 0
51+
52+
53+
for row in range(0):
54+
for col in range(6):
55+
multilist[0][2]= 0*2 = 0
56+
57+
58+
for row in range(0):
59+
for col in range(6):
60+
multilist[0][3]= 0*3 = 0
61+
62+
63+
for row in range(0):
64+
for col in range(6):
65+
multilist[0][4]= 0*4 = 0
66+
67+
68+
for row in range(0):
69+
for col in range(6):
70+
multilist[0][5]= 0*5 = 0
71+
72+
73+
for row in range(0):
74+
for col in range(6):
75+
multilist[0][6]= 0*6 = 0
76+
77+
------------------Row 2-------------------------
78+
79+
for row in range(1):
80+
for col in range(6):
81+
multilist[1][0]= 1*0 = 0
82+
83+
for row in range(1):
84+
for col in range(6):
85+
multilist[1][1]= 1*1 = 1
86+
87+
88+
for row in range(1):
89+
for col in range(6):
90+
multilist[1][2]= 1*2 = 2
91+
92+
93+
for row in range(1):
94+
for col in range(6):
95+
multilist[1][3]= 1*3 = 3
96+
97+
98+
99+
for row in range(1):
100+
for col in range(6):
101+
multilist[1][4]= 1*4 = 4
102+
103+
104+
for row in range(1):
105+
for col in range(6):
106+
multilist[1][5]= 1*5 = 5
107+
108+
109+
for row in range(1):
110+
for col in range(6):
111+
multilist[1][6]= 1*6 = 6
112+
113+
114+
------------------Row 3---------------------------
115+
116+
for row in range(2):
117+
for col in range(6):
118+
multilist[2][0]= 2*0 = 0
119+
120+
121+
for row in range(2):
122+
for col in range(6):
123+
multilist[2][1]= 2*1 = 2
124+
125+
126+
for row in range(2):
127+
for col in range(6):
128+
multilist[2][2]= 2*2 = 4
129+
130+
131+
for row in range(2):
132+
for col in range(6):
133+
multilist[2][3]= 2*3 = 6
134+
135+
136+
for row in range(2):
137+
for col in range(6):
138+
multilist[2][4]= 2*4 = 8
139+
140+
141+
for row in range(2):
142+
for col in range(6):
143+
multilist[2][5]= 2*5 = 10
144+
145+
146+
for row in range(2):
147+
for col in range(6):
148+
multilist[2][6]= 2*6 = 12
149+
150+
151+
152+
153+
------------------Row 4---------------------------------
154+
155+
for row in range(3):
156+
for col in range(6):
157+
multilist[3][0]= 3*0 = 0
158+
159+
160+
for row in range(3):
161+
for col in range(6):
162+
multilist[3][1]= 3*1 = 3
163+
164+
165+
for row in range(3):
166+
for col in range(6):
167+
multilist[3][2]= 3*2 = 6
168+
169+
170+
for row in range(3):
171+
for col in range(6):
172+
multilist[3][3]= 3*3 = 9
173+
174+
175+
for row in range(3):
176+
for col in range(6):
177+
multilist[3][4]= 3*4 = 12
178+
179+
180+
for row in range(3):
181+
for col in range(6):
182+
multilist[3][5]= 3*5 = 15
183+
184+
"""

Level2/Ex_3.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
3+
Question:
4+
Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence
5+
after sorting them alphabetically.
6+
Suppose the following input is supplied to the program:
7+
without,hello,bag,world
8+
Then, the output should be:
9+
bag,hello,without,world
10+
11+
"""
12+
enter_string = input()
13+
14+
items = [x for x in enter_string.split(',')]
15+
16+
items.sort()
17+
18+
print(','.join(items))
19+
20+
21+
"""
22+
Input : lionel, christiano, diego, aguero
23+
Output : aguero, christiano, diego,lionel
24+
25+
"""

Level2/Ex_4.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.
3+
Suppose the following input is supplied to the program:
4+
Hello world
5+
Practice makes perfect
6+
Then, the output should be:
7+
HELLO WORLD
8+
PRACTICE MAKES PERFECT
9+
10+
"""
11+
lines =[]
12+
input_string = input()
13+
while True:
14+
if input_string:
15+
lines.append(input_string.upper())
16+
else:
17+
break
18+
19+
for sent in lines:
20+
print(sent)
21+

0 commit comments

Comments
 (0)