fork download
  1. def solution(numbers):
  2. n = len(numbers)
  3.  
  4. for i in range(n):
  5. if numbers == numbers[::-1]:
  6. return i, numbers[n:]
  7.  
  8. numbers.insert(n, numbers[i])
  9.  
  10.  
  11. def solution_faster(numbers):
  12. for i, _ in enumerate(numbers):
  13. seq = numbers[i:]
  14.  
  15. if seq == seq[::-1]:
  16. return i, numbers[:i]
  17.  
  18.  
  19. def solution_fastest(numbers):
  20. n = len(numbers)
  21.  
  22. for i in range(n):
  23. if all(numbers[j] == numbers[i-j-1] for j in range(i, (i + n) // 2)):
  24. return i, numbers[:i]
  25.  
  26. tests = (
  27. [1, 2, 3, 4, 5, 4, 3, 2, 1],
  28. [1, 2, 1, 2, 2],
  29. [1, 2, 3, 4, 5],
  30. )
  31.  
  32. for test in tests:
  33. assert solution(test) == solution_faster(test)
  34. assert solution(test) == solution_fastest(test)
  35.  
  36. n, seq = solution(test)
  37. print(n, seq)
  38.  
Success #stdin #stdout 0.01s 9992KB
stdin
Standard input is empty
stdout
0 []
3 [1, 2, 1]
4 [4, 3, 2, 1]