forked from AllenDowney/ThinkPython2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcartalk3.py
83 lines (61 loc) · 1.87 KB
/
cartalk3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from __future__ import print_function, division
def str_fill(i, n):
"""Returns i as a string with at least n digits.
i: int
n: int length
returns: string
"""
return str(i).zfill(n)
def are_reversed(i, j):
"""Checks if i and j are the reverse of each other.
i: int
j: int
returns:bool
"""
return str_fill(i, 2) == str_fill(j, 2)[::-1]
def num_instances(diff, flag=False):
"""Counts the number of palindromic ages.
Returns the number of times the mother and daughter have
palindromic ages in their lives, given the difference in age.
diff: int difference in ages
flag: bool, if True, prints the details
"""
daughter = 0
count = 0
while True:
mother = daughter + diff
# assuming that mother and daughter don't have the same birthday,
# they have two chances per year to have palindromic ages.
if are_reversed(daughter, mother) or are_reversed(daughter, mother+1):
count = count + 1
if flag:
print(daughter, mother)
if mother > 120:
break
daughter = daughter + 1
return count
def check_diffs():
"""Finds age differences that satisfy the problem.
Enumerates the possible differences in age between mother
and daughter, and for each difference, counts the number of times
over their lives they will have ages that are the reverse of
each other.
"""
diff = 10
while diff < 70:
n = num_instances(diff)
if n > 0:
print(diff, n)
diff = diff + 1
print('diff #instances')
check_diffs()
print()
print('daughter mother')
num_instances(18, True)