Using Python How Do You Write a Palindrome Tester with Special Requirements?

Problem scenario
You want to write a Python that will test if a string is a palindrome with two other requirements. One requirement is to not use a "::" (a colon pair) in the code. The second requirement is to search for a substring that is a palindrome if the full string is not a palindrome. This substring is not any substring however: it must include the leftmost character. For this requirement we will consider a one-character string to be a palindrome.

How do you create a palindrome testing function without using a double colon that tests for substrings to be palindromic if the entire string is not a palindrome?

Here is the code:

def palindrome_tester(string_to_test):
    if (len(string_to_test) == 1):
        return string_to_test
    tempa = list(string_to_test)  # Make the string a list for reversibility
    for x in range(0, len(tempa)):
        # Convert the tempa list to a string via the str func, map func
        # and the join method
        string_test = ''.join(map(str, tempa))
    if (string_test == string_to_test):
        return string_to_test
        # Remove the rightmost character and test resulting string
        string_to_test = string_to_test[:-1]
        # When using recursion, use the return keyword
        return palindrome_tester(string_to_test)

string_to_test = input("Enter a string: ")
result = palindrome_tester(string_to_test)
print("The longest palindrome that can be made of the characters is:")

Leave a comment

Your email address will not be published. Required fields are marked *