[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

understand how s and s11 shorten one character and then grow one character at a time because there is no loop.

Posted on 2014-08-03
4
Medium Priority
?
158 Views
Last Modified: 2014-08-05
def toChars(s):
    import string
    #s = string.lower(s)
    s = s.lower()
    ans = ''
    for c in s:
        #if c in string.lowercase:
        if c in s:
            ans = ans + c
    return ans

def isPal(s):
    if len(s) <= 1:
        return True
    else:
        s11=s[1:-1]
        equation=(s[0] == s[-1])
        returnequation=equation
        returnispal=isPal(s[1:-1])
        returnboth=returnequation and returnispal
        return returnboth

def isPalindrome(s):
    """Returns True if s is a palindrome and False otherwise"""
    mychars=toChars(s)
    returnispal=isPal(mychars)
    return returnispal

print (isPalindrome('Guttag'))
print (isPalindrome('Guttug'))
print (isPalindrome('Able was I ere I saw Elba'))
print (isPalindrome('Are we not drawn onward, we few, drawn onward to new era?'))

Open in new window


I created variables so I can see changes in a debugger ide

in def is Palindrome(s)
I do not understand how s and s11 shorten one character and then grow one character at a time because there is no loop.
0
Comment
Question by:rgb192
  • 2
  • 2
4 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 40238225
the ispal function is being called recursively.
0
 

Author Comment

by:rgb192
ID: 40239508
line
19
26
or both

and how
0
 
LVL 46

Accepted Solution

by:
aikimark earned 2000 total points
ID: 40239541
isPalindrome is the initial invocation of the isPal function.  The recursion only happens once you are inside the isPal function.  So, I think the answer to your question is 19.
0
 

Author Closing Comment

by:rgb192
ID: 40241162
thanks
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question