開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 2章(数値), 2.5(練習問題)この本の著者の年齢 を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
この本の著者の年齢
コード(BBEdit)
sample.rb
#!/usr/bin/env ruby1.9 #-*- coding: utf-8 -*- t = Time.now b = t - 1.025 * (10 ** 9) b_year = b.year b_month = b.month b_day = b.day age = 0 while Time.local(b_year + age, b_month, b_day) <= t age += 1 end age -= 1 puts age
入出力結果(Terminal)
$ ./sample.rb 32 $
ちなみにJavaScriptの場合。
コード(BBEdit)
var td = new Date(),
b = new Date(),
age = 0,
b_year, b_month, b_date;
b.setSeconds(td.getSeconds() - 1.025 * Math.pow(10, 9));
b_year = b.getFullYear();
b_month = b.getMonth();
b_date = b.getDate();
while ( new Date(b_year + age, b_month, b_date) <= td ) {
age += 1;
}
age -= 1;
$('#pre0').text(age);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
import datetime
td = datetime.datetime.now()
b_timestamp = td.timestamp() - 1.025 * (10 ** 9)
b = datetime.datetime.fromtimestamp(b_timestamp)
age = 0
while b <= td:
b = b.replace(year=b.year + 1)
age += 1
age -= 1
print(age)
入出力結果(Terminal)
$ ./sample.py 32 $
perlの場合。
sample.pl
コード(BBEdit)
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
use Time::Local;
my $td_timestamp = timelocal(localtime);
my $b_timestamp = $td_timestamp - 1.025 * (10 ** 9);
my($bday, $bmon, $byear) = (localtime($b_timestamp))[3,4,5];
my $age = 0;
while ($b_timestamp <= $td_timestamp) {
$age += 1;
$b_timestamp = timelocal(0, 0, 0, $bday, $bmon, $byear + $age);
}
$age -= 1;
say $age;
入出力結果(Terminal)
a$ ./sample.pl 32 $
0 コメント:
コメントを投稿