Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96174
License: OTHER
1
"""This module contains a code example related to
2
3
Think Python, 2nd Edition
4
by Allen Downey
5
http://thinkpython2.com
6
7
Copyright 2015 Allen Downey
8
9
License: http://creativecommons.org/licenses/by/4.0/
10
"""
11
12
from __future__ import print_function, division
13
14
15
def first(word):
16
"""Returns the first character of a string."""
17
return word[0]
18
19
20
def last(word):
21
"""Returns the last of a string."""
22
return word[-1]
23
24
25
def middle(word):
26
"""Returns all but the first and last characters of a string."""
27
return word[1:-1]
28
29
30
def is_palindrome(word):
31
"""Returns True if word is a palindrome."""
32
if len(word) <= 1:
33
return True
34
if first(word) != last(word):
35
return False
36
return is_palindrome(middle(word))
37
38
39
print(is_palindrome('allen'))
40
print(is_palindrome('bob'))
41
print(is_palindrome('otto'))
42
print(is_palindrome('redivider'))
43
44
45