def solution(numbers):
n = len(numbers)
for i in range(n):
if numbers == numbers[::-1]:
return i, numbers[n:]
numbers.insert(n, numbers[i])
def solution_faster(numbers):
for i, _ in enumerate(numbers):
seq = numbers[i:]
if seq == seq[::-1]:
return i, numbers[:i]
def solution_fastest(numbers):
n = len(numbers)
for i in range(n):
if all(numbers[j] == numbers[i-j-1] for j in range(i, (i + n) // 2)):
return i, numbers[:i]
tests = (
[1, 2, 3, 4, 5, 4, 3, 2, 1],
[1, 2, 1, 2, 2],
[1, 2, 3, 4, 5],
)
for test in tests:
assert solution(test) == solution_faster(test)
assert solution(test) == solution_fastest(test)
n, seq = solution(test)
print(n, seq)
ZGVmIHNvbHV0aW9uKG51bWJlcnMpOgogICAgbiA9IGxlbihudW1iZXJzKQoKICAgIGZvciBpIGluIHJhbmdlKG4pOgogICAgICAgIGlmIG51bWJlcnMgPT0gbnVtYmVyc1s6Oi0xXToKICAgICAgICAgICAgcmV0dXJuIGksIG51bWJlcnNbbjpdCgogICAgICAgIG51bWJlcnMuaW5zZXJ0KG4sIG51bWJlcnNbaV0pCgoKZGVmIHNvbHV0aW9uX2Zhc3RlcihudW1iZXJzKToKICAgIGZvciBpLCBfIGluIGVudW1lcmF0ZShudW1iZXJzKToKICAgICAgICBzZXEgPSBudW1iZXJzW2k6XQoKICAgICAgICBpZiBzZXEgPT0gc2VxWzo6LTFdOgogICAgICAgICAgICByZXR1cm4gaSwgbnVtYmVyc1s6aV0KCgpkZWYgc29sdXRpb25fZmFzdGVzdChudW1iZXJzKToKICAgIG4gPSBsZW4obnVtYmVycykKCiAgICBmb3IgaSBpbiByYW5nZShuKToKICAgICAgICBpZiBhbGwobnVtYmVyc1tqXSA9PSBudW1iZXJzW2ktai0xXSBmb3IgaiBpbiByYW5nZShpLCAoaSArIG4pIC8vIDIpKToKICAgICAgICAgICAgcmV0dXJuIGksIG51bWJlcnNbOmldCgp0ZXN0cyA9ICgKCVsxLCAyLCAzLCA0LCA1LCA0LCAzLCAyLCAxXSwKCVsxLCAyLCAxLCAyLCAyXSwKCVsxLCAyLCAzLCA0LCA1XSwKKQoKZm9yIHRlc3QgaW4gdGVzdHM6Cglhc3NlcnQgc29sdXRpb24odGVzdCkgPT0gc29sdXRpb25fZmFzdGVyKHRlc3QpCglhc3NlcnQgc29sdXRpb24odGVzdCkgPT0gc29sdXRpb25fZmFzdGVzdCh0ZXN0KQoKCW4sIHNlcSA9IHNvbHV0aW9uKHRlc3QpCglwcmludChuLCBzZXEpCg==