Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time - Angela #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions rideshare.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# RIDESHARE
# Calculates statistics of rideshare data.

drivers = {
DR0001: [
{
"3rd Feb 2016": [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One note about a confusing point of Ruby. Even if you use quotes if you use the "key": value syntax instead of the "key" => value the key will wind up a symbol.

Note the : before the quotes in the output.

irb(main):001:0> {"3rd Feb 2016": []}
=> {:"3rd Feb 2016"=>[]}

(Yes, symbols can have spaces, you just need to use quotes after the :.)

{
cost: 10,
rider_id: "RD0003",
rating: 3,
},
{
cost: 30,
rider_id: "RD0015",
rating: 4,
},
],
},
{
"5th Feb 2016": [
{
cost: 45,
rider_id: "RD0003",
rating: 2,
},
],
},
],
DR0002: [
{
"3rd Feb 2016": [
{
cost: 25,
rider_id: "RD0073",
rating: 5,
},
],
},
{
"4th Feb 2016": [
{
cost: 15,
rider_id: "RD0013",
rating: 1,
},
],
},
{
"5th Feb 2016": [
{
cost: 35,
rider_id: "RD0066",
rating: 3,
},
],
},
],
DR0003: [
{
"4th Feb 2016": [
{
cost: 5,
rider_id: "RD0066",
rating: 5,
},
],
},
{
"5th Feb 2016": [
{
cost: 50,
rider_id: "RD0003",
rating: 2,
},
],
},
],
DR0004: [
{
"3rd Feb 2016": [
{
cost: 5,
rider_id: "RD0022",
rating: 5
},
],
},
{
"4th Feb 2016": [
{
cost: 10,
rider_id: "RD0022",
rating: 4
},
],
},
{
"5th Feb 2016": [
{
cost: 20,
rider_id: "RD0073",
rating: 5,
},
],
},
],
}

highest_earner = {
driver: "",
amount_earned: 0,
}

highest_rated = {
driver: "",
rating: 0,
}

puts "STATS PER DRIVER\n**********\n\n"
drivers.each do | driver, workdays |
puts "#{driver} stats:"

# The number of rides each driver has given
ride_count = workdays.sum { |dates, rides| dates.sum { | date, rides | rides.count } }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of a block with .sum! This lets you skip using a .map.

This is equivalent to doing:

   ride_count = workdays.map { |dates, rides| dates.sum { | date, rides | rides.count } }.sum

puts "Number of rides given: #{ride_count}"

# The total amount of money each driver has made
money_earned = workdays.sum { |dates, rides| dates.sum { | date, rides | rides.sum { |ride| ride[:cost] } } }
puts "Total earned: $#{money_earned}"
if money_earned > highest_earner[:amount_earned]
highest_earner[:driver] = driver
highest_earner[:amount_earned] = money_earned
end

# Average rating
total_ratings = workdays.sum { |dates, rides| dates.sum { | date, rides | rides.sum { |ride| ride[:rating] } } }
average_rating = (total_ratings.to_f/ride_count.to_f).round(2)
puts "Average rating: #{average_rating}"
if average_rating > highest_rated[:rating]
highest_rated[:driver] = driver
highest_rated[:rating] = average_rating
end
puts "\n"
end

# Which driver made the most money?
puts "\nHighest earner: #{highest_earner[:driver]}"

# Which driver has the highest average rating?
puts "Highest rated: #{highest_rated[:driver]}"