Advertisement
TKVR

Chris Pine, Learn to Program Chapter 6 - Flow Control Ex.

Feb 16th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.04 KB | None | 0 0
  1. # Flow Control - Branching & Looping - WORKING PROGRAM EXAMPLES
  2.  
  3. # "99 bottles of beer on the wall..." Write a program which prints out the lyrics to that beloved classic, that field-trip favoriteff: "99 Bottles of Beer on the Wall."
  4. bottles = 100
  5. while bottles > 1
  6.   puts (bottles-1).to_s + ' bottles of beer on the wall, ' + (bottles-1).to_s + ' bottles of beer!'
  7.   puts 'You take one down, pass it around, ' + (bottles-2).to_s + ' bottles of beer on the wall!'
  8.   bottles = (bottles-1)
  9. end
  10.  
  11.   # 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.
  12.  
  13. # Hint: Don't forget about  chomp!  'BYE'with an Enter is not the same as 'BYE' without one!
  14. # 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.
  15. puts 'Uh oh! Looks like Grandma went deaf today.'
  16. puts 'Try saying something to get her attention:'
  17. speak = gets.chomp
  18. while speak != speak.upcase or speak != 'BYE'
  19.     puts 'HUH!? SPEAK UP, SONNY! Hint: Try shouting or say BYE'
  20.     speak = gets.chomp
  21. if speak == speak.upcase
  22.   puts 'NO, NOT NOT SINCE ' + (1930+rand(21)).to_s + '!' + ' Hint: Try saying *BYE*'
  23.   speak = gets.chomp
  24. end
  25. if speak == 'BYE'
  26.   puts 'Grandma gives you the finger and storms away.'
  27. end
  28. end
  29.  
  30. # 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.
  31. puts 'Uh oh! Looks like Grandma went deaf today.'
  32. puts 'Try saying something to get her attention:'
  33.  
  34. response = 'nope'
  35. bye = 0
  36.  
  37. while bye < 3
  38.   response = gets.chomp
  39. if response == (response.upcase and 'BYE')
  40.   puts 'Hmmm... What about...'
  41.   bye = (bye+1)
  42. end
  43. if response != response.upcase
  44.   puts 'Huh?! I CAN\'T HEAR YOU!'
  45. end
  46. if (response == response.upcase and response != 'BYE')
  47.   puts 'NO! NOT SINCE ' + (1930+rand(21)).to_s + '!'
  48. end
  49. end
  50.  
  51. # 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.)
  52. puts 'This program will determine what leap years are between two specific dates that you assign.'
  53. puts ''
  54. puts 'What would you like your starting year to be?'
  55. start_year = gets.chomp.to_i
  56. puts ''
  57. puts 'What would you like your ending year to be?'
  58. end_year = gets.chomp.to_i
  59. puts "Leap years between #{start_year} and #{end_year}"
  60.  
  61. if start_year > end_year
  62.   puts 'Your closing date is before your start date. Please try again'
  63.   return
  64. end
  65.  
  66. leap_years = []
  67. start_year.upto(end_year) do |year|
  68.   if (year % 4 == 0)
  69.     leap_years << year unless (year % 100 == 0) and (year % 400 != 0)
  70.   end
  71. end
  72.  
  73. puts leap_years.join(', ')
  74.  
  75. # Some notes:
  76. # - you can use the Integer#upto method to step through each year (see http://www.ruby-doc.org/core/ for API docs)
  77. # - remember to cast the user input into integers (and add error-handling, which I have left out in the code above)
  78. # - the % operator returns the remainder... E.g. 10 % 3 gives you 1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement