Recursion On Non - Numerics
Recursion On Non - Numerics
on
non-numerics
How
could
we
check
whether
a
string
of
characters
is
a
palindrome,
i.e.,
reads
the
same
forwards
and
backwards
Able
was
I
ere
I
saw
Elba
aAributed
to
Napolean
Are
we
not
drawn
onward,
we
few,
drawn
onward
to
new
era?
Example
Able
was
I
ere
I
saw
Elba
ablewasiereisawleba
isPalindrome(ablewasiereisawleba)
is
same
as
a
==
a
and
isPalindrome(blewasiereisawleb)
def isPalindrome(s):! ! def toChars(s):! s = s.lower()! ans = ''! for c in s:! if c in 'abcdefghijklmnopqrstuvwxyz':! ans = ans + c! return ans! ! def isPal(s):! if len(s) <= 1:! return True! else:! return s[0] == s[-1] and isPal(s[1:-1])! ! return isPal(toChars(s))!