Solution Exercise 20

Define a function is_palindrome() that recognizes palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar") should return True.

def is_palindrome(inStr):
    #front is the index value to the first character
    front = 0
    #end is the index value to the last character
    end = len(inStr)-1
    
    #Now we want to make sure that the character found at index front
    #is equal to the character found at index end.
    #We make the characters lower case before we compare them
    #Also we will make sure that front is less than end because if that
    #is not the case we can stop checking
    while inStr[front].lower() == inStr[end].lower() and front < end:
        front += 1 #Increase front by one
        end -= 1   #Decrease end by one

    #We can exit the while loop in two ways
    #1. If the two characters that we compare are not equal
    #2. If front is not less than end
	 
    #If, at this point in the program, front is still less than end
    #we have exited the while loop as described in case 1 above and then
    #we know that this is not a palindrome
    #If front is not less than end then we know this is a palindrome (case 2 above)
    if front < end:
        return False
    else:
        return True

def main():
    if is_palindrome("Radar"):
        print("It is a palindrome")
    else:
        print("It's not a palindrome")
    
if __name__ == '__main__':
    main()