chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


Lunar Python Challenge: Palindrome Checker filter_list
Author
Message
Python Challenge: Palindrome Checker #1
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."
[Image: 9JVyFsC.png]
Sig by @Symadox

XMPP: Mi5@DukGo.com

[+] 2 users Like Megan's post
Reply

RE: Python Challenge: Palindrome Checker #2
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
(This post was last modified: 12-12-2015, 09:29 PM by m0dem.)

[+] 1 user Likes m0dem's post
Reply

RE: Python Challenge: Palindrome Checker #3
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.')

[+] 1 user Likes Nil's post
Reply

RE: Python Challenge: Palindrome Checker #4
what does word_one[::-1] do?
---
Click here to get started with Linux!

If I helped you, please +rep me, apparently we've started over on Rep and I'd like to break 100 again...

Inori Wrote: got clickbaited by roger

Reply

RE: Python Challenge: Palindrome Checker #5
(12-13-2015, 03:28 AM)roger_smith Wrote: what does word_one[::-1] do?

It reverses the string.
http://stackoverflow.com/questions/93109...-in-python
https://docs.python.org/2/whatsnew/2.3.h...ded-slices

[+] 2 users Like m0dem's post
Reply

RE: Python Challenge: Palindrome Checker #6
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.

Reply

RE: Python Challenge: Palindrome Checker #7
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..

Reply

RE: Python Challenge: Palindrome Checker #8
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()]))
141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622

Reply

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

Reply

RE: Python Challenge: Palindrome Checker #10
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
[Image: CDUAq9d.png]

Reply






Users browsing this thread: 1 Guest(s)