zoukankan      html  css  js  c++  java
  • 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	#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	%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

  • 相关阅读:
    深度学习实战 Tricks
    Latex 琐碎
    Latex 琐碎
    数据网站(数据集的获取)、打分网站
    数据网站(数据集的获取)、打分网站
    matplotlib —— 添加文本信息(text)
    hdu 1506 Largest Rectangle in a Histogram
    poj2007
    OpenRisc-43-or1200的IF模块分析
    RHEL 6.4 安装svn和apache
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3828340.html
Copyright © 2011-2022 走看看