Created
February 13, 2012 01:13
-
-
Save mugenen/1812417 to your computer and use it in GitHub Desktop.
SONY GO FOR IT1(http://www.sony.co.jp/SonyInfo/Jobs/newgrads/sus/q01.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
u"""人生を24時間換算したときの今日の時間を表示 | |
This is released under the MIT License(http://www.opensource.org/licenses/mit-license.php). | |
Copyright (c) 2012 Satoru Ogasawara | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
""" | |
import datetime | |
import math | |
import sys | |
def deltaOfDays(start, end): | |
u"""2つの日付の間の日数 | |
日付が小さすぎる | |
>>> deltaOfDays(datetime.date(-1, 4, 2), datetime.date(2012, 4, 2)) | |
Traceback (most recent call last): | |
... | |
ValueError: year is out of range | |
>>> deltaOfDays(datetime.date(1, 14, 2), datetime.date(3, -4, 2)) | |
Traceback (most recent call last): | |
... | |
ValueError: month must be in 1..12 | |
>>> deltaOfDays(datetime.date(1, 1, 2), datetime.date(3, 4, -2)) | |
Traceback (most recent call last): | |
... | |
ValueError: day is out of range for month | |
日付が大きすぎる | |
>>> deltaOfDays(datetime.date(10000, 4, 2), datetime.date(10003, 4, 2)) | |
Traceback (most recent call last): | |
... | |
ValueError: year is out of range | |
>>> deltaOfDays(datetime.date(1, 14, 2), datetime.date(3, 4, 2)) | |
Traceback (most recent call last): | |
... | |
ValueError: month must be in 1..12 | |
>>> deltaOfDays(datetime.date(1, 1, 200), datetime.date(3, 4, 2)) | |
Traceback (most recent call last): | |
... | |
ValueError: day is out of range for month | |
1年生きた時の日数(閏年でない) | |
>>> deltaOfDays(datetime.date(2001, 1, 1), datetime.date(2002, 1, 1)).days | |
365 | |
1年生きた時の日数(閏年) | |
>>> deltaOfDays(datetime.date(2000, 1, 1), datetime.date(2001, 1, 1)).days | |
366 | |
4年生きた時の日数(閏年) | |
>>> deltaOfDays(datetime.date(2000, 1, 1), datetime.date(2004, 1, 1)).days | |
1461 | |
100年生きた時の日数(100で割れて400で割れないときは閏年でない) | |
>>> deltaOfDays(datetime.date(2100, 1, 1), datetime.date(2200, 1, 1)).days | |
36524 | |
400年生きた時の日数(100で割れて400で割れないときは閏年でない) | |
>>> deltaOfDays(datetime.date(2000, 1, 1), datetime.date(2400, 1, 1)).days | |
146097 | |
""" | |
delta = end - start | |
return delta | |
def numberOfDays(start, n): | |
u"""startからn歳まで生きるときに,24時間換算で今日はいつか(秒未満は切り捨て) | |
nが正の整数でない | |
>>> numberOfDays(datetime.date(3, 4, 2), -1) | |
Traceback (most recent call last): | |
... | |
ValueError: n must be positive integer | |
生まれた日と寿命の日の間に今日がない | |
>>> numberOfDays(datetime.date(3, 4, 2), 1) | |
Traceback (most recent call last): | |
... | |
ValueError: today must be between start and end | |
>>> today = datetime.date.today() | |
#今日が生まれた日の場合 | |
>>> numberOfDays(datetime.date(today.year, today.month, today.day), 0) | |
0時0分0秒 | |
#死んだ日が今日の場合 | |
>>> numberOfDays(datetime.date(today.year - 1, today.month, today.day), 0) | |
24時0分0秒 | |
#2011年に生まれて1歳まで生きる(2年間)の場合の今日(一年生きた場合),閏年なので12時ではない | |
#2月29日以降はテストに失敗する | |
>>> numberOfDays(datetime.date(today.year - 1, today.month, today.day), 1) | |
11時59分0秒 | |
""" | |
if n < 0 or math.floor(n) != n: | |
raise ValueError("n must be positive integer") | |
end = datetime.date(start.year + n + 1, start.month, start.day) | |
today = datetime.date.today() | |
#日付の差分をとる | |
d1 = deltaOfDays(start, today) | |
d2 = deltaOfDays(start, end) | |
d3 = deltaOfDays(today, end) | |
#start, endと今日の前後関係が正しいかチェック | |
if d1 != abs(d1) or d3 != abs(d3): | |
raise ValueError("today must be between start and end") | |
#全体の日数で今日までの日数を割る(得られる単位は秒と日) | |
result = (d1 / d2.days).seconds | |
result2 = (d1 / d2.days).days | |
if result2 == 0: | |
seconds = result % 60 | |
minute = result / 60 % 60 | |
hour = result / 60 / 60 | |
print unicode(hour) + u"時" + unicode(minute) + u"分" + unicode(seconds) + u"秒" | |
else: | |
print u"24時0分0秒" | |
def _test(): | |
import doctest | |
doctest.testmod() | |
if __name__ == "__main__": | |
if len(sys.argv) == 4: | |
a1 = int(sys.argv[1]) | |
a2 = int(sys.argv[2]) | |
n = int(sys.argv[3]) | |
for i in xrange(a1, a2 + 1): | |
#1月1日を誕生日とする | |
numberOfDays(datetime.date(i, 1, 1), n) | |
else: | |
_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment