-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpalindrome.py
40 lines (29 loc) · 832 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
30
31
32
33
34
35
36
37
38
39
40
# Related blog post - https://algoritmim.co.il/2020/02/03
def is_palindrome_v1(word):
word = str(word)
i = 0
j = len(word) - 1
while i < j:
if word[i] != word[j]:
return False
i += 1
j -= 1
return True
def _is_palindrome_recur(word):
if len(word) <= 1:
return True
if word[0] != word[-1]:
return False
return _is_palindrome_recur(word[1:-1])
def is_palindrome_v2(word):
return _is_palindrome_recur(str(word))
def run_tests():
for func in [is_palindrome_v1, is_palindrome_v2]:
assert func("a") is True
assert func("aba") is True
assert func("abba") is True
assert func("dog") is False
assert func(123) is False
assert func(123321) is True
if __name__ == '__main__':
run_tests()