Question 1

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
from random import seed, randint
import sys


def f(arg_for_seed, nb_of_elements, max_element):
'''
>>> f(0, 0, 10)
Here is L: []
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[]
>>> f(0, 1, 10)
Here is L: [6]
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[[6]]
>>> f(0, 2, 10)
Here is L: [6, 6]
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[[6]]
>>> f(0, 3, 10)
Here is L: [6, 6, 0]
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[[6], [0]]
>>> f(0, 4, 10)
Here is L: [6, 6, 0, 4]
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[[6], [0, 4]]
>>> f(0, 5, 10)
Here is L: [6, 6, 0, 4, 8]
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[[6], [0, 4, 8]]
>>> f(0, 6, 10)
Here is L: [6, 6, 0, 4, 8, 7]
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[[6], [0, 4, 8], [7]]
>>> f(0, 7, 10)
Here is L: [6, 6, 0, 4, 8, 7, 6]
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[[6], [0, 4, 8], [7], [6]]
>>> f(3, 10, 6)
Here is L: [1, 4, 4, 1, 2, 4, 3, 5, 4, 0]
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[[1, 4], [1, 2, 4], [3, 5], [4], [0]]
>>> f(3, 15, 8)
Here is L: [3, 8, 2, 5, 7, 1, 0, 7, 4, 8, 3, 3, 7, 8, 8]
The decomposition of L into increasing sequences,
with consecutive duplicates removed, is:
[[3, 8], [2, 5, 7], [1], [0, 7], [4, 8], [3, 7, 8]]
'''
if nb_of_elements < 0:
sys.exit()
seed(arg_for_seed)
L = [randint(0, max_element) for _ in range(nb_of_elements)]
print('Here is L:', L)
R = []
# INSERT YOUR CODE HERE

R = []
tmp = [L[0]]

for index in range(1, len(L)):

if L[index] > L[index - 1]:
tmp.append(L[index])
elif L[index] < L[index - 1]:
R.append(tmp)
tmp = [L[index]]
else:
continue

R.append(tmp)
print('The decomposition of L into increasing sequences,')
print(' with consecutive duplicates removed, is:\n ', R)

Question 2

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
# Will be tested with height a strictly positive integer.


def f(height):
'''
>>> f(1)
0
>>> f(2)
0
123
>>> f(3)
0
123
45678
>>> f(4)
0
123
45678
9012345
>>> f(5)
0
123
45678
9012345
678901234
>>> f(6)
0
123
45678
9012345
678901234
56789012345
>>> f(20)
0
123
45678
9012345
678901234
56789012345
6789012345678
901234567890123
45678901234567890
1234567890123456789
012345678901234567890
12345678901234567890123
4567890123456789012345678
901234567890123456789012345
67890123456789012345678901234
5678901234567890123456789012345
678901234567890123456789012345678
90123456789012345678901234567890123
4567890123456789012345678901234567890
123456789012345678901234567890123456789
'''
# INSERT YOUR CODE HERE
tmp = []
start_from_here = 0
cnt = 0
number_list = [i for i in range(10)]
for h in range(1, height + 1):
for i in range(start_from_here, h ** 2):
tmp.append(number_list[i % 10])
cnt += 1
print(' ' * (height - h) + ''.join(str(x) for x in tmp))
start_from_here = cnt
tmp = []

Question 3

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
# On each line of the form "x plus y equals z", x, y and z are
# possibly padded with 0s on the left; in particular, the number of
# digits in both x and y is the maximum of the number of digits in
# the first input, m, and the number of digits in the second input, n.
#
# The third argument, direction, is one of 'L' or 'R',
# for left or right rotation.


def f(m, n, direction):
'''
>>> f(0, 0, 'L')
0 plus 0 equals 0
>>> f(0, 0, 'R')
0 plus 0 equals 0
>>> f(1234, 0, 'L')
1234 plus 0000 equals 1234
2341 plus 0000 equals 2341
3412 plus 0000 equals 3412
4123 plus 0000 equals 4123
>>> f(1234, 0, 'R')
1234 plus 0000 equals 1234
4123 plus 0000 equals 4123
3412 plus 0000 equals 3412
2341 plus 0000 equals 2341
>>> f(2134, 3412, 'L')
2134 plus 3412 equals 5546
1342 plus 4123 equals 5465
3421 plus 1234 equals 4655
4213 plus 2341 equals 6554
>>> f(2134, 3412, 'R')
2134 plus 3412 equals 5546
4213 plus 2341 equals 6554
3421 plus 1234 equals 4655
1342 plus 4123 equals 5465
>>> f(213287, 3166, 'L')
213287 plus 003166 equals 0216453
132872 plus 031660 equals 0164532
328721 plus 316600 equals 0645321
287213 plus 166003 equals 0453216
872132 plus 660031 equals 1532163
721328 plus 600316 equals 1321644
>>> f(8901, 3419306, 'R')
0008901 plus 3419306 equals 03428207
1000890 plus 6341930 equals 07342820
0100089 plus 0634193 equals 00734282
9010008 plus 3063419 equals 12073427
8901000 plus 9306341 equals 18207341
0890100 plus 1930634 equals 02820734
0089010 plus 4193063 equals 04282073
>>> f(800095, 900003, 'L')
800095 plus 900003 equals 1700098
000958 plus 000039 equals 0000997
009580 plus 000390 equals 0009970
095800 plus 003900 equals 0099700
958000 plus 039000 equals 0997000
580009 plus 390000 equals 0970009
'''
# INSERT YOUR CODE HERE
max_length = max(len(str(m)), len(str(n)))
m, n = str(m).rjust(int(max_length), '0'), str(n).rjust(int(max_length), '0')

def move_display(m, n, direction):
str_m = str(m)
str_n = str(n)
for i in range(len(str_m)):
if direction == 'L':
print(str(str_m)[i:] + str_m[:i] + ' plus ' + str(str_n)[i:] + str_n[:i] + ' equals ' + plus(
str(str_m)[i:] + str_m[:i],
str(str_n)[i:] + str_n[:i]))

elif direction == 'R':
print(str(str_m)[-i:] + str_m[:-i] + ' plus ' + str(str_n)[-i:] + str_n[:-i] + ' equals ' + plus(
str(str_m)[-i:] + str_m[:-i],
str(str_n)[-i:] + str_n[:-i]))

def plus(m: str, n: str) -> str:
res = str(int(m) + int(n))
if len(m) > 4:
res = res.rjust(len(m) + 1, '0')
return res

move_display(m, n, direction)

Question 5

cpiai.csv

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
# Will be tested with year between 1913 and 2013.


import csv


def f(year):
'''
>>> f(1914)
In 1914, maximum inflation was: 2.0
It was achieved in the following mth: Aug
>>> f(1922)
In 1922, maximum inflation was: 0.6
It was achieved in the following mth: Jul, Oct, Nov, Dec
>>> f(1995)
In 1995, maximum inflation was: 0.4
It was achieved in the following mth: Jan, Feb
>>> f(2013)
In 2013, maximum inflation was: 0.82
It was achieved in the following mth: Feb
'''
months = 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', \
'Sep', 'Oct', 'Nov', 'Dec'
date_mapping = {}
months_mapping = {}
# INSERT YOUR CODE HERE
for i in range(len(months)):
months_mapping[i + 1] = months[i]
print(months_mapping)
target_year = year
print(target_year)
with open('cpiai.csv') as file:
for line in file:
if not line or line.startswith('D'):
continue
year_month_date, index, inflation = line.split(',')
year, month, date = year_month_date.split('-')
date_mapping[year, month] = float(inflation)
max_inflation = 0.0
max_inflation_mth = 0

for date_info, inflation in date_mapping.items():
year, mth = date_info
if int(year) == int(target_year):
# print(year, mth)
if inflation > max_inflation:
max_inflation = inflation
max_inflation_mth = months_mapping[int(mth)]

print(f'In {target_year}, maximum inflation was: {max_inflation}')
print(f'It was achieved in the following months: {max_inflation_mth}')