-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
39 lines (31 loc) · 796 Bytes
/
index.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
def isPalindrome(string):
even = len(string) % 2
if even == 0:
return isPalindromeEven(string)
if even > 0:
return isPalindromeOdd(string)
def isPalindromeEven(string):
array = list(string)
midIdx = (len(array) - 1) // 2
nextIdx = midIdx + 1
while midIdx >= 0 and nextIdx <= len(array) - 1:
if array[midIdx] == array[nextIdx]:
midIdx -= 1
nextIdx += 1
else:
return False
return True
def isPalindromeOdd(string):
array = list(string)
midIdx = (len(array) - 1) // 2
nextIdx = midIdx + 1
prevIdx = midIdx - 1
while prevIdx >= 0 and nextIdx <= len(array) - 1:
if array[prevIdx] == array[nextIdx]:
prevIdx -= 1
nextIdx += 1
else:
return False
return True
string = 'abfcba'
print(isPalindrome(string))