-2

guys I know there were similar question, but pls help with this piece of code - I could find only codes to similar question but following another logic, which is not usefull to learn what I need to learn.. There is what is wanted: 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. and there is what I came up with.. working but missing something

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = "{input_split}".format(input_split = input_string.lower())
    reverse_string = "{input_split}".format(input_split = input_string.lower()[::-1])

    # Traverse through each letter of the input string
    for letter in new_string:
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        if letter[0] in new_string != " ":
            new_string = "{letter}{new_string}".format(letter = letter[0],new_string = new_string[1:]) 
            reverse_string = "{letter}{reverse_string}".format(letter = letter[0],reverse_string = reverse_string[1:])
    # Compare the strings
    if new_string == reverse_string:
        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
  • 2
    This seems like an unnecessarily complicated way... – mozway Oct 31 '21 at 16:27
  • You'll want to read up a bit on what the `in` keyword is and how it's used : https://stackoverflow.com/questions/19775692/use-and-meaning-of-in-in-an-if-statement – ShadowMitia Oct 31 '21 at 16:31
  • *"Fill in the blanks in this function"*: which blanks? what did you fill in? what is template code? – trincot Oct 31 '21 at 16:32

4 Answers4

0

This might help

def is_palindrome(a_string): 
    string_lower = a_string.lower()
    empty_string ="" 
    for char in string_lower:
        if char.isalpha():
            empty_string += char
    # print(empty_string)
    if empty_string == empty_string[::-1]:
        return True
    else:
        return False
        
Sanket Wagh
  • 156
  • 1
  • 14
0

Use the "split" function and split by whitespaces --> then join all the split strings

def is_palindrome(input_string):
    if input_string.lower() == "".join(input_string.split(' ')).lower()[::-1]:
        print("True")
    else:
        print("False")
PreciXon
  • 443
  • 2
  • 9
0

You're overthinking this. Once you have the reversed string, you can directly see if the original string is a palindrome by checking if the two strings are equal:

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    simple_string = input_string.lower().replace(' ', '')
    reverse_string = simple_string[::-1]
    return simple_string == reverse_string

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
Arne
  • 9,990
  • 2
  • 18
  • 28
0

Okey thanks for pointing me in some direction, this is what I dont like about these case studies, it's written in certain way, to teach you what new methods u acquired, but it's not following any logic, or logic that you would apply and then I am messing up to complex code, even if it's possible to make it in few lines. for the future, that one might google it. hopefully after some effort of doing it by himself..

new_string = input_string.replace(" ", "").lower()
reverse_string = input_string.replace(" ",""). lower()[::-1]
    if new_string == reverse_string:
        return True
    return False