2013年3月31日日曜日

開発環境

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

その他参考書籍

「壁にビールが99本」

コード(BBEdit)

sample.rb

#!/usr/bin/env ruby2.0
#-*- coding: utf-8 -*-

n = 5
n.downto(3) do |i|
    puts "#{i} bottles of beer on the wall, #{i} bottles of beer!"
    i -= 1
    puts "Take one down, pass it around, #{i} bottles of beer on the wall!"
end

puts "2 bottles of beer on the wall, 2 bottles of beer!"
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!
$

pythonの場合。

sample.py

コード(BBEdit)

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

n = 5
for i in range(n, 2, -1):
    print("{0} bottles of beer on the wall, {0} bottles of beer!".format(i))
    i -= 1
    print("Take one down, pass it around, " + str(i) +
        " bottles of beer on the wall!")
print("2 bottles of beer on the wall, 2 bottles of beer!")
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!
$

1 コメント :

Anneさんのコメント...
このコメントは投稿者によって削除されました。

コメントを投稿