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