Skip to content

Instantly share code, notes, and snippets.

@btseytlin
Created September 5, 2017 17:17
Show Gist options
  • Save btseytlin/637415414e8c55831dda9a3b115cee6c to your computer and use it in GitHub Desktop.
Save btseytlin/637415414e8c55831dda9a3b115cee6c to your computer and use it in GitHub Desktop.
def get_palindrome_bounds(text, palindrome_index):
left = palindrome_index
right = palindrome_index+1
while (text[left] == text[right]): #move outwards until we reach the end/start of string or the string between left and right stops being a palindrome
if left -1 >= 0 and right+1 < len(text) and text[left-1] == text[right+1]:
left = left-1
right = right + 1
else:
break
return left, right
def remove_even_palindromes(text):
if not text or len(text) < 2:
return text
left = 0
right = 1
out_text = ''
while right < len(text): #Find adjacent pair, get palindrome that contains it, add everything before it to out, proceed after the palindrome
if text[right-1] == text[right]: #Found a pair
palindrome_left_bound, palindrome_right_bound = get_palindrome_bounds(text, right-1)
out_text+= text[left:palindrome_left_bound]
left = palindrome_right_bound+1
right = left
right+=1
out_text+= text[left:]
return out_text
assert remove_even_palindromes('ww') == ''
assert remove_even_palindromes('www') == 'w'
assert remove_even_palindromes('ztwwst') == 'ztst'
assert remove_even_palindromes('wwsttsww') == ''
assert remove_even_palindromes('wwstdaadi') == 'sti'
assert remove_even_palindromes('wwstdaadierfflitzzz') == 'stierlitz'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment