2012年11月23日金曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 12章(新しいクラスのオブジェクト), 12.6(練習問題の続き)バースデーヘルパー を解いてみる。

その他参考書籍

バースデーヘルパー

コード(TextWrangler)

sample.rb

#!/usr/bin/env ruby1.9
# -*- coding: utf-8 -*-

birth_dates = {}
filename = 'birth_day_helper'
File.readlines(filename).each do |line|
  name, date, year = line.split(",")
  month, day = date.split
  birth_dates[name] = [year.to_i, month, day.to_i]
end

while true
  print "名前を入力(空白で終了): "
  name = gets.chomp
  break if name =~ /^\s*$/
  birth_date = birth_dates[name.strip]
  if birth_date
    age = 0
    td = Time.new
    while Time.gm(birth_date[0] + age, birth_date[1], birth_date[2]) <= td
      age += 1
    end
    age -= 1
    if age < 0
      puts "ファイルに記載ミスがありました。"
      next
    end
    date = Time.gm(birth_date[0], birth_date[1], birth_date[2])
    puts "誕生日:#{date.year}年#{date.month}月#{date.day}日 年齢:#{age}歳"
  else
    puts "その人の誕生年月日はファイルにありません。"
  end
end

入出力結果(Terminal)

$ ./sample.rb
名前を入力(空白で終了): Christopher Pine
ファイルに記載ミスがありました。
名前を入力(空白で終了): Christopher Plummer
誕生日:2000年11月23日 年齢:12歳
名前を入力(空白で終了): Christopher Lloyd
誕生日:1950年10月24日 年齢:62歳
名前を入力(空白で終了): The King of Spain
誕生日:2000年11月25日 年齢:11歳
名前を入力(空白で終了): kamimura
その人の誕生年月日はファイルにありません。
名前を入力(空白で終了): 
$ cat birth_day_helper
Christopher Alexander,  Oct  18, 1936
Christopher Lambert,    Mar 29, 1957
Christopher Lee,        Oct 18, 1950
Christopher Pine,       Nov 20, 2020
Christopher Plummer,    Nov 23, 2000
Christopher Lloyd,      Oct 24, 1950
The King of Spain,      Nov 25, 2000$

ちなみにJavaScriptの場合。

コード(TextWrangler)

var months = {
  "Jan":1, "Feb":2,"Mar":3,"Apr":4,
  "May":5,"Jun":6,"Jul":7,"Aug":8,
  "Sep":9,"Oct":10,"Nov":11,"Dec":12
  };
var birth_day_helper= 'Christopher Alexander,  Oct  18, 1936\nChristopher Lambert,    Mar 29, 1957\nChristopher Lee,        Oct 18, 1950\nChristopher Pine,       Nov 20, 2020\nChristopher Plummer,    Nov 23, 2000\nChristopher Lloyd,      Oct 24, 1950\nThe King of Spain,      Nov 25, 2000';
var birth_dates = {};
var lines = birth_day_helper.split("\n");
for(var i = 0; i < lines.length; i++){
  var items = lines[i].split(",");
  var name = items[0].trim();
  var year = items[2].trim();
  var m_d = items[1].trim().split(" ");
  var month = m_d.shift();
  month = months[month];
  var day = m_d.pop()
  birth_dates[name] = new Date(year, month - 1, day);
}
var result;
var name = $('#t0').val();
var birth_date = birth_dates[name];
if(birth_date){
  var age = 0;
  var td = new Date();
  // 後でbirth_dateを使いたいのでコピーではなく新しいDateオブジェクトを作成
  // 作成しておかないと、次のsetYearで値が変わってしまう
  var date = new Date(1900 + birth_date.getYear(), birth_date.getMonth(), birth_date.getDate());
  while(date.setYear(1900 + birth_date.getYear() + age) <= td){
    age++;
  }
  age--;
  if(age < 0){
    result = "ファイルに記載ミスがありました\n";
  } else {
    result = "誕生日:" + (1900 + birth_date.getYear()) + "年" + 
      (birth_date.getMonth() + 1) + "月" +
      birth_date.getDate() + "日 年齢:" + age + "歳\n";
  }
} else {
  result = "その人の誕生年月日はファイルにありません\n";
}
$('#pre0').text(result);




pythonの場合。

sample.py

コード(TextWrangler)

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

import datetime

def month_s_to_i(s):
    d = {'Jan':1,'Feb':2,'Mar':3,'Apr':4,
         'May':5,'Jun':6,'Jul':7,'Aug':8,
         'Sep':9,'Oct':10,'Nov':11,'Dec':12}
    return d[s]

filename = 'birth_day_helper'
birth_dates = {}
for line in open(filename):
    name, date, year = line.split(",")
    month, day = date.split()
    birth_dates[name] = datetime.date(int(year), month_s_to_i(month), int(day))

import re

pattern = re.compile(r"^\s*$")
while True:
    print("名前を入力(空白で終了): ", end="")
    name = input()
    if re.match(pattern, name): break
    if not name in birth_dates:
        print("その人の誕生年月日はファイルにありません")
        continue
    birth_date = birth_dates[name.strip()]
    age = 0
    td = datetime.date.today()
    while birth_date.replace(year = birth_date.year + age) <= td:
        age += 1
    else:
        age -= 1
    if age < 0:
        print("ファイルに記載ミスがありました。")
        continue
    print("誕生日:{date} 年齢:{age}歳".format(
      date = birth_date.strftime("%Y年%m月%d日"), age = age))

入出力結果(Terminal)

$ ./sample.py
名前を入力(空白で終了): Christopher Pine
ファイルに記載ミスがありました。
名前を入力(空白で終了): Christopher Plummer
誕生日:2000年11月23日 年齢:12歳
名前を入力(空白で終了): Christopher Lloyd
誕生日:1950年10月24日 年齢:62歳
名前を入力(空白で終了): The King of Spain
誕生日:2000年11月25日 年齢:11歳
名前を入力(空白で終了): kamimura
その人の誕生年月日はファイルにありません
名前を入力(空白で終了): 
$

0 コメント:

コメントを投稿