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
Car Talk2的更多相关文章
- Day8-面向对象进阶&&socket基础
抽象类 python2中的写法 import abc class Alert(object): '''报警基类''' __metaclass__ = abc.ABCMeta @abc.abstract ...
- DirectSound的应用
假设仅仅使用PlaySound()这个API函数来表现声音效果的话,那么就无法表现出声音的混音效果,由于PlaySound在播放还有一个声音时,必定会导致现有声音的停止.因此,使用 PlaySound ...
- Lua学习笔记(五):面向对象的实现
Lua本身是没有class之类的关键字的,但是我们可以巧妙利用function也是值和table的特性来实现面向对象的特性. 通过复制表的实现 Lua中的类也是一个table对象,下面我们看看一个简单 ...
- DirectSound应用
只是使用的假设PlaySound()这个API函数来显示的声音效果,然后,然后,它不会出现在混合声音,因为PlaySound还有播放期间声音,这将不可避免地导致现有声音停止. 因此,使用 PlaySo ...
- 33、线程与全局解释器锁(GIL)
之前我们学了很多进程间的通信,多进程并发等等,今天我们来学习线程,线程和进程是什么关系,进程和线程有什么相同而又有什么不同今天就来揭晓这个答案. 一.线程概论 1.何为线程 每个进程有一个地址空间,而 ...
- python并发编程之多线程二
一,开启线程的两种方式 方法一: from threading import Thread import random,time def eat(name): print('%s is eating. ...
- java--抽象类实例(包含静态内部抽象类)
静态内部抽象类可以被继承. public class testfather { public static void main(String[] args) { person.talk2 a = ne ...
- java 多态 ---父类调用子类方法
package test1;//多态的体现import javax.print.attribute.standard.RequestingUserName;import java.util.Scann ...
- python并发编程之多线程1
一多线程的概念介绍 threading模块介绍 threading模块和multiprocessing模块在使用层面,有很大的相似性. 二.开启多线程的两种方式 1.创建线程的开销比创建进程的开销小, ...
随机推荐
- Firefox OS简单介绍
Firefox OS系统架构框图 一些Firefox相关的术语简单介绍: B2G Boot to Gecko 的简称. Boot to Gecko Firefox OS 操作系统的project代号. ...
- Intellij idea 自动完成的变量名称首字母变为小写
Intellij idea 自动完成的变量名称首字母变为小写 好像没有什么好的自动办法,自己输入一个小写的字母吧,然后Idea会出提示.
- 推断CPU 是小端存储(Little endian)还是大端存储(Big endian)模式
第一个版本号: //return true in big-endian machines bool check_big_endian1() { int a = 0; int *p = &a; ...
- 玩转Android Camera开发(三):国内首发---使用GLSurfaceView预览Camera 基础拍照demo
GLSurfaceView是OpenGL中的一个类,也是能够预览Camera的,并且在预览Camera上有其独到之处. 独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就仅仅有使用GLS ...
- Android生命周期里你也许不知道的事
Android生命周期预计连刚開始学习的人都再熟悉只是的东西了,但这里我抛出几个问题.也许大家曾经没有想过或者可能认识的有些错误. 一.当A启动B时,A和B生命周期方法运行的先后顺序是如何的?当按返回 ...
- Codeforces 677D Vanya and Treasure 暴力+BFS
链接 Codeforces 677D Vanya and Treasure 题意 n*m中有p个type,经过了任意一个 type=i 的各自才能打开 type=i+1 的钥匙,最初有type=1的钥 ...
- 基于Asp.Net webApi owin oauth2的实现
干货地址:https://git.oschina.net/DpMa_/WebApi-Owin-oauth2
- js小知识 双叹号(!!)
!!:一般用来将后面的表达式强制转换为布尔值(boolean):true或者false; avascript约定规则为: false.undefinded.null.0.”” 为 false tr ...
- 前端模块化 | 解读JS模块化开发中的 require、import 和 export
本篇分为两个部分 第一部分:总结了ES6出现之前,在当时现有的运行环境中,实现"模块"的方式: 第二部分:总结了ES6出现后,module成为ES6标准,客户端实现模块化的解决方案 ...
- web语义化理解
含义: Web语义化是指使用语义恰当的标签,使页面有良好的结构,页面元素有含义,能够让人和搜索引擎都容易理解. 为什么要web语义化?如今互联网都到了web2.0的时代了,HTML语言在不断的进化并发 ...