開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- 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 ruby2.0 #-*- coding: utf-8 -*- def english_number number return '負ではない数値を入力してください' if number < 0 return 'zero' if number == 0 num_string = '' ones_place = %w[one two three four five six seven eight nine] tens_place = %w[ten twenty thirty forty fifty sixty seventy eighty ninety] teenagers = %w[eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen] others = [ [2, 'hundred'], [3, 'thousand'], [6, 'million'], [9, 'billion'], [12, 'trillion'], [15, 'quandrillion'], [18, 'quintillion'], [21, 'sextillion'], [24, 'septillion']] left = number while others.length > 0 other = others.pop n = other[0] name = other[1] d = 10 ** n write = left / d left -= write * d if write > 0 pre = english_number write num_string = num_string + pre + ' ' + name num_string += ' ' if left > 0 end end write = left / 10 left -= write * 10 if write > 0 if write == 1 and left > 0 num_string += teenagers[left - 1] left = 0 else num_string += tens_place[write - 1] end if left > 0 num_string += '-' end end write = left left = 0 num_string += ones_place[write - 1] if write > 0 num_string end n = 10000 while n > 3 n -= 1 next if n < 9990 puts "#{english_number n} bottles of beer on the wall, " + "#{english_number n} bottles of beer!" puts "Take one down, pass it around, #{english_number n} " + "bottles of beer on the wall!" puts "*" * 100 if n == 9990 end puts "#{english_number 2} bottles of beer on the wall, " + "#{english_number 2} bottles of beer!" puts "Take one down, pass it around, #{english_number 1} " + "bottle of beer on the wall" puts "#{english_number 1} bottle of beer on the wall, " + "#{english_number 1} bottle of beer!" puts "Take one down, pass it around, no more bottles of beer on the wall!"
入出力結果(Terminal)
$ ./sample.rb nine thousand nine hundred ninety-nine bottles of beer on the wall, nine thousand nine hundred ninety-nine bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-nine bottles of beer on the wall! nine thousand nine hundred ninety-eight bottles of beer on the wall, nine thousand nine hundred ninety-eight bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-eight bottles of beer on the wall! nine thousand nine hundred ninety-seven bottles of beer on the wall, nine thousand nine hundred ninety-seven bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-seven bottles of beer on the wall! nine thousand nine hundred ninety-six bottles of beer on the wall, nine thousand nine hundred ninety-six bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-six bottles of beer on the wall! nine thousand nine hundred ninety-five bottles of beer on the wall, nine thousand nine hundred ninety-five bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-five bottles of beer on the wall! nine thousand nine hundred ninety-four bottles of beer on the wall, nine thousand nine hundred ninety-four bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-four bottles of beer on the wall! nine thousand nine hundred ninety-three bottles of beer on the wall, nine thousand nine hundred ninety-three bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-three bottles of beer on the wall! nine thousand nine hundred ninety-two bottles of beer on the wall, nine thousand nine hundred ninety-two bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-two bottles of beer on the wall! nine thousand nine hundred ninety-one bottles of beer on the wall, nine thousand nine hundred ninety-one bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-one bottles of beer on the wall! nine thousand nine hundred ninety bottles of beer on the wall, nine thousand nine hundred ninety bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety 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! $
ちなみにpython3.4の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python #-*- coding: utf-8 -*- def englishNumber(num): if num < 0: return "負でない数値を入力してください" if num == 0: return 'zero' num_string = '' ones_place = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] tens_place = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] teenagers = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] others = [ [2, 'hundred'], [3, 'thousand'], [6, 'million'], [9, 'billion'], [12, 'trillion'], [15, 'quandrillion'], [18, 'quintillion'], [21, 'sextillion'], [24, 'septillion']] left = num while len(others) > 0: other = others.pop() n = other[0] name = other[1] d = 10 ** n write = left // d left -= write * d if write > 0: pre = englishNumber(write) num_string = num_string + pre + ' ' + name if left > 0: num_string += ' ' write = left // 10 left -= write * 10 if write > 0: if write == 1 and left > 0: num_string += teenagers[left - 1] left = 0 else: num_string += tens_place[write - 1] if left > 0: num_string += '-' write = left left = 0 if write > 0: num_string += ones_place[write - 1] return num_string n = 9999 for i in range(n, 2, -1): if i < 9990: continue print("{0} bottles of beer on the wall, {0} bottles of beer!".format( englishNumber(i))) i -= 1 print("Take one down, pass it around, " + englishNumber(i) + " bottles of beer on the wall!") if i == 9990: print('*' * 100) print("{0} bottles of beer on the wall, {0} bottles of beer!".format( englishNumber(2))) print("Take one down, pass it around, {0} bottle of beer on the wall".format( englishNumber(1))) print("{0} bottle of beer on the wall, 1 bottle of beer!".format( englishNumber(1))) print("Take one down, pass it around, no more bottles of beer on the wall!")
入出力結果(Terminal)
$ ./sample.py nine thousand nine hundred ninety-nine bottles of beer on the wall, nine thousand nine hundred ninety-nine bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-eight bottles of beer on the wall! nine thousand nine hundred ninety-eight bottles of beer on the wall, nine thousand nine hundred ninety-eight bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-seven bottles of beer on the wall! nine thousand nine hundred ninety-seven bottles of beer on the wall, nine thousand nine hundred ninety-seven bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-six bottles of beer on the wall! nine thousand nine hundred ninety-six bottles of beer on the wall, nine thousand nine hundred ninety-six bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-five bottles of beer on the wall! nine thousand nine hundred ninety-five bottles of beer on the wall, nine thousand nine hundred ninety-five bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-four bottles of beer on the wall! nine thousand nine hundred ninety-four bottles of beer on the wall, nine thousand nine hundred ninety-four bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-three bottles of beer on the wall! nine thousand nine hundred ninety-three bottles of beer on the wall, nine thousand nine hundred ninety-three bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-two bottles of beer on the wall! nine thousand nine hundred ninety-two bottles of beer on the wall, nine thousand nine hundred ninety-two bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety-one bottles of beer on the wall! nine thousand nine hundred ninety-one bottles of beer on the wall, nine thousand nine hundred ninety-one bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety bottles of beer on the wall! **************************************************************************************************** nine thousand nine hundred ninety bottles of beer on the wall, nine thousand nine hundred ninety bottles of beer! Take one down, pass it around, nine thousand nine hundred eighty-nine 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, 1 bottle of beer! Take one down, pass it around, no more bottles of beer on the wall! $
0 コメント:
コメントを投稿