Skip to content

Instantly share code, notes, and snippets.

@jamesmichiemo
Last active December 18, 2015 10:10
Show Gist options
  • Save jamesmichiemo/5767003 to your computer and use it in GitHub Desktop.
Save jamesmichiemo/5767003 to your computer and use it in GitHub Desktop.
expressions in Ruby language with Rails support
require 'active_support/core_ext/enumerable.rb' # <= enables the use of array.sum with rails installed. (bit.ly/mCblb)·
# Marathon
print "Jon, Carla, Jim, and Harry won lottery tickets to run the NYC marathon. The runners now need to get sponsers so together they've g athered their running stats to see if they qualify for the money. The money people would like to know the average pace between the lotter y winners to get an idea of how fast their team is. Calculate the average time it takes for athlete runners to finish a marathon.\n\n"
distance = 26.2 # <= marathon distance in miles
runnersPace = []
print "At what pace(minutes per mile) does Jon run a marathon? "
paceJon = gets.chomp().to_f # <= 6 mph
runnersPace.push(paceJon) # => [6]
print "At what pace(minutes per mile) does Carla run a marathon? "
paceCarla = gets.chomp().to_f # <= 6 mph
runnersPace.push(paceCarla) # => [6, 6]
print "At what pace(minutes per mile) does Jim run a marathon? "
paceJim = gets.chomp().to_f # <= 9 mph
runnersPace.push(paceJim) # => [6, 6, 9]
print "At what pace(minutes per mile) does Harry run a marathon? "
paceHarry = gets.chomp().to_f # <= 7 mph
runnersPace.push(paceHarry) # => [6, 6, 9, 7]
paceAvg = runnersPace.sum / runnersPace.length # => 7
timeAvg = distance * paceAvg # => 183.4
timeAvgHours = (timeAvg / 60).to_i # => # => 3
timeAvgMins = (timeAvg % 60).to_i # => 3
puts "The average time to finish a marathon between Jon, Carla, Jim, and Harry is #{timeAvgHours} hours and #{timeAvgMins} mins."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment