Skip to content

Instantly share code, notes, and snippets.

@loganhasson
Created September 26, 2013 00:53
Show Gist options
  • Save loganhasson/6708374 to your computer and use it in GitHub Desktop.
Save loganhasson/6708374 to your computer and use it in GitHub Desktop.
Playlist and Quiz from Ruby Lecture 1
##### PLAYLIST #####
# Given:
# 5 songs of the following lengths in seconds
# 223,215,378,324,254
# Goals:
# Assign the length set to variables
# Calculate the Total Length of the Playlists
# Express the Length in Minutes
# Average Song Length in Minutes
song_1 = 223
song_2 = 215
song_3 = 378
song_4 = 324
song_5 = 254
def get_playlist_info(*songs)
length_secs = 0
songs.each { |song| length_secs += song }
length_mins = length_secs / 60
avg_length_mins = length_mins / songs.length
extra_seconds_total = length_secs % 60
extra_seconds_avg = avg_length_mins % 60
puts "Your playlist is #{length_mins.floor} minutes, #{extra_seconds_total} seconds long."
puts "The average song length is #{avg_length_mins.floor} minutes, #{extra_seconds_avg} seconds."
end
get_playlist_info(song_1, song_2, song_3, song_4, song_5)
##### QUIZ #####
# Write a program that tells you the following:
#
# Hours in a year. How many hours are in a year?
# Minutes in a decade. How many minutes are in a decade?
# Your age in seconds. How many seconds old are you?
#
# Define at least the following methods to accomplish these tasks:
#
# seconds_in_minutes(1)
# minutes_in_hours(1)
# hours_in_days(1)
# days_in_weeks(1)
# weeks_in_years(1)
#
# If I am 1,111 million seconds old, how old am I?
# Define an age_from_seconds method
def seconds_in_minutes(minutes)
minutes * 60
end
def minutes_in_hours(hours)
hours * 60
end
def hours_in_days(days)
days * 24
end
def days_in_weeks(weeks)
weeks * 7
end
def weeks_in_years(years)
years * 52
end
def days_in_years(years)
years * 365
end
def age_from_seconds(age)
age / seconds_in_minutes(1) / minutes_in_hours(1) / hours_in_days(1) / days_in_weeks(1) / weeks_in_years(1)
end
puts "How many hours are in a year?"
puts hours_in_days(days_in_weeks(weeks_in_years(1)))
puts "How many minutes are in a decade?"
puts minutes_in_hours(hours_in_days(days_in_weeks(weeks_in_years(10))))
puts "How many seconds old are you?"
puts seconds_in_minutes(minutes_in_hours(hours_in_days(days_in_weeks(weeks_in_years(24)))))
puts "If I am 1,111 million seconds old, how old am I?"
puts "I am #{age_from_seconds(1111000000)} years old."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment