Skip to content

Commit 9e68ff9

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

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

Level1/Ex_2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Write a program which can compute the factorial of a given numbers.
3+
The results should be printed in a comma-separated sequence on a single line.
4+
Suppose the following input is supplied to the program:
5+
8
6+
Then, the output should be:
7+
40320
8+
"""
9+
10+
def factorial(x):
11+
if x==0:
12+
return 1
13+
return x*factorial(x-1)
14+
15+
16+
x=int(input())
17+
print(factorial(x))
18+
19+
20+
21+
"""
22+
Output :
23+
24+
8
25+
=8*7*6*5*4*3*2*1 = 40320
26+
27+
10
28+
=10*9*8*7*6*5*4*3*2*1 = 3628800
29+
"""

Level1/Ex_3.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
3+
With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.
4+
Suppose the following input is supplied to the program:
5+
8
6+
Then, the output should be:
7+
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
8+
9+
10+
"""
11+
12+
n=int(input())
13+
d= dict()
14+
15+
for i in range(1,n+1):
16+
d[i]=i*i
17+
18+
print(d)
19+
20+
21+
"""
22+
Output :
23+
24+
10
25+
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
26+
27+
"""

Level1/Ex_4.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
3+
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.
4+
Suppose the following input is supplied to the program:
5+
34,67,55,33,12,98
6+
Then, the output should be:
7+
['34', '67', '55', '33', '12', '98']
8+
('34', '67', '55', '33', '12', '98')
9+
10+
"""
11+
12+
13+
raw_values = (input())
14+
15+
l = raw_values.split(',')
16+
17+
t= tuple(l)
18+
19+
print(t)
20+
21+
print(l)
22+
23+
"""
24+
Output :
25+
26+
34,22,33,44,44,55,66,33,65,78
27+
('34', '22', '33', '44', '44', '55', '66', '33', '65', '78')
28+
['34', '22', '33', '44', '44', '55', '66', '33', '65', '78']
29+
30+
31+
"""

Level1/Ex_5.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
3+
Question:
4+
Define a class which has at least two methods:
5+
getString: to get a string from console input
6+
printString: to print the string in upper case.
7+
Also please include simple test function to test the class methods.
8+
9+
"""
10+
11+
class InputOutString(object):
12+
def __init__(self):
13+
self.s=''
14+
15+
def getString(self):
16+
self.s=input()
17+
18+
19+
def printString(self):
20+
print(self.s.upper())
21+
22+
23+
strObj = InputOutString()
24+
strObj.getString()
25+
strObj.printString()
26+
27+
"""
28+
Output :
29+
30+
lionel messi is god
31+
LIONEL MESSI IS GOD
32+
33+
"""

0 commit comments

Comments
 (0)