Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def palindrome(slovo:str):
- a = slovo[::-1]
- if slovo == a:
- p = 1
- else:
- p = 0
- return p
- def addition_upto_palindrome(line: str) -> int:
- if len(line) == 1:
- ans = 0
- elif palindrome(line) == 1:
- ans = 0
- else:
- right_ind = []
- i = 2
- while i <= len(line):
- if palindrome(line[:i]) == 1:
- right_ind.append(i)
- i += 1
- else:
- i +=1
- left_ind = []
- j = len(line) - 2
- while j >= 0:
- if palindrome(line[j:]) == 1:
- left_ind.append(j)
- j -= 1
- else:
- j -= 1
- if len(right_ind) != 0 and len(left_ind)!= 0 and len(line[:right_ind[-1]]) > len(line[left_ind[-1]:]):
- remein = len(line[:right_ind[-1]])
- elif len(right_ind) != 0 and len(left_ind)!= 0 and len(line[:right_ind[-1]]) < len(line[left_ind[-1]:]):
- remein = len(line[left_ind[-1]:])
- elif len(right_ind) == 0 and len(left_ind)!= 0:
- remein = len(line[left_ind[-1]:])
- elif len(right_ind) != 0 and len(left_ind)== 0:
- remein = len(line[:right_ind[-1]])
- elif len(right_ind) == 0 and len(left_ind) == 0 and len(line):
- remein = 1
- ans = len(line) - remein
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement