2012年12月9日日曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 7章(フロー制御), 7.5(練習問題)「壁にビールが99本」 を解いてみる。

その他参考書籍

「壁にビールが99本」

コード(TextWrangler)

sample.rb

#!/usr/bin/env ruby1.9
# -*- coding: utf-8 -*-

n = 5
while true
  puts "#{n} bottles of beer on the wall, #{n} bottles of beer!"
  n -= 1
  break if n == 1
  puts "Take one down, pass it around, #{n} bottles of beer on the wall!"
end
puts "Take one down, pass it around, #{n} bottle of beer on the wall!"
puts "#{n} bottle of beer on the wall, #{n} 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の場合。

コード(TextWrangler)

var n = 5;
while(1){
  $('#pre0').append(
    n + " bottles of beer on the wall, " +
    n + " bottles of beer!\n");
  n--;
  if( n == 1) break;
  $('#pre0').append(
    "Take one down, pass it around, " +
    n + " bottles of beer on the wall!\n");
}
$('#pre0').append(
  "Take one down, pass it around, " + 
  n + " bottle of beer on the wall!\n" + 
  n + " bottle of beer on the wall, " +
  n + " bottle of beer!\n" +
  "Take one down, pass it around, no more bottles of beer on the wall!\n");


pythonの場合。

sample.py

コード(TextWrangler)

#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-

n = 5
while True:
    print("{0} bottles of beer on the wall, {0} bottles of beer!".format(n))
    n -= 1
    if n == 1: break
    print("Take one down, pass it around, {0} bottles of beer on the wall!".format(n))

print("Take one down, pass it around, {0} bottle of beer on the wall".format(n))
print("{0} bottle of beer on the wall, {0} bottle of beer!".format(n))
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

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';

my $n = 5;
while(1){
  print $n . " bottles of beer on the wall, " . $n . " bottles of beer!\n";
  $n--;
  last if $n == 1;
  print "Take one down, pass it around, " . $n . " bottles of beer on the wall!\n";
}
print "Take one down, pass it around, ". $n . " bottle of beer on the wall!\n";
print $n . " bottle of beer on the wall, " . $n . " 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 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 コメント:

コメントを投稿