開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 10章(章全部で復習), 10.5(練習問題の続き)「壁にビールが99本」 を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
「壁にビールが99本」
コード(BBEdit)
sample.rb
#!/usr/bin/env ruby1.9 #-*- coding: utf-8 -*- def english_number num return "自然数(0を含む)を入力してください" if num < 0 return 'zero' if num == 0 result = "" ones = %w[one two three four five six seven eight nine] tens_place = %w[eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen] tens = %w[ten twenty thirty forty fifty sixty seventy eighty ninety] others = [ [2, 'hundred'], [3, 'thousand'], [6, 'million'], [9, 'billion'], [12, 'trillion'], [15, 'quandrillion'], [18, 'quintillion'], [21, 'sextillion'], [24, 'septillion']] left = num while others.length > 0 other = others.pop write = left / (10 ** other[0]) left = left - write * (10 ** other[0]) if write > 0 pre = english_number write result = result + pre + ' ' + other[1] if left > 0 if other[1] != 'hundred' result += ', ' else result += ' ' end end end end write = left / 10 left = left - write * 10 if write > 0 if (write == 1) and (left > 0) result += tens_place[left - 1] left = 0 else result += tens[write - 1] result += '-' if left > 0 end end write = left left = 0 result += ones[write - 1] if write > 0 result end puts english_number 123123123
入出力結果(Terminal)
$ ./sample.rb one thousand, one hundred eleven bottles of beer on the wall, one thousand, one hundred eleven bottles of beer! Take one down, pass it aorund, one thousand, one hundred ten bottles of beer on the wall! one thousand, one hundred ten bottles of beer on the wall, one thousand, one hundred ten bottles of beer! Take one down, pass it aorund, one thousand, one hundred nine bottles of beer on the wall! one thousand, one hundred nine bottles of beer on the wall, one thousand, one hundred nine bottles of beer! Take one down, pass it aorund, one thousand, one hundred eight bottles of beer on the wall! one thousand, one hundred eight bottles of beer on the wall, one thousand, one hundred eight bottles of beer! Take one down, pass it aorund, one thousand, one hundred seven bottles of beer on the wall! one thousand, one hundred seven bottles of beer on the wall, one thousand, one hundred seven bottles of beer! Take one down, pass it aorund, one thousand, one hundred six bottles of beer on the wall! one thousand, one hundred six bottles of beer on the wall, one thousand, one hundred six bottles of beer! Take one down, pass it aorund, one thousand, one hundred five bottles of beer on the wall! one thousand, one hundred five bottles of beer on the wall, one thousand, one hundred five bottles of beer! Take one down, pass it aorund, one thousand, one hundred four bottles of beer on the wall! one thousand, one hundred four bottles of beer on the wall, one thousand, one hundred four bottles of beer! Take one down, pass it aorund, one thousand, one hundred three bottles of beer on the wall! one thousand, one hundred three bottles of beer on the wall, one thousand, one hundred three bottles of beer! Take one down, pass it aorund, one thousand, one hundred two bottles of beer on the wall! one thousand, one hundred two bottles of beer on the wall, one thousand, one hundred two bottles of beer! Take one down, pass it aorund, one thousand, one hundred one bottles of beer on the wall! one thousand, one hundred one bottles of beer on the wall, one thousand, one hundred one bottles of beer! Take one down, pass it aorund, one thousand, one hundred bottles of beer on the wall! one thousand, one hundred bottles of beer on the wall, one thousand, one hundred bottles of beer! Take one down, pass it aorund, one thousand, ninety-nine bottles of beer on the wall! one thousand, ninety-nine bottles of beer on the wall, one thousand, ninety-nine bottles of beer! Take one down, pass it aorund, one thousand, ninety-eight bottles of beer on the wall! one thousand, ninety-eight bottles of beer on the wall, one thousand, ninety-eight bottles of beer! Take one down, pass it aorund, one thousand, ninety-seven bottles of beer on the wall! one thousand, ninety-seven bottles of beer on the wall, one thousand, ninety-seven bottles of beer! Take one down, pass it aorund, one thousand, ninety-six bottles of beer on the wall! one thousand, ninety-six bottles of beer on the wall, one thousand, ninety-six bottles of beer! Take one down, pass it aorund, one thousand, ninety-five bottles of beer on the wall! one thousand, ninety-five bottles of beer on the wall, one thousand, ninety-five bottles of beer! Take one down, pass it aorund, one thousand, ninety-four bottles of beer on the wall! one thousand, ninety-four bottles of beer on the wall, one thousand, ninety-four bottles of beer! Take one down, pass it aorund, one thousand, ninety-three bottles of beer on the wall! one thousand, ninety-three bottles of beer on the wall, one thousand, ninety-three bottles of beer! Take one down, pass it aorund, one thousand, ninety-two bottles of beer on the wall! one thousand, ninety-two bottles of beer on the wall, one thousand, ninety-two bottles of beer! Take one down, pass it aorund, one thousand, ninety-one bottles of beer on the wall! one thousand, ninety-one bottles of beer on the wall, one thousand, ninety-one bottles of beer! Take one down, pass it aorund, one thousand, ninety bottles of beer on the wall! one thousand, ninety bottles of beer on the wall, one thousand, ninety bottles of beer! Take one down, pass it aorund, one thousand, eighty-nine bottles of beer on the wall! one thousand, eighty-nine bottles of beer on the wall, one thousand, eighty-nine bottles of beer! Take one down, pass it aorund, one thousand, eighty-eight bottles of beer on the wall! one thousand, eighty-eight bottles of beer on the wall, one thousand, eighty-eight bottles of beer! Take one down, pass it aorund, one thousand, eighty-seven bottles of beer on the wall! one thousand, eighty-seven bottles of beer on the wall, one thousand, eighty-seven bottles of beer! Take one down, pass it aorund, one thousand, eighty-six bottles of beer on the wall! one thousand, eighty-six bottles of beer on the wall, one thousand, eighty-six bottles of beer! Take one down, pass it aorund, one thousand, eighty-five bottles of beer on the wall! one thousand, eighty-five bottles of beer on the wall, one thousand, eighty-five bottles of beer! Take one down, pass it aorund, one thousand, eighty-four bottles of beer on the wall! one thousand, eighty-four bottles of beer on the wall, one thousand, eighty-four bottles of beer! Take one down, pass it aorund, one thousand, eighty-three bottles of beer on the wall! one thousand, eighty-three bottles of beer on the wall, one thousand, eighty-three bottles of beer! Take one down, pass it aorund, one thousand, eighty-two bottles of beer on the wall! one thousand, eighty-two bottles of beer on the wall, one thousand, eighty-two bottles of beer! Take one down, pass it aorund, one thousand, eighty-one bottles of beer on the wall! one thousand, eighty-one bottles of beer on the wall, one thousand, eighty-one bottles of beer! Take one down, pass it aorund, one thousand, eighty bottles of beer on the wall! one thousand, eighty bottles of beer on the wall, one thousand, eighty bottles of beer! Take one down, pass it aorund, one thousand, seventy-nine bottles of beer on the wall! one thousand, seventy-nine bottles of beer on the wall, one thousand, seventy-nine bottles of beer! Take one down, pass it aorund, one thousand, seventy-eight bottles of beer on the wall! one thousand, seventy-eight bottles of beer on the wall, one thousand, seventy-eight bottles of beer! Take one down, pass it aorund, one thousand, seventy-seven bottles of beer on the wall! one thousand, seventy-seven bottles of beer on the wall, one thousand, seventy-seven bottles of beer! Take one down, pass it aorund, one thousand, seventy-six bottles of beer on the wall! one thousand, seventy-six bottles of beer on the wall, one thousand, seventy-six bottles of beer! Take one down, pass it aorund, one thousand, seventy-five bottles of beer on the wall! one thousand, seventy-five bottles of beer on the wall, one thousand, seventy-five bottles of beer! Take one down, pass it aorund, one thousand, seventy-four bottles of beer on the wall! one thousand, seventy-four bottles of beer on the wall, one thousand, seventy-four bottles of beer! Take one down, pass it aorund, one thousand, seventy-three bottles of beer on the wall! one thousand, seventy-three bottles of beer on the wall, one thousand, seventy-three bottles of beer! Take one down, pass it aorund, one thousand, seventy-two bottles of beer on the wall! one thousand, seventy-two bottles of beer on the wall, one thousand, seventy-two bottles of beer! Take one down, pass it aorund, one thousand, seventy-one bottles of beer on the wall! one thousand, seventy-one bottles of beer on the wall, one thousand, seventy-one bottles of beer! Take one down, pass it aorund, one thousand, seventy bottles of beer on the wall! one thousand, seventy bottles of beer on the wall, one thousand, seventy bottles of beer! Take one down, pass it aorund, one thousand, sixty-nine bottles of beer on the wall! one thousand, sixty-nine bottles of beer on the wall, one thousand, sixty-nine bottles of beer! Take one down, pass it aorund, one thousand, sixty-eight bottles of beer on the wall! one thousand, sixty-eight bottles of beer on the wall, one thousand, sixty-eight bottles of beer! Take one down, pass it aorund, one thousand, sixty-seven bottles of beer on the wall! one thousand, sixty-seven bottles of beer on the wall, one thousand, sixty-seven bottles of beer! Take one down, pass it aorund, one thousand, sixty-six bottles of beer on the wall! one thousand, sixty-six bottles of beer on the wall, one thousand, sixty-six bottles of beer! Take one down, pass it aorund, one thousand, sixty-five bottles of beer on the wall! one thousand, sixty-five bottles of beer on the wall, one thousand, sixty-five bottles of beer! Take one down, pass it aorund, one thousand, sixty-four bottles of beer on the wall! one thousand, sixty-four bottles of beer on the wall, one thousand, sixty-four bottles of beer! Take one down, pass it aorund, one thousand, sixty-three bottles of beer on the wall! one thousand, sixty-three bottles of beer on the wall, one thousand, sixty-three bottles of beer! Take one down, pass it aorund, one thousand, sixty-two bottles of beer on the wall! one thousand, sixty-two bottles of beer on the wall, one thousand, sixty-two bottles of beer! Take one down, pass it aorund, one thousand, sixty-one bottles of beer on the wall! one thousand, sixty-one bottles of beer on the wall, one thousand, sixty-one bottles of beer! Take one down, pass it aorund, one thousand, sixty bottles of beer on the wall! one thousand, sixty bottles of beer on the wall, one thousand, sixty bottles of beer! Take one down, pass it aorund, one thousand, fifty-nine bottles of beer on the wall! one thousand, fifty-nine bottles of beer on the wall, one thousand, fifty-nine bottles of beer! Take one down, pass it aorund, one thousand, fifty-eight bottles of beer on the wall! one thousand, fifty-eight bottles of beer on the wall, one thousand, fifty-eight bottles of beer! Take one down, pass it aorund, one thousand, fifty-seven bottles of beer on the wall! one thousand, fifty-seven bottles of beer on the wall, one thousand, fifty-seven bottles of beer! Take one down, pass it aorund, one thousand, fifty-six bottles of beer on the wall! one thousand, fifty-six bottles of beer on the wall, one thousand, fifty-six bottles of beer! Take one down, pass it aorund, one thousand, fifty-five bottles of beer on the wall! one thousand, fifty-five bottles of beer on the wall, one thousand, fifty-five bottles of beer! Take one down, pass it aorund, one thousand, fifty-four bottles of beer on the wall! one thousand, fifty-four bottles of beer on the wall, one thousand, fifty-four bottles of beer! Take one down, pass it aorund, one thousand, fifty-three bottles of beer on the wall! one thousand, fifty-three bottles of beer on the wall, one thousand, fifty-three bottles of beer! Take one down, pass it aorund, one thousand, fifty-two bottles of beer on the wall! one thousand, fifty-two bottles of beer on the wall, one thousand, fifty-two bottles of beer! Take one down, pass it aorund, one thousand, fifty-one bottles of beer on the wall! one thousand, fifty-one bottles of beer on the wall, one thousand, fifty-one bottles of beer! Take one down, pass it aorund, one thousand, fifty bottles of beer on the wall! one thousand, fifty bottles of beer on the wall, one thousand, fifty bottles of beer! Take one down, pass it aorund, one thousand, forty-nine bottles of beer on the wall! one thousand, forty-nine bottles of beer on the wall, one thousand, forty-nine bottles of beer! Take one down, pass it aorund, one thousand, forty-eight bottles of beer on the wall! one thousand, forty-eight bottles of beer on the wall, one thousand, forty-eight bottles of beer! Take one down, pass it aorund, one thousand, forty-seven bottles of beer on the wall! one thousand, forty-seven bottles of beer on the wall, one thousand, forty-seven bottles of beer! Take one down, pass it aorund, one thousand, forty-six bottles of beer on the wall! one thousand, forty-six bottles of beer on the wall, one thousand, forty-six bottles of beer! Take one down, pass it aorund, one thousand, forty-five bottles of beer on the wall! one thousand, forty-five bottles of beer on the wall, one thousand, forty-five bottles of beer! Take one down, pass it aorund, one thousand, forty-four bottles of beer on the wall! one thousand, forty-four bottles of beer on the wall, one thousand, forty-four bottles of beer! Take one down, pass it aorund, one thousand, forty-three bottles of beer on the wall! one thousand, forty-three bottles of beer on the wall, one thousand, forty-three bottles of beer! Take one down, pass it aorund, one thousand, forty-two bottles of beer on the wall! one thousand, forty-two bottles of beer on the wall, one thousand, forty-two bottles of beer! Take one down, pass it aorund, one thousand, forty-one bottles of beer on the wall! one thousand, forty-one bottles of beer on the wall, one thousand, forty-one bottles of beer! Take one down, pass it aorund, one thousand, forty bottles of beer on the wall! one thousand, forty bottles of beer on the wall, one thousand, forty bottles of beer! Take one down, pass it aorund, one thousand, thirty-nine bottles of beer on the wall! one thousand, thirty-nine bottles of beer on the wall, one thousand, thirty-nine bottles of beer! Take one down, pass it aorund, one thousand, thirty-eight bottles of beer on the wall! one thousand, thirty-eight bottles of beer on the wall, one thousand, thirty-eight bottles of beer! Take one down, pass it aorund, one thousand, thirty-seven bottles of beer on the wall! one thousand, thirty-seven bottles of beer on the wall, one thousand, thirty-seven bottles of beer! Take one down, pass it aorund, one thousand, thirty-six bottles of beer on the wall! one thousand, thirty-six bottles of beer on the wall, one thousand, thirty-six bottles of beer! Take one down, pass it aorund, one thousand, thirty-five bottles of beer on the wall! one thousand, thirty-five bottles of beer on the wall, one thousand, thirty-five bottles of beer! Take one down, pass it aorund, one thousand, thirty-four bottles of beer on the wall! one thousand, thirty-four bottles of beer on the wall, one thousand, thirty-four bottles of beer! Take one down, pass it aorund, one thousand, thirty-three bottles of beer on the wall! one thousand, thirty-three bottles of beer on the wall, one thousand, thirty-three bottles of beer! Take one down, pass it aorund, one thousand, thirty-two bottles of beer on the wall! one thousand, thirty-two bottles of beer on the wall, one thousand, thirty-two bottles of beer! Take one down, pass it aorund, one thousand, thirty-one bottles of beer on the wall! one thousand, thirty-one bottles of beer on the wall, one thousand, thirty-one bottles of beer! Take one down, pass it aorund, one thousand, thirty bottles of beer on the wall! one thousand, thirty bottles of beer on the wall, one thousand, thirty bottles of beer! Take one down, pass it aorund, one thousand, twenty-nine bottles of beer on the wall! one thousand, twenty-nine bottles of beer on the wall, one thousand, twenty-nine bottles of beer! Take one down, pass it aorund, one thousand, twenty-eight bottles of beer on the wall! one thousand, twenty-eight bottles of beer on the wall, one thousand, twenty-eight bottles of beer! Take one down, pass it aorund, one thousand, twenty-seven bottles of beer on the wall! one thousand, twenty-seven bottles of beer on the wall, one thousand, twenty-seven bottles of beer! Take one down, pass it aorund, one thousand, twenty-six bottles of beer on the wall! one thousand, twenty-six bottles of beer on the wall, one thousand, twenty-six bottles of beer! Take one down, pass it aorund, one thousand, twenty-five bottles of beer on the wall! one thousand, twenty-five bottles of beer on the wall, one thousand, twenty-five bottles of beer! Take one down, pass it aorund, one thousand, twenty-four bottles of beer on the wall! one thousand, twenty-four bottles of beer on the wall, one thousand, twenty-four bottles of beer! Take one down, pass it aorund, one thousand, twenty-three bottles of beer on the wall! one thousand, twenty-three bottles of beer on the wall, one thousand, twenty-three bottles of beer! Take one down, pass it aorund, one thousand, twenty-two bottles of beer on the wall! one thousand, twenty-two bottles of beer on the wall, one thousand, twenty-two bottles of beer! Take one down, pass it aorund, one thousand, twenty-one bottles of beer on the wall! one thousand, twenty-one bottles of beer on the wall, one thousand, twenty-one bottles of beer! Take one down, pass it aorund, one thousand, twenty bottles of beer on the wall! one thousand, twenty bottles of beer on the wall, one thousand, twenty bottles of beer! Take one down, pass it aorund, one thousand, nineteen bottles of beer on the wall! one thousand, nineteen bottles of beer on the wall, one thousand, nineteen bottles of beer! Take one down, pass it aorund, one thousand, eighteen bottles of beer on the wall! one thousand, eighteen bottles of beer on the wall, one thousand, eighteen bottles of beer! Take one down, pass it aorund, one thousand, seventeen bottles of beer on the wall! one thousand, seventeen bottles of beer on the wall, one thousand, seventeen bottles of beer! Take one down, pass it aorund, one thousand, sixteen bottles of beer on the wall! one thousand, sixteen bottles of beer on the wall, one thousand, sixteen bottles of beer! Take one down, pass it aorund, one thousand, fifteen bottles of beer on the wall! one thousand, fifteen bottles of beer on the wall, one thousand, fifteen bottles of beer! Take one down, pass it aorund, one thousand, fourteen bottles of beer on the wall! one thousand, fourteen bottles of beer on the wall, one thousand, fourteen bottles of beer! Take one down, pass it aorund, one thousand, thirteen bottles of beer on the wall! one thousand, thirteen bottles of beer on the wall, one thousand, thirteen bottles of beer! Take one down, pass it aorund, one thousand, twelve bottles of beer on the wall! one thousand, twelve bottles of beer on the wall, one thousand, twelve bottles of beer! Take one down, pass it aorund, one thousand, eleven bottles of beer on the wall! one thousand, eleven bottles of beer on the wall, one thousand, eleven bottles of beer! Take one down, pass it aorund, one thousand, ten bottles of beer on the wall! one thousand, ten bottles of beer on the wall, one thousand, ten bottles of beer! Take one down, pass it aorund, one thousand, nine bottles of beer on the wall! one thousand, nine bottles of beer on the wall, one thousand, nine bottles of beer! Take one down, pass it aorund, one thousand, eight bottles of beer on the wall! one thousand, eight bottles of beer on the wall, one thousand, eight bottles of beer! Take one down, pass it aorund, one thousand, seven bottles of beer on the wall! one thousand, seven bottles of beer on the wall, one thousand, seven bottles of beer! Take one down, pass it aorund, one thousand, six bottles of beer on the wall! one thousand, six bottles of beer on the wall, one thousand, six bottles of beer! Take one down, pass it aorund, one thousand, five bottles of beer on the wall! one thousand, five bottles of beer on the wall, one thousand, five bottles of beer! Take one down, pass it aorund, one thousand, four bottles of beer on the wall! one thousand, four bottles of beer on the wall, one thousand, four bottles of beer! Take one down, pass it aorund, one thousand, three bottles of beer on the wall! one thousand, three bottles of beer on the wall, one thousand, three bottles of beer! Take one down, pass it aorund, one thousand, two bottles of beer on the wall! one thousand, two bottles of beer on the wall, one thousand, two bottles of beer! Take one down, pass it aorund, one thousand, one bottles of beer on the wall! one thousand, one bottles of beer on the wall, one thousand, one bottles of beer! -------------------------------------------------------------------------------- Take one down, pass it around, one bottle of beer on the wall! 1 bottle of beer on the wall, one bottle of beer! Take one down, pass it around, no more bottles of beer on the wall! $
ちなみにJavaScriptの場合。
コード(BBEdit)
function nToEnglishNumber(n) { if (typeof(n) !== "number") { throw "NaN"; } if ( !(n > 0) ) { throw { type: "エラー", message: "自然数を入力してください" }; } if (n === 0){ return "zero"; } var result = "", ones = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"], teenagers = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"], tens = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"], others = [ [2, 'hundred'], [3, 'thousand'], [6, 'million'], [9, 'billion'], [12, 'trillion'], [15, 'quandrillion'], [18, 'quintillion'], [21, 'sextillion'], [24, 'septillion']], left = n, other, write, pre; while (others.length > 0) { other = others.pop(); write = Math.floor( left / Math.pow(10, other[0])); left = left - write * (Math.pow(10, other[0])); if (write > 0) { pre = nToEnglishNumber(write); result = result + pre + ' ' + other[1]; if (left > 0){ if(other[1] !== "hundred"){ result += ", "; } else { result += " "; } } } } write = Math.floor(left / 10); left = left - write * 10; if (write > 0) { if ((write === 1) && (left > 0)){ result += teenagers[left - 1]; left = 0; } else { result += tens[write - 1]; if (left > 0){ result += "-"; } } } write = left; left = 0; if (write > 0){ result += ones[write - 1]; } return result; } var i = 1111, result = "", s; while(true){ s = nToEnglishNumber(i); result += s + " bottles of beer on the wall, " + s + " bottles of beer!\n"; i--; if(i === 1100) break; result += "Take one down, pass it aorund, " + nToEnglishNumber(i) + " bottles of beer on the wall!\n"; } s = nToEnglishNumber(i); result += "Take one down, pass it around, " + s + " bottle of beer on the wall!\n" + s + " bottle of beer on the wall, " + s + " bottle of beer!\n" + "Take one down, pass it around, no more bottles of beer on the wall!\n"; $('#pre0').text(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- def nToEnglishNumber(n): if n < 0: return "自然数を入力してください" if n == 0: return 'zero' result = "" ones = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] teenagers = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] tens = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] others = [ [2, 'hundred'], [3, 'thousand'], [6, 'million'], [9, 'billion'], [12, 'trillion'], [15, 'quandrillion'], [18, 'quintillion'], [21, 'sextillion'], [24, 'septillion']] left = n while len(others) > 0: other = others.pop() write = left // (10 ** other[0]) left = left - write * (10 ** other[0]) if write > 0: pre = nToEnglishNumber(write) result = result + pre + ' ' + other[1] if left > 0: if other[1] != 'hundred': result += ", " else: result += ' ' write = left // 10 left = left - write * 10 if write > 0: if write == 1 and left > 0: result += teenagers[left - 1] left = 0 else: result += tens[write - 1] if left > 0: result += '-' write = left left = 0 if write > 0: result += ones[write - 1] return result for x in range(1111, 1100, -2): print("{0} bottles of beer on the wall, {0} bottles of beer!".format( nToEnglishNumber(x))) x -= 1 print("Take one down, pass it around, {0} bottles of beer on the wall!".format( nToEnglishNumber(x))) else: print("{0} bottles of beer on the wall, {0} bottles of beer!".format( nToEnglishNumber(2))) print("Take one down, pass it around, {0} bottle of beer on the wall!".format( nToEnglishNumber(1))) print("------------------------------------------------------------------------") print("{0} bottle of beer on the wall, {0} bottle of beer!".format( nToEnglishNumber(1))) print("Take one down, pass it around, no more bottles of beer on the wall!")
入出力結果(Terminal)
$ ./sample.py one thousand, one hundred eleven bottles of beer on the wall, one thousand, one hundred eleven bottles of beer! Take one down, pass it around, one thousand, one hundred ten bottles of beer on the wall! one thousand, one hundred nine bottles of beer on the wall, one thousand, one hundred nine bottles of beer! Take one down, pass it around, one thousand, one hundred eight bottles of beer on the wall! one thousand, one hundred seven bottles of beer on the wall, one thousand, one hundred seven bottles of beer! Take one down, pass it around, one thousand, one hundred six bottles of beer on the wall! one thousand, one hundred five bottles of beer on the wall, one thousand, one hundred five bottles of beer! Take one down, pass it around, one thousand, one hundred four bottles of beer on the wall! one thousand, one hundred three bottles of beer on the wall, one thousand, one hundred three bottles of beer! Take one down, pass it around, one thousand, one hundred two bottles of beer on the wall! one thousand, one hundred one bottles of beer on the wall, one thousand, one hundred one bottles of beer! Take one down, pass it around, one thousand, one hundred bottles of beer on the wall! two bottles of beer on the wall, two bottles of beer! Take one down, pass it around, one bottle of beer on the wall! ------------------------------------------------------------------------ one bottle of beer on the wall, one bottle of beer! Take one down, pass it around, no more bottles of beer on the wall!
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 nToEnglishNumber { my $n = shift; if ($n < 0) { return "自然数を指定してください"; } if ($n == 0) { return 'zero'; } my $result = ""; my @ones = qw(one two three four five six seven eight nine); my @tens_place = qw(eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen); my @tens = qw(ten twenty thirty forty fifty sixty seventy eighty ninety); my @others = ([2, 'hundred'], [3, 'thousand'], [6, 'million'], [9, 'billion'], [12, 'trillion'], [15, 'quandrillion'], [18, 'quintillion'], [21, 'sextillion'], [24, 'septillion']); my $left = $n; my $write; while (@others ) { my $other = pop @others; $write = floor( $left / (10 ** $other->[0]) ); $left = $left - $write * (10 ** $other->[0]); if ($write > 0) { my $pre = nToEnglishNumber( $write ); $result = $result . $pre . " " . $other->[1]; if ($left > 0) { if ($other->[1] ne 'hundred') { $result .= ", "; } else { $result .= " "; } } } } $write = floor($left / 10); $left = $left - $write * 10; if ($write > 0) { if (($write == 1) and ($left > 0)) { $result .= $tens_place[$left - 1]; $left = 0; } else { $result .= $tens[$write - 1]; $result .= '-' if $left > 0; } } $write = $left; $left = 0; $result .= $ones[$write - 1] if $write > 0; $result; } my $i = 1111; my $result = ""; my $s; while(1){ $s = nToEnglishNumber $i; $result .= $s . " bottles of beer on the wall, " . $s . " bottles of beer!\n"; $i -= 111; last if $i <= 0; $result .= "Take one down, pass it aorund, " . nToEnglishNumber($i) . " bottles of beer on the wall!\n"; } $i = 1; $s = nToEnglishNumber($i); $result .= "Take one down, pass it around, " . $s . " bottle of beer on the wall!\n" . $i . " bottle of beer on the wall, " . $s . " bottle of beer!\n" . "Take one down, pass it around, no more bottles of beer on the wall!\n"; print $result;
入出力結果(Terminal)
$ ./sample.pl one thousand, one hundred eleven bottles of beer on the wall, one thousand, one hundred eleven bottles of beer! Take one down, pass it aorund, one thousand bottles of beer on the wall! one thousand bottles of beer on the wall, one thousand bottles of beer! Take one down, pass it aorund, eight hundred eighty-nine bottles of beer on the wall! eight hundred eighty-nine bottles of beer on the wall, eight hundred eighty-nine bottles of beer! Take one down, pass it aorund, seven hundred seventy-eight bottles of beer on the wall! seven hundred seventy-eight bottles of beer on the wall, seven hundred seventy-eight bottles of beer! Take one down, pass it aorund, six hundred sixty-seven bottles of beer on the wall! six hundred sixty-seven bottles of beer on the wall, six hundred sixty-seven bottles of beer! Take one down, pass it aorund, five hundred fifty-six bottles of beer on the wall! five hundred fifty-six bottles of beer on the wall, five hundred fifty-six bottles of beer! Take one down, pass it aorund, four hundred forty-five bottles of beer on the wall! four hundred forty-five bottles of beer on the wall, four hundred forty-five bottles of beer! Take one down, pass it aorund, three hundred thirty-four bottles of beer on the wall! three hundred thirty-four bottles of beer on the wall, three hundred thirty-four bottles of beer! Take one down, pass it aorund, two hundred twenty-three bottles of beer on the wall! two hundred twenty-three bottles of beer on the wall, two hundred twenty-three bottles of beer! Take one down, pass it aorund, one hundred twelve bottles of beer on the wall! one hundred twelve bottles of beer on the wall, one hundred twelve bottles of beer! Take one down, pass it aorund, one bottles of beer on the wall! one bottles of beer on the wall, one bottles of beer! Take one down, pass it around, one bottle of beer on the wall! 1 bottle of beer on the wall, one bottle of beer! Take one down, pass it around, no more bottles of beer on the wall! $
0 コメント:
コメントを投稿