開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 9章(自作メソッドの書き方), 9.5(練習問題)モダンなローマ数字 を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
モダンなローマ数字
コード(BBEdit)
sample.rb
#!/usr/bin/env ruby1.9 #-*- coding: utf-8 -*- def roman_numeral n result = 'M' * (n / 1000) n = n % 1000 h = n / 100 n = n % 100 t = n / 10 n = n % 10 o = n if h == 9 result += 'CM' elsif h == 4 result += 'CD' else result += 'D' * (h / 5) result += 'C' * (h % 5) end if t == 9 result += 'XC' elsif t == 4 result += 'XL' else result += 'L' * (t / 5) result += 'X' * (t % 5) end if o == 9 result += 'IX' elsif o == 4 result += 'IV' else result += 'V' * (o / 5) result += 'I' * (o % 5) end result end puts roman_numeral 4 puts roman_numeral 1234
入出力結果(Terminal)
$ ./sample.rb IV MCCXXXIV $
ちなみにJavaScriptの場合。
コード(BBEdit)
var n = parseInt($('#t0').val(), 10), romanNumeral = function( n ) { var result = new Array(Math.floor(n / 1000) + 1).join('M'), h, t, o; n %= 1000; h = Math.floor( n / 100); n %= 100; t = Math.floor(n / 10); n %= 10; o = n; switch ( h ) { case 9: result += 'CM'; break; case 4: result += 'CD'; break; default: result += new Array(Math.floor(h / 5) + 1).join('D'); result += new Array(h % 5 + 1).join('C'); } switch ( t ) { case 9: result += 'XC'; break; case 4: result += 'XL'; break; default: result += new Array(Math.floor( t / 5) + 1).join('L'); result += new Array(Math.floor( t % 5 ) + 1).join('X'); } switch ( o ) { case 9: result += 'IX'; break; case 4: result += 'IV'; break; default: result += new Array(Math.floor( o / 5 ) + 1).join('V'); result += new Array(Math.floor( o % 5 ) + 1).join('I'); } return result; }; $('#pre0').text(romanNumeral(n));
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- def romanNumeral( n ): result = 'M' * (n // 1000) n %= 1000 h = n // 100 n %= 100 t = n // 10 n %= 10 o = n if h == 9: result += 'CM' elif h == 4: result += 'CD' else: result += 'D' * (h // 5) result += 'C' * (h % 5) if t == 9: result += 'XC' elif t == 4: result += 'XL' else: result += 'L' * ( t // 5 ) result += 'X' * (t % 5) if o == 9: result += 'IX' elif o == 4: result += 'IV' else: result += 'V' * (o // 5) result += 'I' * (o % 5) return result for x in [4, 1234]: print(romanNumeral(x))
入出力結果(Terminal)
$ ./sample.py IV MCCXXXIV $
perlの場合。
sample.pl
コード(BBEdit)
#!/usr/bin/env perl use strict; use warnings; use utf8; use 5.016; binmode STDIN, ":utf8"; binmode STDOUT, ":utf8"; use POSIX; sub romanNumeral { my $n = shift; my $result = 'M' x floor($n / 1000); $n = $n % 1000; my $h = floor( $n / 100); $n = $n % 100; my $t = floor( $n / 10 ); $n = $n % 10; my $o = $n; if ($h == 9) { $result .= 'CM'; } elsif ($h == 4) { $result .= 'CD'; } else { $result .= 'D' x floor($h / 5); $result .= 'C' x ($h % 5); } if ($t == 9) { $result .= 'XC'; } elsif ($t == 4) { $result .= 'XL' } else { $result .= 'L' x floor( $t / 5 ); $result .= 'X' x ($t % 5); } if ($o == 9) { $result .= 'IX'; } elsif ($o == 4) { $result .= 'IV'; } else { $result .= 'V' x floor($o / 5); $result .= 'I' x ($o % 5); } $result; } for ((4,1234)) { printf "%s\n", romanNumeral $_; }
入出力結果(Terminal)
$ ./sample.pl IV MCCXXXIV $
0 コメント:
コメントを投稿