Учитывая, что 8 букв можно переставить примерно 40 тысячами можно просто запустить поиск в ширину, сохранить для всех перестановок то, из какой строчки они получились, и потом восстановить ответ для строчки abcdefgh.
while not to_process.empty(): s, prev = to_process.get() if s in prec: continue for i in range(7): for j in range(i + 1, 8): if i == 0: next_s = s[j::-1] + s[j+1:] else: next_s = s[:i] + s[j:i-1:-1] + s[j+1:] if next_s not in prec: to_process.put((next_s, s)) prec[s] = prev
current = "abcdefgh" print(current) while prec[current] is not None: current = prec[current] print(current)
Учитывая, что 8 букв можно переставить примерно 40 тысячами можно просто запустить поиск в ширину, сохранить для всех перестановок то, из какой строчки они получились, и потом восстановить ответ для строчки abcdefgh.
while not to_process.empty(): s, prev = to_process.get() if s in prec: continue for i in range(7): for j in range(i + 1, 8): if i == 0: next_s = s[j::-1] + s[j+1:] else: next_s = s[:i] + s[j:i-1:-1] + s[j+1:] if next_s not in prec: to_process.put((next_s, s)) prec[s] = prev
current = "abcdefgh" print(current) while prec[current] is not None: current = prec[current] print(current)
2)
number_list = []
for number in range(10):
number_list.append(int(input()))
second_list = []
for n in number_list:
if n % 3 == 0:
second_list.append(n)
amount = 0
for num in second_list:
amount += num
print(amount)
3)
number_list = []
for number in range(10):
number_list.append(int(input()))
second_list = []
for n in number_list:
if n % 2 != 0:
second_list.append(n)
multiplication = 1
for num in second_list:
multiplication *= num
print(multiplication)