quiz_2_this_year

quiz_2.pdf

使用字典存储列表中元素索引位置。

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

try:
your_list = [int(x) for x in
input('Enter a permutation of 0, ..., n '
'for some n >= 0: '
).split()
]
if not your_list:
raise ValueError
your_list_as_set = set(your_list)
if len(your_list_as_set) != len(your_list) \
or your_list_as_set != set(range(len(your_list))):
raise ValueError
except ValueError:
print('Incorrect input, giving up.')
sys.exit()
try:
for_seed, length = \
(int(x) for x in input('Enter two integers, '
'the second one between 0 and 10: '
).split()
)
if not 0 <= length <= 10:
raise ValueError
except ValueError:
print('Incorrect input, giving up.')
sys.exit()
seed(for_seed)
my_list = list(range(length))
shuffle(my_list)
print('Here is your list:')
print(' ', your_list)
print('Here is my list:')
print(' ', my_list)

# INSERT THE FIRST PART OF YOUR CODE HERE

def removeMinMax(your_list):
while your_list and (
your_list[0] == min(your_list) or your_list[0] == max(your_list) or your_list[-1] == min(your_list) or
your_list[-1] == max(your_list)):
min_value = min(your_list)
max_value = max(your_list)
if min_value == your_list[-1] or max_value == your_list[-1]:
your_list.pop()
elif min_value == your_list[0] or max_value == your_list[0]:
your_list.pop(0)

removeMinMax(your_list)

print()
print('Removing again and again the currently largest\n'
'or smallest element in your list for as long as\n'
'it currently starts or ends the list, we get:'
)
print(your_list)
print()
print("That's how to travel in my list:")

# INSERT THE SECOND PART OF YOUR CODE HERE

dict_space = {}
last_advent = 0

for cur in sorted(my_list):
dict_space[int(cur)] = 2 * my_list.index(int(cur))

tmp = my_list
while tmp:
cur = str(min(my_list))
if last_advent > dict_space[int(cur)]:
print(' ' * dict_space[int(cur)] + cur + '-' * (last_advent - dict_space[int(cur)]))
elif last_advent < dict_space[int(cur)]:
if int(cur) == 0:
print(' ' * dict_space[int(cur)] + cur + '-' * (last_advent - dict_space[int(cur)]))
else:
print(' ' * last_advent + '-' * (dict_space[int(cur)] - last_advent) + cur)
last_advent = dict_space[int(cur)]
tmp.remove(int(cur))

使用列表生成式存储元素对应索引位置。

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
# INSERT THE SECOND PART OF YOUR CODE HERE
# indexed_list = [(int(cur), 2 * my_list.index(int(cur))) for cur in sorted(my_list)]
# Use list comprehension to generate the list of tuples based on my_list.
# Two elements in the tuple are the elements in my_list and the twofold index value of elements respectively.
# Sort the list based on the first element of the tuple 'element' in ascending order.
index_list = sorted([(element, 2 * index) for index, element in enumerate(my_list)], key=lambda x: x[0])

# define the last_advent index
last_advent = 0

while index_list:
# Pop the leftmost tuple from the ordered index_list each time.
# Unpack each tuple of index_list.
# The first one represents current element.
# The second one means it corresponding twofold index value.
cur, cur_space = index_list.pop(0)
# For concatenate string of cur below, convert its type from integer into string
cur = str(cur)
if int(cur) == 0:
print(' ' * cur_space + str(cur))
else:
# The number in the previous line is to the right of the number in this line
if last_advent >= cur_space:
print(' ' * cur_space + cur + '-' * (last_advent - cur_space))
# The number in the previous line is to the left of the number in this line
elif last_advent < cur_space:
print(' ' * last_advent + '-' * (cur_space - last_advent) + cur)

# last_advent index move forward
last_advent = cur_space