ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Recursive Palandrome Checking in Python

    Developer Discussion
    recursion python
    1
    1
    1.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by scottalanmiller

      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))
      
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post