Skip to content

Commit cb01de4

Browse files
author
avais umar
committed
Using slicing technique checking for palindrome
1 parent 1be790d commit cb01de4

File tree

1 file changed

+4
-39
lines changed

1 file changed

+4
-39
lines changed

Palindrome_Checker.py

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,15 @@
1-
# AUTHOR: ekbalba
2-
# DESCRIPTION: A simple script which checks if a given phrase is a Palindrome
3-
# PALINDROME: A word, phrase, or sequence that reads the same backward as forward
41

5-
samplePhrase = "A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!"
6-
# givenPhrase = ""
7-
# phrase = ""
8-
9-
givenPhrase = input("\nPlease input a phrase:(Press ENTER to use the sample phrase) ") #takes a phrase for input
10-
11-
#if nothing in given as input then the sample phrase is stored in the variable phrase otherwise the given phrase if stored
12-
if givenPhrase == "":
13-
print("\nThe sample phrase is: {0}".format(samplePhrase))
14-
phrase = samplePhrase
15-
else:
16-
phrase = givenPhrase
17-
18-
phrase = ''.join([c for c in phrase.lower() if c.isalpha() or c.isdigit()]) #converting all the characters of the phrase to the lowercase
19-
20-
length_ = len(phrase) #returns the length of string
21-
bol_ = True
22-
23-
# check using two pointers, one at beginning
24-
# other at the end. Use only half of the list.
25-
for items in range(length_ // 2):
26-
if phrase[items] != phrase[length_ - 1 - items]:
27-
print("\nSorry, The given phrase is not a Palindrome.")
28-
bol_ = False
29-
break
30-
31-
if bol_ == True:
32-
print("\nWow!, The phrase is a Palindrome!")
33-
34-
35-
36-
372

383

394
"""
40-
Method #2:
415
42-
A simple mmethod is , to reverse the string and and compare with original string.
6+
A simple method is , to reverse the string and and compare with original string.
437
If both are same that's means string is palindrome otherwise else.
448
"""
9+
phrase=input()
4510
if phrase==phrase[::-1]:#slicing technique
4611
"""phrase[::-1] this code is for reverse a string very smartly """
4712

48-
print("\nBy Method 2: Wow!, The phrase is a Palindrome!")
13+
print("\n Wow!, The phrase is a Palindrome!")
4914
else:
50-
print("\nBy Method 2: Sorry, The given phrase is not a Palindrome.")
15+
print("\n Sorry, The given phrase is not a Palindrome.")

0 commit comments

Comments
 (0)