2013年3月25日月曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 2章(数値), 2.5(練習問題)この本の著者の年齢 を解いてみる。

その他参考書籍

この本の著者の年齢

コード(BBEdit)

sample.rb

#!/usr/bin/env ruby2.0
#-*- coding: utf-8 -*-

t = Time.new
b = t - 1.025 * (10 ** 9)

age = t.year - b.year
if b.month < t.month or (b.month == t.month and t.day < t.day)
    age -= 1
end
puts age

入出力結果(Terminal)

$ ./sample.rb
33
$

ちなみにJavaScriptの場合。

コード(BBEdit)

var d = new Date(),
    b = new Date(),
    age;
b.setSeconds( d.getSeconds() - 1.025 * Math.pow(10, 9));
age = d.getFullYear() - b.getFullYear();
if ( b.getMonth() < d.getMonth() ||
    (b.getMonth() === d.getMonth() && b.getDate() < d.getDate())) {
    age -= 1;
}
$('#pre0').text(age);



pythonの場合。

sample.py

コード(BBEdit)

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import datetime

t = datetime.datetime.today()
b = datetime.datetime.fromtimestamp(t.timestamp() - 1.025 * (10 ** 9))
age = t.year - b.year
if b.month < t.month or (b.month == t.month and b.day < t.day):
    age -= 1
print(age)

入出力結果(Terminal)

$ ./sample.py
33
$

0 コメント:

コメントを投稿