開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 7章(フロー制御), 7.5(練習問題)「壁にビールが99本」 を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
「壁にビールが99本」
コード(BBEdit)
sample.rb
#!/usr/bin/env ruby1.9
#-*- coding: utf-8 -*-
puts "5 bottles of beer on the wall, 5 bottles of beer!"
4.downto(2) do |i|
puts "Take one down, pass it around, #{i} bottles of beer on the wall!"
puts "#{i} bottles of beer on the wall, #{i} bottles of beer!"
end
puts "Take one down, pass it around, 1 bottle of beer on the wall!"
puts "1 bottle of beer on the wall, 1 bottle of beer!"
puts "Take one down, pass it around, no more bottles of beer on the wall!"
入出力結果(Terminal)
$ ./sample.rb 5 bottles of beer on the wall, 5 bottles of beer! Take one down, pass it around, 4 bottles of beer on the wall! 4 bottles of beer on the wall, 4 bottles of beer! Take one down, pass it around, 3 bottles of beer on the wall! 3 bottles of beer on the wall, 3 bottles of beer! Take one down, pass it around, 2 bottles of beer on the wall! 2 bottles of beer on the wall, 2 bottles of beer! Take one down, pass it around, 1 bottle of beer on the wall! 1 bottle of beer on the wall, 1 bottle of beer! Take one down, pass it around, no more bottles of beer on the wall! $
ちなみにJavaScriptの場合。
コード(BBEdit)
var result = "",
i, min;
result += "5 bottles of beer on the wall, 5 bottles of beer!\n";
for ( i = 4, min = 2; i >= 2; i -= 1) {
result += "Take one down, pass it around, " + i +
" bottles of beer on the wall!\n" + i +
" bottles of beer on the wall, " + i +
" bottles of beer!\n";
}
result += "Take one down, pass it around, 1 bottle of beer on the wall!\n" +
"1 bottle of beer on the wall, 1 bottle of beer!\n" +
"Take one down, pass it around, no more bottles of beer on the wall!";
$('#pre0').text(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
print("5 bottles of beer on the wall, 5 bottles of beer!")
for i in range(4, 1, -1):
print("Take one down, pass it around, {0} bottles of beer on the wall!".format(i))
print("{0} bottles of beer on the wall, {0} bottles of beer!".format(i))
print("Take one down, pass it around, 1 bottle of beer on the wall!")
print("1 bottle of beer on the wall, 1 bottle of beer!")
print("Take one down, pass it around, no more bottles of beer on the wall!")
入出力結果(Terminal)
$ ./sample.py 5 bottles of beer on the wall, 5 bottles of beer! Take one down, pass it around, 4 bottles of beer on the wall! 4 bottles of beer on the wall, 4 bottles of beer! Take one down, pass it around, 3 bottles of beer on the wall! 3 bottles of beer on the wall, 3 bottles of beer! Take one down, pass it around, 2 bottles of beer on the wall! 2 bottles of beer on the wall, 2 bottles of beer! Take one down, pass it around, 1 bottle of beer on the wall! 1 bottle of beer on the wall, 1 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";
print "5 bottles of beer on the wall, 5 bottles of beer!\n";
for(reverse (1..4)) {
print "Take one down, pass it around, $_ bottles of beer on the wall!\n";
print "$_ bottles of beer on the wall, $_ bottles of beer!\n";
}
print "Take one down, pass it around, 1 bottle of beer on the wall!\n";
print "1 bottle of beer on the wall, 1 bottle of beer!\n";
print "Take one down, pass it around, no more bottles of beer on the wall!\n";
入出力結果(Terminal)
$ ./sample.pl 5 bottles of beer on the wall, 5 bottles of beer! Take one down, pass it around, 4 bottles of beer on the wall! 4 bottles of beer on the wall, 4 bottles of beer! Take one down, pass it around, 3 bottles of beer on the wall! 3 bottles of beer on the wall, 3 bottles of beer! Take one down, pass it around, 2 bottles of beer on the wall! 2 bottles of beer on the wall, 2 bottles of beer! Take one down, pass it around, 1 bottles of beer on the wall! 1 bottles of beer on the wall, 1 bottles of beer! Take one down, pass it around, 1 bottle of beer on the wall! 1 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 コメント:
コメントを投稿