The is_palindrome function checks if a string is a palindrome. A palindrome is a string that can be equally read from left to right or right to left, omitting blank spaces, and ignoring capitalization. Examples of palindromes are words like kayak and radar, and phrases like "Never Odd or Even". Fill in the blanks in this function to return True if the passed string is a palindrome, False if not.
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for ___:
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if ___:
new_string = ___
reverse_string = ___
# Compare the strings
if ___:
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
Answers
Answer:
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for r in input_string.strip():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
new_string = new_string+r.replace(" ","")
reverse_string = r.replace(" ","")+reverse_string
# Compare the strings
if new_string.lower() == reverse_string.lower():
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
Explanation:
.
function to return True if the passed string is a palindrome
Explanation:
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for i in range(len(input_string)):
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if input_string[i]!=" ":
new_string = new_string+input_string[i]
reverse_string = input_string[i]+reverse_string
# Compare the strings
if new_string.lower()==reverse_string.lower():
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
hence, above are the correct answers for the given blanks