Skip to content

Instantly share code, notes, and snippets.

@knowshan
Last active February 12, 2016 01:34
Show Gist options
  • Save knowshan/6dc72730eba421151190 to your computer and use it in GitHub Desktop.
Save knowshan/6dc72730eba421151190 to your computer and use it in GitHub Desktop.
Palindrome test using range or loop operators
#!/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
#!/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