Как заменить многочисленные if на словарь, где ключ это имя метода, а значение это сам метод {'push_back': object.push_back,}?
def main():
count_command = int(input())
queue_length = int(input())
queue = Deck(queue_length)
for i in range(count_command):
command = input()
operation, *value = command.split()
if value:
if operation == 'push_back':
a = queue.push_back(int(*value))
if a is not None:
print(a)
if operation == 'push_front':
a = queue.push_front(int(*value))
if a is not None:
print(a)
else:
if operation == 'pop_front':
print(queue.pop_front())
if operation == 'pop_back':
print(queue.pop_back())
Ответы (1 шт):
Автор решения: n1tr0xs
→ Ссылка
Как-то так:
def main():
count_command = int(input())
queue_length = int(input())
queue = Deck(queue_length)
operation_dict = {
'push_back': queue.push_back,
'push_front': queue.push_front,
'pop_front': lambda: print(queue.pop_front()),
'pop_back': lambda: print(queue.pop_back()),
}
for i in range(count_command):
command = input()
operation, *value = command.split()
if value:
a = operation_dict[operation](int(*value))
if a is not None:
print(a)
else:
operation_dict[operation]()