Skip to content

Instantly share code, notes, and snippets.

@jefferyhsu
Created July 11, 2012 23:31
Show Gist options
  • Save jefferyhsu/3094428 to your computer and use it in GitHub Desktop.
Save jefferyhsu/3094428 to your computer and use it in GitHub Desktop.
Intro to Ruby: Quiz 1 (from Jeff Hsu)
# 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(seconds)
seconds * 60.0
end
def minutes_in_hours(minutes)
minutes * 60.0
end
def hours_in_days(hours)
hours * 24.0
end
def hours_in_years
24 * 365
end
def days_in_weeks(days)
days * 7.0
end
def weeks_in_years(weeks)
weeks * 52.0
end
# Hours in a year. How many hours are in a year? - 6pts
print "There are " + hours_in_years.to_s + " hours in a year."
print "\n"
# Minutes in a decade. How many minutes are in a decade? - 6pts
print "There are " + (10*weeks_in_years(days_in_weeks(hours_in_days(minutes_in_hours(1))))).to_s + " minutes in a decade."
print "\n"
# Your age in seconds. How many seconds old are you? - 6pts
def age_in_seconds(years)
years * weeks_in_years(days_in_weeks(hours_in_days(minutes_in_hours(seconds_in_minutes(1)))))
end
print "If you are 25 years old, you are " + age_in_seconds(25).to_s + " seconds old."
print "\n"
# If I am 1,111 million seconds old, how old am I?
def age_from_seconds(seconds)
seconds / weeks_in_years(days_in_weeks(hours_in_days(minutes_in_hours(seconds_in_minutes(1)))))
end
print "If I am 1,111 million seconds old, how old am I?"
print "\n"
print "You are " + age_from_seconds(1111000000).to_s + " years old."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment