開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 7章(フロー制御), 7.5(練習問題)続・耳の遠いおばあちゃん を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
続・耳の遠いおばあちゃん
コード(TextWrangler)
sample.rb
#!/usr/bin/env ruby1.9 # -*- coding: utf-8 -*- count = 0 while said = gets.chomp if said == "BYE" count += 1 break if count == 3 puts "NO, NOT SINCE #{rand(21) + 1930}!" elsif said == said.upcase puts "NO, NOT SINCE #{rand(21) + 1930}!" count = 0 else puts "HUH?! SPEAK UP, SONNY!" count = 0 end end puts "BYE"
入出力結果(Terminal)
$ ./sample.rb hi HUH?! SPEAK UP, SONNY! Hi HUH?! SPEAK UP, SONNY! HI NO, NOT SINCE 1940! HI NO, NOT SINCE 1945! HI NO, NOT SINCE 1932! HI NO, NOT SINCE 1930! HI NO, NOT SINCE 1939! by HUH?! SPEAK UP, SONNY! BYE NO, NOT SINCE 1945! BYE NO, NOT SINCE 1938! HI NO, NOT SINCE 1933! BYE NO, NOT SINCE 1940! BYE NO, NOT SINCE 1937! BYE BYE $
ちなみにJavaScriptの場合。
コード(TextWrangler)
<pre id='pre0' ></pre> <label id="l0"><input id="t0" type="text" value="Hi" onkeydown="f()" /></label> <input id="btn0" type="button" value="say" onclick="say()"/><br /> <script> var count = 0; function say(){ var said = $('#t0').val(); $('#pre0').append(said + "\n"); if(said == "BYE"){ count += 1; if(count === 3){ $('#pre0').append("BYE\n"); $('#l0').remove(); $('#btn0').remove(); return; } $('#pre0').append("NO, NOT SINCE " + year + "!\n"); }else if(said == said.toUpperCase()){ count = 0; var year = 1930 + parseInt(Math.random() * 21); $('#pre0').append("NO, NOT SINCE " + year + "!\n"); } else { count = 0; $('#pre0').append("HUH?! SPEAK UP, SONNY!\n"); } } function f(e){ var e = e ? e : window.event; if(e.keyCode == 13) say(); } </script>
pythonの場合。
sample.py
コード(TextWrangler)
#!/usr/bin/env python3.3 # -*- coding: utf-8 -*- import random count = 0 while 1: said = input() if said == "BYE": count += 1 if count == 3: print("BYE") break print("NO, NOT SINCE {0}!".format(random.randint(1930, 1950))) elif said == said.upper(): count = 0 print("NO, NOT SINCE {0}!".format(random.randint(1930, 1950))) else: count = 0 print("HUH?! SPEAK UP, SONNY!")
入出力結果(Terminal)
$ ./sample.py hi HUH?! SPEAK UP, SONNY! HI NO, NOT SINCE 1941! BYE NO, NOT SINCE 1942! BYE NO, NOT SINCE 1936! HI NO, NOT SINCE 1943! BYE NO, NOT SINCE 1933! BYE NO, NOT SINCE 1934! BYE BYE $
perlの場合。
sample.pl
コード(TextWrangler)
#!/usr/bin/env perl use strict; use warnings; use utf8; use 5.016; binmode STDIN, ':utf8'; binmode STDOUT, ':utf8'; my $count = 0; while(1){ chomp(my $said = <STDIN>); if($said eq "BYE"){ $count++; if($count == 3){ print "BYE\n"; last; } print "NO, NOT SINCE " . int(1930 + rand(21)) . "!\n"; } elsif($said eq uc $said){ $count = 0; print "NO, NOT SINCE " . int(1930 + rand(21)) . "!\n"; } else { $count = 0; print "HUH?! SPEAK UP, SONNY!\n"; } }
入出力結果(Terminal)
$ ./sample.pl Hi HUH?! SPEAK UP, SONNY! HI NO, NOT SINCE 1935! BYE NO, NOT SINCE 1942! BYE NO, NOT SINCE 1946! bye HUH?! SPEAK UP, SONNY! BYE NO, NOT SINCE 1950! BYE NO, NOT SINCE 1930! BYE BYE $
0 コメント:
コメントを投稿