|
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 |
4 | 1 |
|
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 | | - |
37 | 2 |
|
38 | 3 |
|
39 | 4 | """ |
40 | | -Method #2: |
41 | 5 |
|
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. |
43 | 7 | If both are same that's means string is palindrome otherwise else. |
44 | 8 | """ |
| 9 | +phrase=input() |
45 | 10 | if phrase==phrase[::-1]:#slicing technique |
46 | 11 | """phrase[::-1] this code is for reverse a string very smartly """ |
47 | 12 |
|
48 | | - print("\nBy Method 2: Wow!, The phrase is a Palindrome!") |
| 13 | + print("\n Wow!, The phrase is a Palindrome!") |
49 | 14 | 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