Exercises from Chapter 7

“99 Bottles of Beer on the Wall”

How you could do it:

num_at_start = 5 ​# change to 99 if you want
num_now = num_at_start
while​ num_now > 2
puts num_now.to_s + ​' bottles of beer on the wall, '​ +
num_now.to_s + ​' bottles of beer!'
num_now = num_now - 1
puts ​'Take one down, pass it around, '​ +
num_now.to_s + ​' 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!"
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!

How I would do it:

num_at_start = 5 ​# change to 99 if you want
num_bot = proc { |n| ​"​#{n}​ bottle​#{n == 1 ? ​''​ : ​'s'​}​"​ }
num_at_start.downto(2) ​do​ |num|
puts ​"​#{num_bot[num]}​ of beer on the wall, ​#{num_bot[num]}​ of beer!"
puts ​"Take one down, pass it around, ​#{num_bot[num-1]}​ of beer on the wall!"
end
puts ​"​#{num_bot[1]}​ of beer on the wall, ​#{num_bot[1]}​ of beer!"
puts ​"Take one down, pass it around, no more bottles of beer on the wall!"
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!

Deaf Grandma

How you could do it:

puts ​'HEY THERE, SONNY! GIVE GRANDMA A KISS!'
while​ true
said = gets.chomp
if​ said == ​"BYE"
puts ​'BYE SWEETIE!'
break
end
if​ said != said.upcase
puts ​'HUH?! SPEAK UP, SONNY!'
else
random_year = 1930 + rand(21)
puts ​'NO, NOT SINCE '​ + random_year.to_s + ​'!'
end
end
<= HEY THERE, SONNY! GIVE GRANDMA A KISS!
=> hi, grandma
<= HUH?! SPEAK UP, SONNY!
=> HI, GRANDMA!
<= NO, NOT SINCE 1945!
=> HOW YOU DOING?
<= NO, NOT SINCE 1933!
=> I SAID, HOW YOU DOING?
<= NO, NOT SINCE 1944!
=> OK
<= NO, NOT SINCE 1934!
=> BYE
<= BYE SWEETIE!

How I would do it:

puts ​'HEY THERE, SONNY! GIVE GRANDMA A KISS!'
while​ true
said = gets.chomp
break​ ​if​ said == ​"BYE"
response = ​if​ said != said.upcase
'HUH?! SPEAK UP, SONNY!'
else
"NO, NOT SINCE ​#{rand(1930..1950)}​!"
end
puts response
end
puts ​'BYE SWEETIE!'
<= HEY THERE, SONNY! GIVE GRANDMA A KISS!
=> hi, grandma
<= HUH?! SPEAK UP, SONNY!
=> HI, GRANDMA!
<= NO, NOT SINCE 1941!
=> HOW YOU DOING?
<= NO, NOT SINCE 1932!
=> I SAID, HOW YOU DOING?
<= NO, NOT SINCE 1949!
=> OK
<= NO, NOT SINCE 1950!
=> BYE
<= BYE SWEETIE!

Deaf Grandma Extended

How you could do it:

puts ​'HEY THERE, PEACHES! GIVE GRANDMA A KISS!'
bye_count = 0
while​ true
said = gets.chomp
if​ said == ​'BYE'
bye_count = bye_count + 1
else
bye_count = 0
end
if​ bye_count >= 3
puts ​'BYE-BYE CUPCAKE!'
break
end
if​ said != said.upcase
puts ​'HUH?! SPEAK UP, SONNY!'
else
random_year = 1930 + rand(21)
puts ​'NO, NOT SINCE '​ + random_year.to_s + ​'!'
end
end
<= HEY THERE, PEACHES! GIVE GRANDMA A KISS!
=> HI, GRANDMA!
<= NO, NOT SINCE 1950!
=> BYE
<= NO, NOT SINCE 1936!
=> BYE
<= NO, NOT SINCE 1947!
=> ADIOS, MUCHACHA!
<= NO, NOT SINCE 1949!
=> BYE
<= NO, NOT SINCE 1940!
=> BYE
<= NO, NOT SINCE 1938!
=> BYE
<= BYE-BYE CUPCAKE!

How I would do it:

puts ​'HEY THERE, PEACHES! GIVE GRANDMA A KISS!'
bye_count = 0
while​ true
said = gets.chomp
if​ said == ​'BYE'
bye_count += 1
else
bye_count = 0
end
break​ ​if​ bye_count >= 3
response = ​if​ said != said.upcase
'HUH?! SPEAK UP, SONNY!'
else
"NO, NOT SINCE ​#{rand(1930..1950)}​!"
end
puts response
end
puts ​'BYE-BYE CUPCAKE!'
<= HEY THERE, PEACHES! GIVE GRANDMA A KISS!
=> HI, GRANDMA!
<= NO, NOT SINCE 1934!
=> BYE
<= NO, NOT SINCE 1940!
=> BYE
<= NO, NOT SINCE 1945!
=> ADIOS, MUCHACHA!
<= NO, NOT SINCE 1931!
=> BYE
<= NO, NOT SINCE 1932!
=> BYE
<= NO, NOT SINCE 1949!
=> BYE
<= BYE-BYE CUPCAKE!

Leap Years

How you could do it:

puts ​'Pick a starting year (like 1973 or something):'
starting = gets.chomp.to_i
puts ​'Now pick an ending year:'
ending = gets.chomp.to_i
puts ​'Check it out... these years are leap years:'
year = starting
while​ year <= ending
if​ year%4 == 0
if​ year%100 != 0 || year%400 == 0
puts year
end
end
year = year + 1
end
<= Pick a starting year (like 1973 or something):
=> 1973
<= Now pick an ending year:
=> 1977
<= Check it out... these years are leap years:
 1976

How I would do it:

puts ​'Pick a starting year (like 1973 or something):'
starting = gets.chomp.to_i
puts ​'Now pick an ending year:'
ending = gets.chomp.to_i
puts ​'Check it out... these years are leap years:'
(starting..ending).each ​do​ |year|
next​ ​if​ year%4 != 0
next​ ​if​ year%100 == 0 && year%400 != 0
puts year
end
<= Pick a starting year (like 1973 or something):
=> 1973
<= Now pick an ending year:
=> 1977
<= Check it out... these years are leap years:
 1976
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.214.141