Empty

2013年4月から社会人になりました。

SONY GOF FOR IT ~1問目: 人生の時計~

概要

GO FOR IT を参照.
毎日一問のペースで解いていければいいなと思ってる。


問題(引用)

1) 人生の時計

あなたの一生を24時間にたとえると今日は何時何分何秒ですか?
ただしあなたはあなたの誕生日(a年b月c日)の0時ちょうどに生まれてn歳まで生きる(n歳のときは生きていてn+1歳にはなれない)とし、bとcは一般的な月日の範囲とします。

i) 1990<=a<=2000,n=80のとき、今日は何時何分何秒ですか?
ii) 1900<=a<=2000,n=200のとき、今日は何時何分何秒ですか?

http://www.sony.co.jp/SonyInfo/Jobs/newgrads/sus/q01.html

Script(Python)

ソースコードのライセンスはフリーです。

#!/usr/bin/env python
# coding: utf-8

from datetime import datetime

class Input(object):
        """データセット情報を保持するClass"""

        def __init__(self, year, month, day, age):
                self.year = year
                self.month = month
                self.day = day
                self.age = age
                # display input information
                self.dump()

        def dump(self):
                print "Input:\n\tbirth date(year/month/day): %d/%d/%d\n\tdeath age: %d" % \
                        (self.year, self.month, self.day, self.age)


def solve(input):
        """人生を一日に換算する. (hour, min, second)を返す."""
        birth_date = datetime(input.year, input.month, input.day)
        death_date = datetime(input.year + input.age, input.month, input.day)
        now_date = datetime.now()

        X = convert_second(birth_date, death_date)
        Y = convert_second(birth_date, now_date)
        # 人生を1日に換算する(sec)
        Z = (60 *60 *24 * Y) / X
        # 0時0分0秒からZ秒を経過させる
        h = int(Z / (60 * 60))
        m = int((Z - (h * 60 * 60)) / 60)
        s = int(Z - (h * 60 * 60) - (m * 60))
        return (h, m, s)

def convert_second(from_date, to_date):
        """from_date ~ to_date までの秒数を得る"""
        diff_date = to_date - from_date
        return diff_date.days * 60 * 60 * 24 + diff_date.seconds

if __name__ == "__main__":
        # i)
        ex = Input(1990, 1, 1, 80)
        print "Output:\n\t%02d:%02d:%02d\n-----" % solve(ex)

        ex = Input(2000, 11, 10, 80)
        print "Output:\n\t%02d:%02d:%02d\n-----" % solve(ex)

        # ii)
        ex = Input(1900, 1, 1, 200)
        print "Output:\n\t%02d:%02d:%02d\n-----" % solve(ex)

        ex = Input(1987, 12, 11, 200) # 誕生日。200歳まで生きれたらいいなー。
        print "Output:\n\t%02d:%02d:%02d\n-----" % solve(ex)

        ex = Input(2000, 12, 31, 200)
        print "Output:\n\t%02d:%02d:%02d\n-----" % solve(ex)

# End of File #

コードの説明

実装する前に書いた作業メモで許してください.

まず、生まれた日から a年から死ぬまでに何秒の時間を過ごしたのか?
死んだ日の定義があいまいだったから、定義しておく.
    e.g) a=1950, b=1, c=1, n=20
        1950年 1/1に生まれる
        51歳になるぎりぎりまで生きる. 2000年12/31で死ぬ. => 51年間生きるという計算

    1950-1/1 ~ 2001/1/1 まで何秒? => X秒
    1950-1/1 ~ 今日まで何秒? => Y秒

    <人生を一日に換算する>
        このX秒を 一日 60 * 60  * 24  とした時に、
        X: 60*60*24 = Y : Z の比の問題を解く.
        <=>     Z = (60*60*24 * Y) / X
        といった計算で人生を一日に換算して、0時0分0秒からZ秒経過した時の時刻を求める.

Ouput

Input:
        birth date(year/month/day): 1990/1/1
        death age: 80
Output:
        06:37:44
-----
Input:
        birth date(year/month/day): 2000/11/10
        death age: 80
Output:
        03:22:17
-----
Input:
        birth date(year/month/day): 1900/1/1
        death age: 200
Output:
        13:27:05
-----
Input:
        birth date(year/month/day): 1987/12/11
        death age: 200
Output:
        02:53:55
-----
Input:
        birth date(year/month/day): 2000/12/31
        death age: 200
Output:
        01:19:54
-----

感想等

テストコード書いてないです。明日余裕があれば、第二問解く予定。