Rubyでプログラミング勉強中(参照)。
本当は毎日ちょっとずつでもプログラムを書いて勉強していこうなんて意気込んでいたんだけど、日々のアレコレに紛れて、なかなか・・・。ま、いっか。のんびりいきましょ。
というわけで、今日は、Chapter 6. Flow Control(参照)。BRANCHING(if文)やらLOOPING(while)が登場。これまでのChapterに比べると、ぐっと難しく、そんでもって楽しくなってきた。
A Few Things to Try on 6. Flow Control
一問目
“99 bottles of beer on the wall…” Write a program which prints out the lyrics to that beloved classic, that field-trip favorite: “99 Bottles of Beer on the Wall.”
6. Flow Control
※”99 bottles of beer on the wall”っていうのは、マザーグースの歌らしい。日本語訳(参照)に出力例が出ている。
ちょっと考えたけど、以下な感じで落ち着いた。出力例通りに出力されたので、OKかな。
beer = 99
while beer != 0
puts beer.to_s + ' Bottles of beer on the wall'
puts beer.to_s + ' Bottles of beer'
puts 'Take on down and pass it around'
beer = beer - 1
puts beer.to_s + ' Bottles of beer on the wall'
puts ''
end
二問目
Write a Deaf Grandma program. Whatever you say to grandma (whatever you type in), she should respond with HUH?! SPEAK UP, SONNY!, unless you shout it (type in all capitals). If you shout, she can hear you (or at least she thinks so) and yells back, NO, NOT SINCE 1938! To make your program really believable, have grandma shout a different year each time; maybe any year at random between 1930 and 1950. (This part is optional, and would be much easier if you read the section on Ruby’s random number generator at the end of the methods chapter.) You can’t stop talking to grandma until you shout BYE.
- Hint: Don’t forget about chomp! ‘BYE’with an Enter is not the same as ‘BYE’ without one!
- Hint 2: Try to think about what parts of your program should happen over and over again. All of those should be in your while loop.
6. Flow Control
耳が遠いおばあちゃんとの会話プログラム。Hintにもあるとおり、while loopがKeyですな。『any year at random between 1930 and 1950』っていうのをどう書いたらいいのか迷ったけど、結局ここでもwhile文を使った。他に書き方あるのかな・・・。
puts 'Hi, Granma.'
iSay = gets.chomp
while iSay != iSay.upcase
puts 'HUH? SPEAK UP, SUNNY!'
iSay = gets.chomp
end
while iSay != 'BYE'
rYear = 1
while rYear <= 1930
rYear = (rand(1951))
end
puts 'NO, NOT SINCE ' + rYear.to_s + '!'
iSay = gets.chomp
end
puts 'Okay.... bye for now...'
三問目
Extend your Deaf Grandma program: What if grandma doesn’t want you to leave? When you shout BYE, she could pretend not to hear you. Change your previous program so that you have to shout BYE three times in a row. Make sure to test your program: if you shout BYE three times, but not in a row, you should still be talking to grandma.
6. Flow Control
BYEを三回連続で叫ばないと耳の遠いおばあちゃんとの会話が終わらないように二問目のコードを変更する。この『三回連続』っていうとこで、ちょっとはまった。二回連続で言った後に、BYE以外の言葉を言ったら、カウントがリセットされるっていうのが漏れてて、悩んだ。で、結局以下のようなコードに。
puts 'Hi, Granma.'
iSay = gets.chomp
while iSay != iSay.upcase
puts 'HUH? SPEAK UP, SUNNY!'
iSay = gets.chomp
end
while iSay != 'BYE'
rYear = 1
while (rYear <= 1930)
rYear = (rand(1951))
end
puts 'NO, NOT SINCE ' + rYear.to_s + '!'
iSay = gets.chomp
end
iBye = 1
while iBye != 3
puts 'HUH? WHAT DID YOU SAY?'
iSay = gets.chomp
if iSay == 'BYE'
iBye = iBye + 1
else
iBye = 0
end
end
puts 'Okay.... bye for now...'
四問目
Leap Years. Write a program which will ask for a starting year and an ending year, and then puts all of the leap years between them (and including them, if they are also leap years). Leap years are years divisible by four (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are divisible by 400 (like 1600 and 2000, which were in fact leap years). (Yes, it’s all pretty confusing, but not as confusing has having July in the middle of the winter, which is what would eventually happen.)
6. Flow Control
入力した年と年の間の閏年を一覧出力するプログラム。閏年の算出って結構ややこしいってことは知ってたけど、これをプログラムに落とすっていうのはなかなか頭を使った。まずは、『入力した年と年の間のう閏年を一覧出力』を考える前に、閏年判別プログラムを書いてみる。
(注: 以下の回答はかなりへタレ。模範解答を参考に書き直した ⇒ id:heydays:20080508:1210237624)
puts 'Enter a year.'
iYear = gets.chomp
aYear = iYear.to_i % 4
bYear = iYear.to_i % 100
cYear = iYear.to_i % 400
if aYear == 0
if bYear != 0
puts iYear.to_s + ' is the leap year.'
else
if cYear == 0
puts iYear.to_s + ' is the leap year.'
else
puts iYear.to_s + ' is NOT the leap year.'
end
end
else
puts iYear.to_s + ' is NOT the leap year.'
end
うむ、いい感じ。で、『入力した年と年の間のう閏年を一覧出力』するプログラムは、以下な感じで。一応、『start year <= end year』チェックを入れてみた。
puts 'Enter a start year.'
sYear = gets.chomp
puts ''
puts 'Enter an end year.'
eYear = gets.chomp
puts ''
if sYear > eYear
puts 'An end year must be equal to or greater than a start year.'
else
puts 'The leap years between ' + sYear.to_s + ' and ' + eYear.to_s + ' are listed below.'
puts ''
while sYear.to_i != (eYear.to_i + 1)
aYear = sYear.to_i % 4
bYear = sYear.to_i % 100
cYear = sYear.to_i % 400
if aYear == 0
if bYear != 0
puts sYear.to_s
else
if cYear == 0
puts sYear.to_s
end
end
end
sYear = sYear.to_i + 1
end
end
雑感
ふー。今回は、前回までのそれと比べて、随分と頭を使った。おかげで随分と理解が深まった。四問目で書いた『閏年判定プログラム』は、実際に意外と役立つプログラムだったりするので、何だか小さな達成感。けど、もっと上手に書くことが出来るような気がしないでもない。慣れてきてから振り返ってみるか。
See also