開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 12章(章全部で復習), 12.5(練習問題の続き)バースデーヘルパー を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
バースデーヘルパー
コード(BBEdit)
sample.rb
#!/usr/bin/env ruby1.9 #-*- coding: utf-8 -*- birth_day = {} td = Time.new File.open("birth_day_helper").each_line do |line| name, mon_day, year = line.split(",") mon, day = mon_day.split birth_day[name] = Time.gm(year.to_i, mon, day.to_i) end while true print "名前を入力(空文字で終了): " name = gets.chomp break if name =~ /^\s*$/ b = birth_day[name] next unless birth_day[name] age = td.year - b.year age -= 1 if td.mon < b.mon or (td.mon == b.mon and td.day < b.day) puts "誕生日: #{b.year}年#{b.mon}月#{b.day}日 #{age}歳" end
入出力結果(Terminal)
$ ./sample.rb 名前を入力(空文字で終了): Christopher Lambert 誕生日: 2000年3月12日 13歳 名前を入力(空文字で終了): Christopher Lee 誕生日: 2000年3月13日 13歳 名前を入力(空文字で終了): Christopher Pine 誕生日: 2000年3月14日 12歳 名前を入力(空文字で終了): ruby 名前を入力(空文字で終了): $ Christopher Alexander, Oct 18, 1936 Christopher Lambert, Mar 12, 2000 Christopher Lee, Mar 13, 2000 Christopher Pine, Mar 14, 2000 Christopher Plummer, Feb 4, 2000 Christopher Lloyd, Feb 5, 2000 The King of Spain, Feb 6, 2000 $
ちなみにJavaScriptの場合。
名前: 誕生日: 年齢:歳
コード(BBEdit)
HTML
<p>名前: <label> <select id="names_birth" onkeyup="try{ var text = $('#pre1').text(); eval( text ); } catch (e) { alert(e); }"> <option>Christopher Alexander</option> <option>Christopher Lambert</option> <option>Christopher Lee</option> <option>Christopher Pine</option> <option>Christopher Plummer</option> <option>Christopher Lloyd</option> <option>The King of Spain</option> </select> </label> 誕生日:<span id="b_day"></span> 年齢:<span id="b_age"></span>歳</p>
JavaScript
(function () { var s = 'Christopher Alexander, Oct 18, 1936\nChristopher Lambert, Mar 12, 2000\nChristopher Lee, Mar 13, 2000\nChristopher Pine, Mar 14, 2000\nChristopher Plummer, Feb 4, 2000\nChristopher Lloyd, Feb 5, 2000\nThe King of Spain, Feb 6, 2000\n', lines = s.trim().split("\n"), birth_day = {}, m_i = {'Jan':1,'Feb':2,'Mar':3,'Apr':4,'May':5,'Jun':6, 'Jul':7,'Aug':8,'Sep':9,'Oct':10,'Nov':11,'Dec':12}, td = new Date(), $select = $('#names_birth'), $name = $('option:selected', $select).text(), result = "", i, max, line, name, year, month, day, mon_day, age, b; for (i = 0, max = lines.length; i < max; i += 1) { line = lines[i].split(","); name = line[0].trim(); mon_day = line[1].trim().split(" "); month = m_i[mon_day[0].trim()]; day = mon_day[1]; year = line[2].trim(); birth_day[name] = new Date(year, month - 1, day); } b = birth_day[$name]; age = td.getFullYear() - b.getFullYear(); if ( (td.getMonth() < b.getMonth()) || ((td.getMonth() === b.getMonth()) && (td.getDate() < b.getDate()))) { age -= 1; } $('#b_day').text(b.getFullYear() + "年" + (b.getMonth() + 1) + "月" + b.getDate() + "日"); $('#b_age').text(age); })();
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- import datetime, re birth_day = {} m_i = {'Jan':1,'Feb':2,'Mar':3,'Apr':4,'May':5,'Jun':6, 'Jul':7,'Aug':8,'Sep':9,'Oct':10,'Nov':11,'Dec':12} td = datetime.date.today() with open('birth_day_helper') as f: for line in f: name, mon_day, year = line.split(",") mon, day = mon_day.split() mon = m_i[mon] birth_day[name] = datetime.date(int(year), int(mon), int(day)) while True: name = input("名前を入力(空白で終了): ") if name in birth_day: b = birth_day[name] age = td.year - b.year if td.month < b.month or (td.month == b.month and td.day < b.day): age -= 1 print("誕生日:{0} 年齢:{1}歳".format(b, age)) elif re.search(r"^\s*$", name): break
入出力結果(Terminal)
$ ./sample.py 名前を入力(空白で終了): Christopher Lambert 誕生日:2000-03-12 年齢:13歳 名前を入力(空白で終了): Christopher Lee 誕生日:2000-03-13 年齢:13歳 名前を入力(空白で終了): Christopher Pine 誕生日:2000-03-14 年齢:12歳 名前を入力(空白で終了): python 名前を入力(空白で終了): $
perlの場合。
sample.pl
コード(BBEdit)
#!/usr/bin/env perl use strict; use warnings; use utf8; use 5.016; binmode STDIN, ":utf8"; binmode STDOUT, ":utf8"; my %birth_day = (); my($day, $month, $year) = (localtime)[3,4,5]; my %m_i = ('Jan' => 1,'Feb' => 2,'Mar' => 3,'Apr' => 4,'May' => 5,'Jun' => 6, 'Jul' => 7,'Aug' => 8,'Sep' => 9,'Oct' => 10,'Nov' => 11,'Dec' => 12); open my $fh, "<", "birth_day_helper" or die; while (<$fh>) { chomp; my($name, $mon_day, $year) = split ",", $_; my($mon, $day) = split " ", $mon_day; $mon = $m_i{$mon}; $birth_day{$name} = [$day, $mon, $year]; } close $fh; while (1) { print "名前を入力: "; chomp(my $name = <STDIN>); last if $name =~ /^\s*$/; my $b = $birth_day{$name}; next unless $b; my $age = $year + 1900 - $b->[2]; $age -= 1 if $month + 1 < $b->[1] or ($month + 1 == $b->[1] and $day < $b->[0]); printf "誕生日:%d年%d月%d日 年齢:%d歳\n", $b->[2], $b->[1], $b->[0], $age; }
入出力結果(Terminal)
$ ./sample.pl 名前を入力: Christopher Lambert 誕生日:2000年3月12日 年齢:13歳 名前を入力: Christopher Lee 誕生日:2000年3月13日 年齢:13歳 名前を入力: Christopher Pine 誕生日:2000年3月14日 年齢:12歳 名前を入力: perl 名前を入力: $
0 コメント:
コメントを投稿