Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. def palindrome(slovo:str):
  2. a = slovo[::-1]
  3. if slovo == a:
  4. p = 1
  5. else:
  6. p = 0
  7. return p
  8.  
  9.  
  10. def addition_upto_palindrome(line: str) -> int:
  11. if len(line) == 1:
  12. ans = 0
  13. elif palindrome(line) == 1:
  14. ans = 0
  15. else:
  16. right_ind = []
  17. i = 2
  18. while i <= len(line):
  19. if palindrome(line[:i]) == 1:
  20. right_ind.append(i)
  21. i += 1
  22. else:
  23. i +=1
  24.  
  25. left_ind = []
  26. j = len(line) - 2
  27. while j >= 0:
  28. if palindrome(line[j:]) == 1:
  29. left_ind.append(j)
  30. j -= 1
  31. else:
  32. j -= 1
  33.  
  34. if len(right_ind) != 0 and len(left_ind)!= 0 and len(line[:right_ind[-1]]) > len(line[left_ind[-1]:]):
  35. remein = len(line[:right_ind[-1]])
  36. elif len(right_ind) != 0 and len(left_ind)!= 0 and len(line[:right_ind[-1]]) < len(line[left_ind[-1]:]):
  37. remein = len(line[left_ind[-1]:])
  38. elif len(right_ind) == 0 and len(left_ind)!= 0:
  39. remein = len(line[left_ind[-1]:])
  40. elif len(right_ind) != 0 and len(left_ind)== 0:
  41. remein = len(line[:right_ind[-1]])
  42. elif len(right_ind) == 0 and len(left_ind) == 0 and len(line):
  43. remein = 1
  44.  
  45. ans = len(line) - remein
  46. return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement