開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 12章(新しいクラスのオブジェクト), 12.6(練習問題の続き)バースデーヘルパー を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
バースデーヘルパー
コード(BBEdit)
sample.rb
#!/usr/bin/env ruby1.9 # -*- coding: utf-8 -*- birth_dates = {} name = 'birth_day_helper' File.open(name).each_line do |line| line = line.chomp name, mon_day, year = line.split(",") mon, day = mon_day.split birth_dates[name.strip] = {:year => year.to_i, :mon => mon.strip, :day => day.strip.to_i} end while true print "名前を入力: " name = gets.chomp break if name =~ /^\s*$/ birth_date = birth_dates[name] next unless birth_date birth = Time.gm(birth_date[:year], birth_date[:mon], birth_date[:day]) age = 0 td = Time.new while Time.gm(birth.year + age, birth.mon, birth.day) <= td age += 1 end age -= 1 puts "誕生日:#{birth.year}-#{birth.mon}-#{birth.day} 年齢:#{age}歳" end
入出力結果(Terminal)
$ ./sample.rb 名前を入力: The King of Spain 誕生日:2000-12-30 年齢:11歳 名前を入力: Christopher Lloyd 誕生日:2000-12-29 年齢:12歳 名前を入力: Christopher Plummer 誕生日:2000-12-28 年齢:12歳 名前を入力: kamimura 名前を入力: $
ちなみにJavaScriptの場合。
コード(BBEdit)
var result = ""; var birth_day_helper = 'Christopher Alexander, Oct 18, 1936\n' + 'Christopher Lambert, Mar 29, 1957\n' + 'Christopher Lee, Oct 18, 1950\n' + 'Christopher Pine, Nov 20, 2020\n' + 'Christopher Plummer, Dec 28, 2000\n' + 'Christopher Lloyd, Dec 29, 2000\n' + 'The King of Spain, Dec 30, 2000'; var mon_to_i = {"Jan":1,"Feb":2,"Mar":3,"Apr":4, "May":5,"Jun":6,"Jul":4,"Aug":8, "Sep":9,"Oct":10, "Nov":11,"Dec":12} var birth_dates = {}; var lines = birth_day_helper.split("\n"); for(var i = 0; i < lines.length; i++){ var name_and_mon_day_and_year = lines[i].split(","); var name = name_and_mon_day_and_year[0].trim(); var mon_day = name_and_mon_day_and_year[1].trim().split(/\s+/); var mon = mon_day[0].trim(); var day = mon_day[1].trim(); var year = name_and_mon_day_and_year[2]; birth_dates[name] = new Date(year.trim(), mon_to_i[mon.trim()] - 1, day.trim()); } var name = $('#t0').val(); if(! birth_dates[name]){ result += "リストに名前がありません。\n"; } else { var birth_date = birth_dates[name]; var age = 0; var td = new Date(); var date= new Date(birth_date.getYear() + 1900, birth_date.getMonth(), birth_date.getDate()); while(date <= td){ age += 1; date.setYear(date.getYear() + 1900 + 1); } age -= 1; result += "誕生日:" + birth_date + " 年齢:" + age + "歳\n"; } $('#pre0').text(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 # -*- coding: utf-8 -*- import datetime, re mon_to_i = {"Jan":1,"Feb":2,"Mar":3,"Apr":4, "May":5,"Jun":6,"Jul":4,"Aug":8, "Sep":9,"Oct":10, "Nov":11,"Dec":12} birth_dates = {} for line in open('birth_day_helper'): name, mon_day, year = line.split(",") mon, day = mon_day.split() birth_dates[name.strip()] = datetime.date(int(year), mon_to_i[mon.strip()], int(day)) while True: name = input("名前を入力: ") if re.match(r"^\s*$", name): break if not name in birth_dates: continue birth_date = birth_dates[name] date = birth_date age = 0 td = datetime.date.today() while date <= td: age += 1 date = date.replace(year= birth_date.year + age) else: age -= 1 print("誕生日:{0} 年齢:{1}歳".format(birth_date, age))
入出力結果(Terminal)
$ ./sample.py 名前を入力: Christopher Plummer 誕生日:2000-12-28 年齢:12歳 名前を入力: Christopher Lloyd 誕生日:2000-12-29 年齢:12歳 名前を入力: The King of Spain 誕生日:2000-12-30 年齢:11歳 名前を入力: kamimura 名前を入力: $
0 コメント:
コメントを投稿