Created
March 23, 2018 14:53
-
-
Save novelview9/9e233bcad8d57f0f52d5013d66da79b6 to your computer and use it in GitHub Desktop.
myfil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
class PalindromeNode: | |
def __init__(self, sentence, exception_limit): | |
self.sentence = sentence | |
self.exception_limit = exception_limit | |
def is_done(self): | |
return len(self.sentence) <= 1 | |
def compare_side(self): | |
return self.sentence[0] == self.sentence[-1] | |
def peel_out(self): | |
self.sentence = self.sentence[1:-1] | |
def cut_both_side(self): | |
self.exception_limit -= 1 | |
if self.exception_limit == -1: | |
return [] | |
palindrome_cut_left = copy.copy(self) | |
palindrome_cut_left.sentence = self.sentence[1:] | |
palindrome_cut_right = copy.copy(self) | |
palindrome_cut_right.sentence = self.sentence[:-1] | |
return [palindrome_cut_left, palindrome_cut_right] | |
stack = [] | |
result = 'fail' | |
palindrome_node = PalindromeNode('wowxx', 2) | |
stack.append(palindrome_node) | |
while stack: | |
palindrome_node = stack.pop(0) | |
if palindrome_node.is_done(): | |
result = 'success' | |
break | |
if palindrome_node.compare_side(): | |
palindrome_node.peel_out() | |
stack.append(palindrome_node) | |
else: | |
output = palindrome_node.cut_both_side() | |
stack.extend(output) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment