-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.py
29 lines (23 loc) · 1018 Bytes
/
palindrome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from log import Log
from sentence import Sentence
class Palindrome:
cache = {}
def __init__(self, text):
self.sentence = Sentence(text)
self.ordinary_index = 0
self.opposite_index = self.sentence.length - 1
self.half_way_index = round(self.opposite_index / 2)
def _increment_and_decrement_indexes(self):
self.ordinary_index += 1
self.opposite_index -= 1
def _is_sentence_symmetrical(self):
while self.ordinary_index < self.half_way_index:
if not self.sentence.characters_are_equal(self.ordinary_index, self.opposite_index):
return False
self._increment_and_decrement_indexes();
return True
def is_valid(self):
Log.stdout('Checking if "%s" is a palindrome.' % self.sentence.text)
if self.sentence.text not in self.cache:
self.cache[self.sentence.text] = self.sentence.is_valid() and self._is_sentence_symmetrical()
return self.cache[self.sentence.text]