Sinisterly
Lunar Python Challenge: Palindrome Checker - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: Lunar Python Challenge: Palindrome Checker (/Thread-Lunar-Python-Challenge-Palindrome-Checker)



Python Challenge: Palindrome Checker - Megan - 12-12-2015

So after finishing @GOD 's challenge I decided to make my own.

Here is my attempt at making a palindrome checker, stupidly easy to do but will be interesting to see if anyone would do it differently?

Code:
#!/usr/bin/env python

word_one = raw_input("Enter in your word to be checked: ")
word_two = word_one[::-1]
if word_one == word_two:
    print word_one + " is a palindrome."
else:
    print word_one + " is not a palindrome."



RE: Python Challenge: Palindrome Checker - m0dem - 12-12-2015

Code:
print("Enter a word...")
w = raw_input("> ")
if w == w[::-1]:
    print("is palindrome")

else:
    print("not pal")

Which is basically the same as you... I don't know if there are many other ways to do it tho.

Since I was too bored with the easy way xD ... I just made a function (a spec more complex) Smile
Code:
def isPal(w):
    w = w.lower()
    for i in range(0, len(w)):
        if w[i] == w[len(w) - 1 - i]:
            continue

        return False

    return True



RE: Python Challenge: Palindrome Checker - Nil - 12-12-2015

So, the solution I immediately thought of is the same thing you basically have. The only issue with that code is that it uses spaces as part of it's check. So if you want to check a multi-word string as a palindrome you'll get false.

So you could just add another statement such as:

Code:
word_one = input("Enter in your word to be checked: ")
word_one = word_one.replace(' ', '')
word_two = word_one[::-1]
if word_one == word_two:
    print(word_one + " is a palindrome.")
else:
    print(word_one + " is not a palindrome.")

So with that statement added, you can end up with something like:
Code:
word_one = input("Enter a word/sentence to check as a palindrome: ").replace(' ','')
if word_one == word_one[::-1]:
    print('That is a palindrome')
else:
    print('That is not a palindrome.')



RE: Python Challenge: Palindrome Checker - roger_smith - 12-13-2015

what does word_one[::-1] do?


RE: Python Challenge: Palindrome Checker - m0dem - 12-13-2015

(12-13-2015, 03:28 AM)roger_smith Wrote: what does word_one[::-1] do?

It reverses the string.
http://stackoverflow.com/questions/931092/reverse-a-string-in-python
https://docs.python.org/2/whatsnew/2.3.html#extended-slices


RE: Python Challenge: Palindrome Checker - Inori - 12-16-2015

There's a few options here
Code:
#solve using a double ended queue, which gives more versatility
from collections import deque

def dequeSolve(i):
    return i==str(deque(i).rotate(len(i)))

#solve with reversal
def revSolve(i):
    return i==i[::-1]

Because i==whatever returns a boolean value, you can use it as your return statement.


RE: Python Challenge: Palindrome Checker - Viomi - 01-31-2016

I'd also highly recommend converting any capital letters to non-capitals. Otherwise it will think "Calac" isn't a palindrome due to the capital letter at the beginning.

Now we just need to make a program for emordnilaps..


RE: Python Challenge: Palindrome Checker - Iron Lady - 01-31-2016

Code:
(lambda s: print("Palindrome") if s[::-1] == s else print("Not palindrome"))("".join([c if c.isalpha() else "" for c in "".join([input() for _ in range(int(input()))]).lower()]))



RE: Python Challenge: Palindrome Checker - Sans - 02-09-2016

Code:
>>> def isPalindrome(x): return x.lower() == x.lower()[::-1]
...
>>> isPalindrome("AbAbA")
True
one line + example, though its nothing new


RE: Python Challenge: Palindrome Checker - Shebang - 02-09-2016

Code:
(lambda x:x==x[::-1])(filter(str.isalpha,raw_input()).lower())

>>> (lambda x:x==x[::-1])(filter(str.isalpha,raw_input()).lower())
Is this a palindrome?
False
>>> (lambda x:x==x[::-1])(filter(str.isalpha,raw_input()).lower())
A man, a plan, a canal, Panama!
True