Q:1] Why are the print statements (commented) resulting in print(mapped)
to not print the set? If those lines
# print(list(word))
# print(list(palindrome))
are not-commented out, then the output results in:
['N', 'u', 'r', 's', 'e', 's', 'r', 'u', 'n']
['n', 'u', 'r', 's', 'e', 's', 'r', 'u', 'N']
The zipped result is : set()
Sentence is palindrome.
Q:2] Why does N==n
not fail? I was expecting it to fail in the for
loop.
def palindrome(word):
if ' ' in word:
word = word.replace(' ', '')
palindrome = reversed(word)
# print(list(word))
# print(list(palindrome))
mapped = zip(word, palindrome)
# converting values to print as set
mapped = set(mapped)
# printing resultant values
print("The zipped result is : ",end="")
print(mapped)
for letter, rev_letter in zip(word, palindrome):
if letter != rev_letter:
return 'Not Palindrome'
return 'Palindrome'
# Driver program to test sentencePalindrome()
s = "Nurses run"
if (palindrome(s)):
print ("Sentence is palindrome.")
else:
print ("Sentence is not palindrome.")
If these lines below are commented
# print(list(word))
# print(list(palindrome))
the result is:
The zipped result is : {('u', 'u'), ('n', 'N'), ('s', 's'), ('N', 'n'), ('e', 'e'), ('r', 'r')}
Sentence is palindrome.