SONY Go For It: 1) の解答

解答

life_clock.rb

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# FILE: life_clock.rb
# AUTHOR: rhysd (http://d.hatena.ne.jp/rhysd/)
# License: MIT license  {{{
#     Permission is hereby granted, free of charge, to any person obtaining
#     a copy of this software and associated documentation files (the
#     "Software"), to deal in the Software without restriction, including
#     without limitation the rights to use, copy, modify, merge, publish,
#     distribute, sublicense, and/or sell copies of the Software, and to
#     permit persons to whom the Software is furnished to do so, subject to
#     the following conditions:
#
#     The above copyright notice and this permission notice shall be included
#     in all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#     IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
#     CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
#     TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# }}}

require 'bigdecimal'

SECONDS_OF_DAY = 86400

def life_in_24hours year,month,day,lifetime
    from = Time.gm  year,month,day
    to   = Time.gm(year+lifetime,month,day) - 1
    now  = Time.now

    raise "lifetime is invalid!!" if from > to
    raise "Time over!!" if now > to

    life_seconds = BigDecimal.new((to - from).to_s)
    now_seconds = BigDecimal.new((now - from).to_s)
    now_of_the_day = now_seconds / life_seconds * SECONDS_OF_DAY

    result_time = Time.gm(0) + now_of_the_day
    result_time.strftime "%Hh %Mm %Ss"
end

#
# main
#
if __FILE__ == $0 then

    print "i) a b c n="
    a,b,c,n = STDIN.gets.split(" ").map{|i| i.to_i}
    puts life_in_24hours a,b,c,n

    puts

    print "ii) a b c n="
    a,b,c,n = STDIN.gets.split(" ").map{|i| i.to_i}
    puts life_in_24hours a,b,c,n

end

test_life_clock.rb

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# FILE: test_life_clock.rb
# AUTHOR: rhysd (http://d.hatena.ne.jp/rhysd/)
# License: MIT license  {{{
#     Permission is hereby granted, free of charge, to any person obtaining
#     a copy of this software and associated documentation files (the
#     "Software"), to deal in the Software without restriction, including
#     without limitation the rights to use, copy, modify, merge, publish,
#     distribute, sublicense, and/or sell copies of the Software, and to
#     permit persons to whom the Software is furnished to do so, subject to
#     the following conditions:
#
#     The above copyright notice and this permission notice shall be included
#     in all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#     IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
#     CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
#     TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# }}}

require './life_clock.rb'
require 'minitest/unit'

class TestLifeClock < MiniTest::Unit::TestCase

    def test_method_respond
        assert_respond_to(main, :life_in_24hours, "doesn't have life_in_24hours method")
    end

    def test_invalid_value
        [1, 10, 21].each do |life|
            assert_raise(RuntimeError){ life_in_24hours 1990,9,29,life }
        end
        assert_raise(RuntimeError){ life_in_24hours 1990,9,29,-30 }
    end

    def test_value_pattern
        [[1900,200],
         [1950,200],
         [2000,200],
         [1990,40],
         [1990,80],
         [2000,40],
         [2000,80]].each do |y,l|
            assert_match(/^[0-2][0-9]h [0-6][0-9]m [0-6][0-9]s$/,
                         result = life_in_24hours(y,9,29,l),
                         "invalid return value: #{result}")
        end
    end

end

コードの中にもある通り,ライセンスはMIT Licenseです.
今回は簡単な単体テストも書いてみました.

Fixnumでは値が溢れてしまうので,BigDecimalを使っています.