Skip to content

Instantly share code, notes, and snippets.

@johnkferguson
Created February 7, 2013 23:58
Show Gist options
  • Save johnkferguson/4735374 to your computer and use it in GitHub Desktop.
Save johnkferguson/4735374 to your computer and use it in GitHub Desktop.
# FizzBuzz - The Programmer's Stairway to Heaven
# Define the fizzbuzz method to do the following: 10pts
# Use the modulo % method (divisible by)
# 2 % 2 #=> true
# 1 % 2 #=> false
# If a number is divisible by 3, puts "Fizz".
# If a number is divisible by 5, puts "Buzz".
# If a number is divisible by 3 and 5, puts "FizzBuzz"
# Use if statements 2pts
# Use the && operator 3pts
# Write a loop that will group the numbers from 1 through 50
# by whether they fizz, buzz, or fizzbuzz - 10pts
def fizzbuzz(int)
if int % 5 == 0 && int % 3 == 0
puts "Fizzbuzz"
elsif int % 3 == 0
puts "Fizz"
elsif int & 5 == 0
puts "Buzz"
else
puts int
end
end
fizzbuzz(49)
for i in 1..50
# (1..50).each do
# puts #{i}
# end
fizzbuzz = []
fizz = []
buzz = []
for i in 1..50 do
if i % 5 == 0 && i % 3 == 0
fizzbuzz << i
elsif i % 3 == 0
fizz << i
elsif i % 5 == 0
buzz << i
end
end
fizzbuzz
fizz
buzz
# quiz.rb
# Write a program that tells you the following:
#
# Hours in a year. How many hours are in a year? - 6pts
# Minutes in a decade. How many minutes are in a decade? - 6pts
# Your age in seconds. How many seconds old are you? - 6pts
#
# Define at least the following methods to accomplish these tasks:
#
# seconds_in_minutes(1) #=> 60 - 3pts
# minutes_in_hours(1) #=> 60 - 3pts
# hours_in_days(1) #=> 24 - 3pts
# days_in_weeks(1) #=> 7 - 3pts
# weeks_in_years(1) #=> 52 - 3pts
#
# If I am 1,111 million seconds old, how old am I?
# Define an age_from_seconds method - 7pts
def seconds_in_minutes(mins = 1)
mins * 60
end
def minutes_in_hours(hrs = 1)
hrs * 60
end
def hours_in_days(day = 1)
day * 24
end
def days_in_week(week = 1)
week * 7
end
def weeks_in_years(year = 1)
year * 52
end
def days_in_years(year = 1)
year * 365
end
hours_in_a_year = hours_in_days(days_in_years(1))
minutes_in_decade = minutes_in_hours(hours_in_days(days_in_years(10)))
age_in_seconds = seconds_in_minutes(minutes_in_hours(hours_in_days(days_in_years(29))))
# def age_from_seconds (seconds)
# seconds / 60 / 60 / 24 / 365
# end
def age_from_seconds (seconds)
seconds / seconds_in_minutes / minutes_in_hours / hours_in_days / days_in_years
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment