-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome-checker.py
54 lines (44 loc) · 1.06 KB
/
palindrome-checker.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#palindrome checker, checks if a word is a palindrome or not
#What is a palindrom? Words that are spelt the same from both backwards and frontwards, example
#- 'kayak', 'noon'.
#Method 1: Slicing Operation
def palindromchecker1(s):
return s == s[::-1]
s = input("enter a word ")
ans = palindromchecker1(s)
if ans:
print("yes")
else:
print("no")
#Method 2: Loop to compare letters
def is_palindrome2(s):
n = len(s)
for i in range(n // 2):
if s[i] != s[n - i - 1]:
return False
return True
s = input("Enter a word: ")
if is_palindrome2(s):
print("Yes")
else:
print("No")
#Method 3: Using 'reversed()' function
def is_palindrome3(s):
return s == ''.join(reversed(s))
s = input("Enter a word: ")
if is_palindrome3(s):
print("Yes")
else:
print("No")
#Method 4: Using recursive function
def is_palindrome4(s):
if len(s) <= 1:
return True
if s[0] != s[-1]:
return False
return is_palindrome4(s[1:-1])
s = input("Enter a word: ")
if is_palindrome4(s):
print("Yes")
else:
print("No")