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.创建线程的开销比创建进程的开销小, ...
随机推荐
- Could not connect to SMTP host: localhost, port: 25;
1.错误描写叙述 DEBUG: setDebug: JavaMail version 1.3.3 DEBUG: getProvider() returning javax.mail.Provider[ ...
- nodeJS npm grunt grunt-cli
1.安装好nodeJS后 ,一般都会把npm也安装好的.nodeJs集成npm的,可通过在cmd 分别运行 node -v和 npm -v来查看他们的版本,假设显示可说明可继续以下的操作 2.想安装g ...
- BZOJ 3112 [Zjoi2013]防守战线 线性规划
题意: 简单叙述: 一个长度为n的序列,在每一个点建塔的费用为Ci.有m个区间.每一个区间内至少有Dj个塔.求最小花费. 方法:线性规划 解析: 与上一题相似.相同使用对偶原理解题.解法不再赘述. 代 ...
- c:\Windows\System32\drivers\etc\hosts的作用
c:\Windows\System32\drivers\etc\hosts 是域名解析文件. 可以直接用记事本打开.将IP地址重定向. 格式为:ip地址-空格-域名 可以将一个域名重新定向到一个IP ...
- SPOJ 694/705 后缀数组
思路: 论文题*n Σn-i-ht[i]+1 就是结果 O(n)搞定~ //By SiriusRen #include <cstdio> #include <cstring> ...
- BZOJ 1898 构造矩阵+矩阵快速幂
思路: T的最小公倍数是12 那么12以内暴力 整除12 的部分用矩阵快速幂 //By SiriusRen #include <cstdio> #include <cstring&g ...
- 转js调优
随着网络的发展,网速和机器速度的提高,越来越多的网站用到了丰富客户端技术.而现在Ajax则是最为流行的一种方式.javascript是一种解释型 语言,所以能无法达到和C/Java之类的水平,限制了它 ...
- Optional Chaining as an Alternative to Forced Unwrapping
?与!的区别 You specify optional chaining by placing a question mark (?) after the optional value on whic ...
- vue mint-ui swipe 不显示或显示空白
vue mint-ui swipe 不显示或显示空白? 解决需要在mt-swipe上层元素设置高度 <div> <div> <mt-header title=" ...
- CF1000G Two-Paths (树形DP)
题目大意:给你一棵树,点有点权$a_{i}$,边有边权$w_{e}$,定义一种路径称为$2-path$,每条边最多经过2次且该路径的权值为$\sum _{x} a_{x}\;-\;\sum_{e}w_ ...