Skip to content

Commit f895473

Browse files
author
zac11
committed
Added Level 2 Excercises solutions
1 parent d7d087a commit f895473

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

Level2/Ex_10.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.
3+
Suppose the following input is supplied to the program:
4+
9
5+
Then, the output should be:
6+
11106
7+
"""
8+
9+
number = input()
10+
11+
n1 = int('%s'%number)
12+
print(n1)
13+
n2 = int('%s%s'%(number,number))
14+
print(n2)
15+
n3= int('%s%s%s'%(number,number,number))
16+
print(n3)
17+
n4= int('%s%s%s%s'%(number,number,number,number))
18+
print(n4)
19+
20+
print(n1+n2+n3+n4)
21+
22+
"""
23+
24+
9
25+
99
26+
999
27+
9999
28+
11106
29+
30+
"""

Level2/Ex_11.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.
3+
Suppose the following input is supplied to the program:
4+
1,2,3,4,5,6,7,8,9
5+
Then, the output should be:
6+
1,3,5,7,9
7+
8+
"""
9+
10+
values = input()
11+
numbers = [x for x in values.split(',') if int(x)%2!=0]
12+
print(numbers)
13+
print(' '.join(numbers))

Level2/Ex_12.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Question:
3+
Write a program that computes the net amount of a bank account based a transaction log from console input. The
4+
transaction log format is shown as following:
5+
D 100
6+
W 200
7+
¡­
8+
D means deposit while W means withdrawal.
9+
Suppose the following input is supplied to the program:
10+
D 300
11+
D 300
12+
W 200
13+
D 100
14+
Then, the output should be:
15+
500
16+
"""
17+
18+
import sys
19+
netAccountAmount=0
20+
while True:
21+
entry =input()
22+
if not entry:
23+
break
24+
values = entry.split(',')
25+
op = values[0]
26+
newAmount = int(values[1])
27+
if op =='D':
28+
netAccountAmount+=newAmount
29+
elif op =='W':
30+
netAccountAmount-=newAmount
31+
else:
32+
pass
33+
34+
print(netAccountAmount)

Level2/Ex_9.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 that accepts a sentence and calculate the number of upper case letters and lower case letters.
4+
Suppose the following input is supplied to the program:
5+
Hello world!
6+
Then, the output should be:
7+
UPPER CASE 1
8+
LOWER CASE 9
9+
10+
"""
11+
12+
input_string = input()
13+
dict_d ={'LOWECASE':0,'UPPERCASE':0}
14+
15+
for items in input_string:
16+
if items.isupper():
17+
dict_d['UPPERCASE']+=1
18+
elif items.islower():
19+
dict_d['LOWECASE']+=1
20+
else:
21+
pass
22+
23+
print('Lower case is ',dict_d['LOWECASE'])
24+
print('Upper case is ',dict_d['UPPERCASE'])
25+
26+
"""
27+
Harambe is not dead. He is always in our hearts
28+
Lower case is 35
29+
Upper case is 2
30+
31+
"""

0 commit comments

Comments
 (0)