Skip to content

Commit d7d087a

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

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

Level2/Ex_5.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
3+
Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all
4+
duplicate words and sorting them alphanumerically.
5+
Suppose the following input is supplied to the program:
6+
hello world and practice makes perfect and hello world again
7+
Then, the output should be:
8+
again and hello makes perfect practice world
9+
10+
"""
11+
12+
string_input = input()
13+
words =[word for word in string_input.split(" ")]
14+
print(" ".join(sorted(list(set(words)))))
15+
16+
17+
"""
18+
Let's break it down now
19+
20+
21+
print(set(words))
22+
23+
24+
This will print a set of the words, with all the unique values
25+
26+
print(list(set(words)))
27+
28+
29+
Create a list out of the values of words
30+
31+
print(sorted(list(set(words))))
32+
33+
34+
This will sort the list
35+
36+
print(" ".join(sorted(list(set(words)))))
37+
38+
39+
This is join the sorted list items with a whitespace
40+
41+
42+
For this input :
43+
44+
I like to yawn and I also like to make a music and a car
45+
46+
Now output will be :
47+
48+
I a also and car like make music to yawn
49+
50+
Notice that the uppercase I is sorted at first position
51+
"""
52+

Level2/Ex_6.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
3+
Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check
4+
whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a
5+
comma separated sequence.
6+
7+
Example:
8+
0100,0011,1010,1001
9+
Then the output should be:
10+
1010
11+
"""
12+
string_val =input()
13+
val =[]
14+
15+
items =[x for x in string_val.split(',')]
16+
17+
for p in items:
18+
div5 = int(p,2) #converts the binary to a decimal
19+
if not div5%5:
20+
val.append(p)
21+
22+
23+
print(','.join(val))

Level2/Ex_7.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
3+
Question:
4+
Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each
5+
digit of the number is an even number.
6+
The numbers obtained should be printed in a comma-separated sequence on a single line.
7+
8+
"""
9+
10+
values =[]
11+
for i in range(1000,3001):
12+
string_s = str(i)
13+
if (int(string_s[0]) % 2 == 0) and (int(string_s[1]) % 2 == 0) \
14+
and (int(string_s[2]) % 2 == 0) and (int(string_s[3]) % 2 == 0):
15+
values.append(string_s)
16+
17+
print(','.join(values))
18+
19+
"""
20+
Output :
21+
2000,2002,2004,2006,2008,2020,2022,2024,2026,2028,2040,2042,2044,2046,2048,2060,2062,2064,
22+
2066,2068,2080,2082,2084,2086,2088,2200,2202,2204,2206,2208,2220,2222,2224,2226,2228,2240,
23+
2242,2244,2246,2248,2260,2262,2264,2266,2268,2280,2282,2284,2286,2288,2400,2402,2404,2406,
24+
2408,2420,2422,2424,2426,2428,2440,2442,2444,2446,2448,2460,2462,2464,2466,2468,2480,2482,
25+
2484,2486,2488,2600,2602,2604,2606,2608,2620,2622,2624,2626,2628,2640,2642,2644,2646,2648,
26+
2660,2662,2664,2666,2668,2680,2682,2684,2686,2688,2800,2802,2804,2806,2808,2820,2822,2824,
27+
2826,2828,2840,2842,2844,2846,2848,2860,2862,2864,2866,2868,2880,2882,2884,2886,2888
28+
"""

Level2/Ex_8.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
3+
Write a program that accepts a sentence and calculate the number of letters and digits.
4+
Suppose the following input is supplied to the program:
5+
hello world! 123
6+
Then, the output should be:
7+
LETTERS 10
8+
DIGITS 3
9+
10+
"""
11+
12+
input_string = input()
13+
dict_d ={'DIGITS':0,'LETTERS':0}
14+
15+
for items in input_string:
16+
if items.isdigit():
17+
dict_d['DIGITS']+=1
18+
elif items.isalpha():
19+
dict_d['LETTERS']+=1
20+
else:
21+
pass
22+
23+
print('LETTERS are ',dict_d['LETTERS'])
24+
print('DIGITS are ', dict_d['DIGITS'])
25+
26+
27+
"""
28+
29+
Harambe is dead. Please shout 12#4
30+
31+
Output is :
32+
LETTERS are 24
33+
DIGITS are 3
34+
35+
"""
36+
37+

0 commit comments

Comments
 (0)