forked from AdaGold/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 48
Space - Cathy #24
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
Open
cojenco
wants to merge
1
commit into
Ada-C13:master
Choose a base branch
from
cojenco:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Space - Cathy #24
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # create drivers {hash} as a single variable data structure | ||
| # each driver id is a key in the hash while the value is an [array] that holds each ride info in a {hash} | ||
|
|
||
| drivers = { | ||
| "DR0001": [ | ||
| { | ||
| :date => "3rd Feb 2016", | ||
| :cost => 10, | ||
| :rider_id => "RD0003", | ||
| :rating => 3 | ||
| }, | ||
| { | ||
| :date => "3rd Feb 2016", | ||
| :cost => 30, | ||
| :rider_id => "RD0015", | ||
| :rating => 4 | ||
| }, | ||
| { | ||
| :date => "5th Feb 2016", | ||
| :cost => 45, | ||
| :rider_id => "RD0003", | ||
| :rating => 2 | ||
| } | ||
| ], | ||
| "DR0002": [ | ||
| { | ||
| :date => "3rd Feb 2016", | ||
| :cost => 25, | ||
| :rider_id => "RD0073", | ||
| :rating => 5 | ||
| }, | ||
| { | ||
| :date => "4th Feb 2016", | ||
| :cost => 15, | ||
| :rider_id => "RD0013", | ||
| :rating => 1 | ||
| }, | ||
| { | ||
| :date => "5th Feb 2016", | ||
| :cost => 35, | ||
| :rider_id => "RD0066", | ||
| :rating => 3 | ||
| } | ||
| ], | ||
| "DR0003": [ | ||
| { | ||
| :date => "4th Feb 2016", | ||
| :cost => 5, | ||
| :rider_id => "RD0066", | ||
| :rating => 5 | ||
| }, | ||
| { | ||
| :date => "5th Feb 2016", | ||
| :cost => 50, | ||
| :rider_id => "RD0003", | ||
| :rating => 2 | ||
| } | ||
| ], | ||
| "DR0004": [ | ||
| { | ||
| :date => "3rd Feb 2016", | ||
| :cost => 5, | ||
| :rider_id => "RD0022", | ||
| :rating => 5 | ||
| }, | ||
| { | ||
| :date => "4th Feb 2016", | ||
| :cost => 10, | ||
| :rider_id => "RD0022", | ||
| :rating => 4 | ||
| }, | ||
| { | ||
| :date => "5th Feb 2016", | ||
| :cost => 20, | ||
| :rider_id => "RD0073", | ||
| :rating => 5 | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| # create a method to organize and generate calculation into a new set of array | ||
| # this method creates an array of hashes that includes driver id, total rides, total cost, average rating per driver | ||
| def get_organized_data(hash_entered) | ||
| data_per_driver = [] | ||
|
|
||
| hash_entered.each do |driver, rides| | ||
| cost_sum = 0 | ||
| rating_sum = 0.0 | ||
|
|
||
| rides.each do |ride| | ||
| cost_sum += ride[:cost] | ||
| rating_sum += ride[:rating] | ||
| end | ||
|
|
||
| data_per_driver << {:driver_id => driver, :total_rides => rides.length, :total_money => cost_sum, :avg_rating => (rating_sum/rides.length).round(1)} | ||
| end | ||
|
|
||
| return data_per_driver | ||
| end | ||
|
|
||
| # create a method to sum up money made each day per driver and get the date they made the most money | ||
| def get_max_money_date(array_entered) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really nice use of methods! |
||
| daily = {} | ||
|
|
||
| array_entered.each do |ride| | ||
| if daily.key?(ride[:date]) == false | ||
| daily[ride[:date]] = 0 | ||
| end | ||
|
|
||
| daily[ride[:date]] += ride[:cost] | ||
| end | ||
|
|
||
| max_money_day = nil | ||
| max_daily_sum = 0 | ||
|
|
||
| daily.each do |date, money| | ||
| if money >= max_daily_sum | ||
| max_daily_sum = money | ||
| max_money_day = date | ||
| end | ||
| end | ||
|
|
||
| return max_money_day | ||
| end | ||
|
|
||
| # call method and store into a variable | ||
| organized_data = get_organized_data(drivers) | ||
|
|
||
| # output data using forementioned variable and an iterator | ||
| organized_data.length.times do |i| | ||
| puts "Driver #{organized_data[i][:driver_id]} has given a total of #{organized_data[i][:total_rides]} rides" | ||
| puts "Driver #{organized_data[i][:driver_id]} has made a total of $#{organized_data[i][:total_money]}" | ||
| puts "Driver #{organized_data[i][:driver_id]} has an average rating of #{organized_data[i][:avg_rating]}" | ||
| end | ||
|
|
||
| # find which driver made the most money using an enumerable and generate result | ||
| max_money = organized_data.max_by{|each_hash| each_hash[:total_money]} | ||
| puts "The driver that made the most money with $#{max_money[:total_money]} is #{max_money[:driver_id]}" | ||
|
|
||
| # find which driver had highest rating using an enumerable and generate result | ||
| max_rating = organized_data.max_by{|each_hash| each_hash[:avg_rating]} | ||
| puts "The driver that has the highest average rating of #{max_rating[:avg_rating]} is #{max_rating[:driver_id]}" | ||
|
Comment on lines
+130
to
+142
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section is clear, concise and I love it! |
||
|
|
||
| # call method and use a loop to output which date each driver made the most money | ||
| drivers.each do |driver, rides| | ||
| puts "#{driver} made the most money on #{get_max_money_date(rides)}" | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, small thing, but the name of this file made things tricky!
you named the file using spaces, which meant I couldn't easily run it from the command line, and you also forgot to make the file type
.rb. Not a huge deal, and might not come up again, but I figure it's good to know.