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.创建线程的开销比创建进程的开销小, ...
随机推荐
- HDU 1023
卡特兰数.把进栈看成是+1,出栈看成是-1,任何时候部分和都有a1+a2+....ak>=0.求这样的数列的个数.这明显是卡特兰数的一个解释嘛.在<组合数学>这本书就有这样的原本的证 ...
- aliyun Ubuntu 14.04 64bit OpenJDK Tomcat7 install
my work environment: aliyun Ubuntu 14.04 64位 first phase:apt-get update (it is very important,oth ...
- org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Session after a
1.错误描写叙述 org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Sessio ...
- Java知识点解析
JAVA 1:简述Java的基本历史 java起源于SUN公司的一个GREEN的项目,其原先目的是为家用消费电子产品 发送一个信息的分布式代码系统,通过发送信息控制电视机.冰箱等. 2:简单写出Jav ...
- Mysql 数据迁移后 启动出错
今天上班后不知道为什么,mysql一直无法启动,折腾了半天于是决定重装 我本地的server用的是wamp , 重装的时候, 要进行数据备份 , 我使用的最简单粗暴的备份方式, 就是直接进入到mysq ...
- ES什么时候会平衡分片
"cluster.routing.allocation.balance.shard":"0.45f",//定义分配在该节点的分片数的因子 阈值=因子*(当前节点 ...
- 93.快速搭建Web环境 Angularjs + Express3 + Bootstrap3
转自:https://www.cnblogs.com/wawahaha/p/3946023.html 前言 Angularjs越用越顺手,不仅代码量比jQuery少很多,而且实现思路特别清晰,构建大型 ...
- What's new in Safari 11.0
导语: Safari 11.0 的亮点 网络会议.使用WebRTC标准实现对等会议. 开发调试工具增强.使用用于测量代码和网络性能的新工具来测试代码. WebAssembly.当使用新的WebAsse ...
- SQL之子查询
子查询概念:把一个查询的结果在另一个查询中使用就叫做子查询 1.子查询作为条件时 当我们使用子查询作为条件时,若子查询返回值为多个,则会报以下错误: "子查询返回的值不止一个.当子查询跟随在 ...
- Centos 7 iptables 开放端口
MySQL 开放远程连接时, 已经打开了对应端口的安全组发现还是连接不上, 那么就需要 check 一下防火墙端口是否开放. firewall-cmd --zone=public --query-po ...