#! /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. QT5 OpenGL (六, 键盘事件, 开关灯,放大缩小综合运用)

    概要 实例效果图 立体图放大图 立体图缩小图 不加矢量开灯图 不加矢量关灯图 加矢量关灯图1 加矢量关灯图2 部分代码展示 主要内容解析 QT键盘事件 立体图形的放大和缩小 上下左右键以及A键D争键控 ...

  2. Android学习之GridView图片布局适配经验

    開始解说这篇博客之前,我想问一下,当布局相似GridView这样的多列布局时,我们该怎么布局,才干更好的去适配呢? 扣张图来展示一下 比如这样的需求,三张图片均分屏幕 实现方法: 1.切图固定,比如是 ...

  3. Executors线程池关闭时间计算

    Executors线程池关闭时间计算 学习了:http://blog.csdn.net/wo541075754/article/details/51564359 https://www.cnblogs ...

  4. selenium的报错信息:selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted

    报错信息:selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class ...

  5. Javaee 应用分层架构

    应用分层的优点:修改方便,仅修改有问题的那层以及其相邻几层即可,层数越多,其相应的资源分配也会更加平均 缺点:耗费时间,速度慢,调用占用大量堆栈. JAVAEE的分层: 4层分法:1.客户层:运行在客 ...

  6. Sambaserver搭建

    1. 安装Samba及相关包 $ sudo apt-getinstall samba samba-common smbfspython-glade2system-config-samba 2. 创建共 ...

  7. .net垃圾回收-原理浅析

    本文引自:http://www.cnblogs.com/wilber2013/p/4357910.html 在开发.NET程序过程中,由于CLR中的垃圾回收(garbage collection)机制 ...

  8. HTML基础——网站后台显示页面

    1.框架集标签:(作用:将页面进行区域的划分) <frameset rows="" cols=""> <frame src="&qu ...

  9. 人工智能,你到底是天使or魔鬼?

    人工智能的概念早在60多年前就被提出,但又一度沉寂.随着谷歌人工智能程序AlphaGo(阿尔法狗)战胜围棋世界冠军李世石,再次为世人瞩目.然而,与无限风光一起相伴而来的,还有关于人工智能的种种争议! ...

  10. 创建一个 Django 项目

    一. 创建项目 其中: 确认项目是否创建成功: 在 manage.py 目录上运行 python manage.py runserver server 启动后,在浏览器访问 http://127.0. ...