Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

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

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.
Avatar of aikimark
aikimark
Flag of United States of America image

the ispal function is being called recursively.
Avatar of rgb192

ASKER

line
19
26
or both

and how
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

thanks