Last active
February 12, 2016 01:34
-
-
Save knowshan/6dc72730eba421151190 to your computer and use it in GitHub Desktop.
Palindrome test using range or loop operators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
words = ['madam', 'dalada', 'kayak', 'civic'] | |
print 'Palindrome test using range, loop and array index' | |
for word in words: | |
word_r = "".join([word[i] for i in range(len(word)-1, -1, -1)]) | |
if word == word_r: | |
print "%s is palindrome" %word | |
else: | |
print "%s is NOT palindrome" %word |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
words = ['madam', 'dalada', 'kayak', 'civic'] | |
print 'Palindrome test using slice' | |
for word in words: | |
word_rev = word[::-1] | |
if word == word_rev: | |
print "%s is palindrome" %word | |
else: | |
print "%s is NOT palindrome" %w |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment