Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96169
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 has_palindrome(i, start, length):
16
"""Checks if the string representation of i has a palindrome.
17
18
i: integer
19
start: where in the string to start
20
length: length of the palindrome to check for
21
"""
22
s = str(i)[start:start+length]
23
return s[::-1] == s
24
25
26
def check(i):
27
"""Checks if the integer (i) has the desired properties.
28
29
i: int
30
"""
31
return (has_palindrome(i, 2, 4) and
32
has_palindrome(i+1, 1, 5) and
33
has_palindrome(i+2, 1, 4) and
34
has_palindrome(i+3, 0, 6))
35
36
37
def check_all():
38
"""Enumerate the six-digit numbers and print any winners.
39
"""
40
i = 100000
41
while i <= 999996:
42
if check(i):
43
print(i)
44
i = i + 1
45
46
47
print('The following are the possible odometer readings:')
48
check_all()
49
print()
50
51
52
53