#! /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

Car Talk2的更多相关文章

  1. Day8-面向对象进阶&&socket基础

    抽象类 python2中的写法 import abc class Alert(object): '''报警基类''' __metaclass__ = abc.ABCMeta @abc.abstract ...

  2. DirectSound的应用

    假设仅仅使用PlaySound()这个API函数来表现声音效果的话,那么就无法表现出声音的混音效果,由于PlaySound在播放还有一个声音时,必定会导致现有声音的停止.因此,使用 PlaySound ...

  3. Lua学习笔记(五):面向对象的实现

    Lua本身是没有class之类的关键字的,但是我们可以巧妙利用function也是值和table的特性来实现面向对象的特性. 通过复制表的实现 Lua中的类也是一个table对象,下面我们看看一个简单 ...

  4. DirectSound应用

    只是使用的假设PlaySound()这个API函数来显示的声音效果,然后,然后,它不会出现在混合声音,因为PlaySound还有播放期间声音,这将不可避免地导致现有声音停止. 因此,使用 PlaySo ...

  5. 33、线程与全局解释器锁(GIL)

    之前我们学了很多进程间的通信,多进程并发等等,今天我们来学习线程,线程和进程是什么关系,进程和线程有什么相同而又有什么不同今天就来揭晓这个答案. 一.线程概论 1.何为线程 每个进程有一个地址空间,而 ...

  6. python并发编程之多线程二

    一,开启线程的两种方式 方法一: from threading import Thread import random,time def eat(name): print('%s is eating. ...

  7. java--抽象类实例(包含静态内部抽象类)

    静态内部抽象类可以被继承. public class testfather { public static void main(String[] args) { person.talk2 a = ne ...

  8. java 多态 ---父类调用子类方法

    package test1;//多态的体现import javax.print.attribute.standard.RequestingUserName;import java.util.Scann ...

  9. python并发编程之多线程1

    一多线程的概念介绍 threading模块介绍 threading模块和multiprocessing模块在使用层面,有很大的相似性. 二.开启多线程的两种方式 1.创建线程的开销比创建进程的开销小, ...

随机推荐

  1. 号外:Spark 1.3.0公布了,快来一起飞!

    Spark 1.3.0 Release Note Spark 1.3.0在上周五正式公布.真是千呼万唤始出来.本次公布最大的惊喜就是DataFrame.另外一个值得关注的是Spark SQL从Alph ...

  2. linux centos下载地址

    Centos下载地址 http://r.aminglinux.com

  3. 采集电脑摄像头和mic,rtp端口推送音视频工具

    介绍:这个是我在做一个rtmp播放的项目中自己写的rtp推送的工具,可选择摄像头,可选择推送rtp的端口和ip 下载地址: github:https://github.com/alexhegang/s ...

  4. POJ 3368 线段树

    思路: 先统计在第i个位置当前数字已经出现的次数. 维护两个数组,一个是当前位置的数字最后一次出现的位置,另一个是当前位置的数字第一次出现的位置 查找的时候分为两种情况: 没有和边界相交(意会意会)的 ...

  5. 你不知道的JavaScript(一)数据类型

    ECMAScript 规范地址: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf 有过WEB前端开发 ...

  6. POJ 1852 Ants O(n)

    题目: 思路:蚂蚁相碰和不相碰的情况是一样的,相当于交换位置继续走. 代码: #include <iostream> #include <cstdio> #include &l ...

  7. php计算两个日期相差的天数

    /** * 时间差计算 * * @param Timestamp $time * @return String Time Elapsed */ function time2Units ($time,$ ...

  8. SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理

    在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...

  9. 关于Vue中父子组件相互传值

    Header为子组件,Home为父组件,通过子组件调用父组件 运行结果如下 下面是父组件调用子组件的案例 通过button按钮的click事件 图一是父组件Home中的run方法,图二是msg和fun ...

  10. Vue中两种跳转方式

    第一种:通过标签跳转,<router-link></router-link> 第二种:通过js跳转,定义点击事件进行跳转