Skip to content

Instantly share code, notes, and snippets.

@nthj
Created December 10, 2013 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nthj/7895198 to your computer and use it in GitHub Desktop.
Save nthj/7895198 to your computer and use it in GitHub Desktop.
# Quiz Exercise, Part One
#
# Quiz for a list of names & birthdays,
# then list out their ages
#
# For example:
#
# Hey there! Please enter a list of names:
# > John, Sam, Bob, Alisha, Henry
#
# Thanks. When is John's birthday? 10/13/2000
#
# OK, and Sam's birthday? 8/3/1987
# And Bob's? 2/17/1957
# Alisha's? 4/21/1978
# Henry's? 6/23/1980
#
# OK, all done. You have 5 guests. Here are their ages:
#
# * John: 13
# * Sam: 26
# * Bob: 56
# * Alisha: 35
# * Henry: 33
#
# Enjoy your party!
#
##
## OUTLINE
##
# We're going to want to break the problem down into steps.
# First, let's ask for the user's input:
puts "Hey there! Please enter a list of names:"
guest_names = 'John, Sam, Bob, Alisha, Henry' # TODO: ask for input
# guest_names = gets.chomp
# We want to loop (or iterate) over each name.
# * What type of data do we have in the `guest_names` variable?
# * What type of data supports iteration?
# * How do we transform the first into the second?
list_of_guest_names = guest_names.split(',').map do |name|
name.strip
end
guests = list_of_guest_names.map do |guest_name|
guest_birthday = '10/27/1978' # TODO: ask them their birthday
is_bringing_a_gift = rand(2).to_i.zero? ? true : false
how_many_friends = rand(2).to_i
age = Time.now.year - guest_birthday.split('/').last.to_i
{
:age => age, # TODO: calculate this based on their birthday,
:gift => is_bringing_a_gift,
:friends => how_many_friends,
:name => guest_name
}
end
number_of_gifts = guests.select { |g| g[:gift] }.length
number_of_people = guests.length + guests.reject do |guest|
raise 'only 0 or 1 friends' if guest[:friends] > 1
guest[:friends].zero? # right now they can only bring 1 friend,
# if we let them bring more later,
# this logic will fail
end.length
# We want to display the data onto the screen.
#
# * How many guests do we have total?
# * What are their names and ages?
puts "All done. You have #{guests.length} guests with friends for a total of #{number_of_people} people."
puts "#{number_of_gifts} of them are bringing gifts."
puts "Here is everyone's ages: "
# TODO: loop over each guest
guests.each do |guest|
puts "* #{guest[:name]} is #{guest[:age]} years old"
end
##
## EXTENSION #1
##
# Ask for a few more pieces of information:
#
# * Are they bringing a gift? (true or false)
# * Display how many people are bringing a gift.
##
## EXTENSION #2
##
# * How many friends are they bringing? (0, 1)
# * Update the "number of guests" to take the friends' numbers into account:
#
# "You have 5 guests who are bringing 2 friends for a total of 7 people."
##
## EXTENSION #3
##
# Calculate how much the party will cost.
# * Estimate $5 for each person, including the friends that guests are bringing.
# * Add an extra $3 for each person over 21, as they will drink our alcohol
# * Provide a $2 discount for each senior citizen
##
## EXTENSION #4
##
# Display a receipt for the total party cost
# * Display each person as a line-item ($5)
# * Display each alcohol addition
# * Display each discount
# * `rjust` or `ljust` (string methods) each column and value so everything lines up nicely
# EXAMPLE:
# * Nathaniel: $5
# * John: $5
# * Sam: $5
# * John Alcohol: $3
# * Sam Alcohol: $3
# * John Senior: ($2)
# * Subtotal: $19
# * Tax: $1.90
# * Total: $21.90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment