Skip to content

Instantly share code, notes, and snippets.

@lgranger
Created September 28, 2015 00:20
Show Gist options
  • Save lgranger/43173058e1c732582a1d to your computer and use it in GitHub Desktop.
Save lgranger/43173058e1c732582a1d to your computer and use it in GitHub Desktop.
# "99 Bottles of Beer on the Wall." Write a program that prints out the lyrics to that beloved classic.
bottles = 99
while bottles != 2 do
puts "#{bottles} bottles of beer on the wall! #{bottles} bottles of beer! Take one down! Pass it around! #{bottles - 1} bottles of beer on the wall!\n"
bottles -= 1
end
puts "#{bottles} bottles of beer on the wall! #{bottles} bottles of beer! Take one down! Pass it around! #{bottles - 1} bottle of beer on the wall!\n"
bottles -= 1
puts "#{bottles} bottle of beer on the wall! #{bottles} bottle of beer! Take it down! Pass it around! No more bottles of beer on the wall!"
# Deaf grandma. Whatever you say to Grandma she should respond with "HUH? SPEAK UP, SONNY!", unless you shout it, in which case she responds with "NO, NOT SINCE ____!" with _____ being a randomly generated year between 1930 and 1950.
# Deaf grandma extended. What if Grandma doesn't want you to leave? When you shout BYE she could pretend to go hear you unless you shout BYE three times in a row.
goodbye_count = 0
cant_hear_array = ["I don't think she heard you.", "Uhhh...Maybe her hearding aid is off", "That was quite some time ago!", "Oh Grandma! You're a riot!", "Well isn't that...interesting, thanks Grandma."]
say_prompts = ["Say something else to Grandma.", "What are you going to say now?", "I think Grandma is waitng for you to say something.", "Hey, it's your turn to talk next!", "Well....what do you say to that?!?", "Come on, use your words."]
def bad_grandchild
puts "You are such bad grandchild!\n\n"
puts "Say something else!"
print "you >> "
end
def grandma_shouts(cant_hear_array, say_prompts)
puts "grandma: NO, NOT SINCE #{rand(1930..1950)}!\n\n"
puts "#{cant_hear_array.sample}"
puts "#{say_prompts.sample}"
print "you >> "
end
def cant_hear_f(cant_hear_array, goodbye_count)
puts "grandma:\n\n"
puts "#{cant_hear_array[goodbye_count - 1]} Try again."
print "you >> "
end
puts "Grandma is here! What should we say to her?"
print "you >> "
while goodbye_count < 3 do
say = gets.chomp
if say == "BYE"
goodbye_count += 1
if goodbye_count <= 2
puts cant_hear_f(cant_hear_array, goodbye_count)
end
elsif say != say.upcase && say != "bye"
goodbye_count = 0
puts "grandma: HUH? SPEAK UP, SONNY!\n\n"
puts "I don't think she heard you. Do you want me to shout if for you?"
shout = gets.chomp.downcase
case shout
when "yes", "y", "sure", "i guess"
puts "#{say.upcase}\n"
puts grandma_shouts(cant_hear_array, say_prompts)
when "no", "n", "nah", "nope"
puts bad_grandchild
end
elsif say == "bye"
puts "I don't think she heard you. Do you want me to shout if for you?"
shout = gets.chomp.downcase
case shout
when "yes", "y", "sure", "i guess"
goodbye_count += 1
puts "#{say.upcase}\n"
if say != "bye"
puts cant_hear_f(cant_hear_array, goodbye_count)
end
when "no", "n", "nah", "nope"
goodbye_count = 0
puts bad_grandchild
end
else
goodbye_count = 0
puts grandma_shouts(cant_hear_array, say_prompts)
end
end
puts """grandma: WHY DO YOU HAVE TO GO?
YOU HAVE YOUR WHOLE LIFE TO GO SOMEWHERE!
UNGREAFUL PUNK!"""
# Leap years. Write a program that asks for a starting year and an ending year and then puts all the leap years in that range. Leap years are divisible by 4, but not 100, unless also by 400.
def leap (year_array, old_year, new_year)
leap_years = []
year_array.each do |yr|
if (yr % 4) != 0
year_array.delete(yr)
elsif ((yr % 100) == 0) && ((yr % 400) != 0)
year_array.delete(yr)
else
leap_years.push(yr)
year_array.delete(yr)
end
end
if leap_years.length != 0
puts "The years from #{old_year} to #{new_year} are: #{leap_years}"
else
puts "There are no leap years from #{old_year} to #{new_year}"
end
end
puts "Let's calculate all the leap years in a given range.\n\n"
print "Give me a year: "
# asks for a year, checks to see if it is a positve, whole integer, asks for new imput until given the correct imputprint "Give me a starting year: "
check = 0
while check == 0 do
year_a = gets.chomp
if (year_a.to_i.to_s == year_a.to_s) && (year_a.to_i > 0)
a = year_a
check += 1
else
puts "Sorry, I can take whole, positive numbers only.\nGive me a year. For example: 1999"
end
end
# asks for a second year, checks to see if it is a positve, whole integer, asks for new imput until given the correct imput
print "Give me an ending year: "
while check == 1 do
year_b = gets.chomp
if (year_b.to_i.to_s == year_b.to_s) && (year_b.to_i > 0)
check += 1
b = year_b
else
puts "Sorry, I can take numbers only.\nGive me a year. For example: 1647"
end
end
# Checks to see which year, a or b is larger and which is smaller and then assigns them to newest and oldest years respectfuly. If they are the same, check to see if that year is a leap year.
if a > b
new_year = a
old_year = b
elsif b > a
new_year = b
old_year = a
else
new_year = a
old_year = b
same_array = [a.to_i]
end
# Checks for the leap years using the leap function, which is defined at the top ⬆️ of this document
if a == b
leap(same_array, old_year, new_year)
else
year_array = (old_year.to_i..new_year.to_i).to_a
leap(year_array, old_year, new_year)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment