Car Talk2

#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#
# “Recently I had a visit with my mom and we realized that the two digits that
# make up my age when reversed resulted in her age. For example, if she’s 73,
# I’m 37. We wondered how often this has happened over the years but we got
# sidetracked with other topics and we never came up with an answer.
# “When I got home I figured out that the digits of our ages have been
# reversible six times so far. I also figured out that if we’re lucky it would
# happen again in a few years, and if we’re really lucky it would happen one
# more time after that. In other words, it would have happened 8 times over all.
# So the question is, how old am I now?”

def are_digits_reversed ( v1, v2 ) :
    """Accepts two strings, 2 characters long.  Returns True if the two strings
are reversed, that is, character 1 from 1 string is the same as character 2 from
the other string"""
    return v1[0] == v2[1] and v1[1] == v2[0]

def number_instances ( diff ) :
    """Returns the number of times the digits are reversed given the differnce
in the ages"""
    child = 0
    age_list = []
    while True :
        parent = child + diff
        parent_age_str = "%02d" % parent
        child_age_str = "%02d" % child
        if are_digits_reversed ( parent_age_str, child_age_str ) :
            age_list.append(child)
        if parent > 99 :
            break
        child += 1
    return age_list

print ("Diff\t#instances")
for diff in range ( 10, 70 ) :      # seems unlikely that the mother would be 10...
    age_list = number_instances ( diff )
    if len(age_list) > 0 :
        print ("%d\t%d" % (  diff, len(age_list) ))
    if len(age_list) == 8 :
        print ("I am currently %d years old and my parent is %d years olf" \
              % ( age_list[5], age_list[5]+diff ))

Result as below:

 

from Thinking in Python

转载于:https://www.cnblogs.com/ryansunyu/p/3828340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值