Recursive Palandrome Checking in Python
-
This one comes up a lot in interview questions it seems: make a program that checks if something that is input is a palandrome. Most people check this using some sort of a loop. I've seen people look to do this using recursion which makes it a little cleaner, but is a very advanced interview question. I've never had this asked in Python but have had to do this in Ruby before and while on an airplane recently with very little to do I implemented this in Python too. So here is a simple palandrome checking application in Python using recursion. Enjoy.
myPalandrome = input("Input a string to test: ") def testPalandrome(p, f, b): if f == b: print("Palandrome!") elif p[f] == p[b] and f == (b -1): print("Palandrome!") elif p[f] != p[b]: print("Gibberish") else: testPalandrome(p, (f + 1), (b - 1)) testPalandrome(myPalandrome, 0, (len(myPalandrome) - 1))