#--------------------------------------------------------------- # pal_naive.py #--------------------------------------------------------------- # Naive palindrome # take in user input exp = input('Give me a palindrome: ') # string reversing routine new = '' # new is initially empty for x in exp: new = x + new exp_rev = new # exp_rev is reversed exp # test and print out if len(exp) <= 2 : # case 1: input too short print('Sorry, try something longer.') elif exp == exp_rev : # case 2: is palindrome print('YES, "'+exp+'" is a palindrome.') else : # case 3: not palindrome print('NO, "'+exp+'" is not a palindrome.') #--------------------------------------------------------------- # pal_smart.py #--------------------------------------------------------------- # Smart palindrome # take in user input exp = input('Give me a palindrome: ') # clean up user input: lowercase, remove space and punctuation exp_clean = exp.lower().replace(' ', '').replace(',', '').replace("'", '').replace(':', '').replace('.', '') # string reversing routine, using exp_clean now new = '' # new is initially empty for x in exp_clean : new = x + new exp_rev = new # exp_rev is reversed exp_clean # test and print out if len(exp) <= 2 : # case 1: input too short print('Sorry, try something longer.') elif exp_clean == exp_rev : # case 2: is palindrome print('YES, "'+exp+'" is a palindrome.') else : # case 3: not palindrome print('NO, "'+exp+'" is not a palindrome.') #--------------------------------------------------------------- # pal_loop.py #--------------------------------------------------------------- # Insistent palindrome print("Hello! Let's start.") # initial message, outside loop # loop condition: initially set to false success = False # loop back while unsuccessful while not success : # take in user input exp = input('Give me a palindrome: ') # clean up user input: lowercase, remove space and punctuation # line was too long: using \ to break up exp_clean = exp.lower().replace(' ', '').replace(',', '') \ .replace("'", '').replace(':', '').replace('.', '') # string reversing routine, using exp_clean now new = '' # new is initially empty for x in exp_clean : new = x + new exp_rev = new # exp_rev is reversed exp_clean # test and print out if len(exp) <= 2 : # case 1: input too short print('Sorry, try something longer.') elif exp_clean == exp_rev : # case 2: is palindrome print('YES, "'+exp+'" is a palindrome.') success = True # loop condition changed else : # case 3: not palindrome print('NO, "'+exp+'" is not a palindrome. Let\'s try again.') print("Goodbye.") # last message, outside loop #--------------------------------------------------------------- # pal_functions.py #--------------------------------------------------------------- # Modular palindrome def getRev(wd): "Takes a string, returns its reverse" rev = '' for i in wd : rev = i + rev return rev def cleanInput(foo): "Lowercases input, removes space and punctuation .,':" return foo.lower().replace(' ', '').replace(',', '')\ .replace("'", '').replace(':', '').replace('.', '') ############################################################# # Main routine below print("Hello! Let's start.") # initial message, outside loop # loop condition: initially set to false success = False # loop back while unsuccessful while not success : # take in user input exp = input('Give me a palindrome: ') exp_clean = cleanInput(exp) # clean input exp_rev = getRev(exp_clean) # reverse cleaned input # test and print out if len(exp) <= 2 : # case 1: input too short print('Sorry, try something longer.') elif exp_clean == exp_rev : # case 2: is palindrome print('YES, "'+exp+'" is a palindrome.') success = True # loop condition changed else : # case 3: not palindrome print('NO, "'+exp+'" is not a palindrome. Let\'s try again.') print("Goodbye.") # last message, outside loop