Рассмотрим список (list = []). Вы можете выполнить следующие команды:
insert i e: Insert integer e at position .
print: Print the list.
remove e: Delete the first occurrence of integer e.
append e: Insert integer e at the end of the list.
sort: Sort the list.
pop: Pop the last element from the list.
reverse: Reverse the list.
Инициализируйте ваш список и прочитайте значение, за которым следуют строки команд, где каждая команда будет одного из перечисленных выше типов. Выполните итерацию каждой команды по порядку и выполните соответствующую операцию в вашем списке.
Пример ввода:
12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print
Мой код:
import sys
if __name__ == '__main__':
N = int(input())
my_list = []
inputs = []
for line in sys.stdin:
inputs.append(line)
for item in inputs:
if item[0:5] == 'print':
print(my_list)
elif item[0:2] == 'in':
inserts = [s for s in item.split()][1:3]
inserts = list(map(int, inserts))
my_list.insert(inserts[0], inserts[1])
elif item[0:3] == 'rem':
inserts = list(map(int, [s for s in item.split()][1]))
my_list.remove(inserts[0])
elif item[0:2] == 'ap':
inserts = list(map(int, [s for s in item.split()][1]))
my_list.append(inserts[0])
elif item[0:4] == 'sort':
my_list.sort()
elif item[0:3] == 'pop':
my_list.pop()
elif item[0:7] == 'reverse':
my_list.reverse()
Я не уверен, почему мой код не подтверждается после отправки. В этом тестовом случае они предоставили, мой код проходит.
Ожидаемый результат следующий:
[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
Большое спасибо за помощь!