Skip to content

Update worksheet.rb#33

Open
roshni-patel wants to merge 1 commit intoAda-C14:masterfrom
roshni-patel:master
Open

Update worksheet.rb#33
roshni-patel wants to merge 1 commit intoAda-C14:masterfrom
roshni-patel:master

Conversation

@roshni-patel
Copy link

Assignment Submission: Ride Share

Congratulations! You're submitting your assignment. Please reflect on the assignment with these questions.

Reflection

Question Answer
What did your data structure look like at first? Did this structure evolve over time? Why? Because I had spent time reviewing the roundtable nested data structures exercise and working through the worksheet questions, I was able to recognize that my initial idea of creating an array of hashes where each ride is represented by a hash would not be ideal as it does not have as many layers as it should for the type of data we are working with in this project. At that point, I realized that having an additional layer, particularly one related to organizing driver information as most, if not all, of the questions were focused on the drivers, could be helpful in more efficiently accessing the data needed to answer the questions. My final data structure consisted of a hash whose keys were the drivers’ ids and values were arrays of the rides that contained hashes for each individual ride along with its details (rider id, cost, date, rating).
What was your strategy for going through the data structure and gathering information? My strategy for going through the data structure and gathering information was to create different methods for each question and to use .each and .map for iteration and .sum and .length for some of the other calculations.
What was an example of something that was necessary to store in a variable? Why was it necessary, useful, or helpful? I found that storing information in a variable was particularly useful when determining the driver with the highest average rating, the driver with the highest total earnings, and the day for which each driver earned the most money. By initializing these variables I could compare them with the corresponding values in the hashes and keep updating the values for highest average rating, highest total earnings, and the day for which each driver earned the most money based on those comparisons. Storing this information in a variable was also helpful for being able to print the information outside of the loop.
What kinds of iteration did you use? Did you use .map? If so, when? If not, why, or when would be a good opportunity to use it? I primarily used .each and .map for iteration. I used .map when calculating the number of rides each driver has given, the total earnings for each driver, and the average rating for each driver.
Were some calculations easier than others? Why? Yes and no. I think that, of course, calculating the number of rides each driver has given was easier than calculating the driver with the highest average rating, for example, because there are fewer layers that you have to work through in order to access the data you are interested in. But once I had a better understanding of my data structure and how it worked, I found that, although the calculations for the various questions were different, they were not necessarily more or less difficult than the other ones.

Copy link
Collaborator

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

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

Nice work, you hit all the learning goals here. Well done. The only minor thing I would suggest is to make the methods do the calculation and just return the answer. Then your main program can print the output. That way your methods are doing 1 thing. Currently they are doing calculation and output. Otherwise this is outstanding. Well done.

Comment on lines +5 to +7
# There are three layers: all data for all rides, data for drivers, and data for rides
# Things: driver id, rider id, cost, date, rating, rides
# Relationships: driver id, rides belong to a driver; rider id, cost, date, rating belong to a ride, etc.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The file is a 2D data structure rows and columns. The whole file is a list of ride data. Each row is information about each trip.

Comment on lines +11 to +16
# Within the outermost layer, the drivers hash, there are two additional layers.
# There is an array layer that contains information about the trips completed by each driver.
# There is also a hash layer that contains details about each of those trips (i.e. rider id, date, cost, rating).
# Similarly, the middle array layer can also be considered to have within it a different layer
# (i.e. the hash layer that contains the specific details for the trips). The outermost drivers hash layer is next to
# the middle trips array layer which is next to the innermost hash layer containing details of the trips.
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

# into this data structure, such as "DR0004"
# and "3rd Feb 2016" and "RD0022"

drivers = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

Comment on lines +138 to +147
puts "====================== NUMBER OF RIDES ======================"
num_rides(drivers)

# The total amount of money each driver has made
def total_earnings(drivers)
drivers.map do |driver, rides|
total = rides.map { |ride| ride[:cost]}
puts "#{driver} earned a total of $#{'%.2f' % total.sum}"
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 , Nice use of nested map and sum.

Comment on lines +150 to +159
puts "====================== TOTAL EARNINGS ======================="
total_earnings(drivers)

# The average rating for each driver
def average_rating(drivers)
drivers.map do |driver, rides|
average_rating = ((rides.sum { |ride| ride[:rating] }) / rides.length.to_f)
puts "#{driver}'s average rating is #{'%.2f' % average_rating}"
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice use of map and sum.

Comment on lines +162 to +177
puts "====================== AVERAGE RATING ========================"
average_rating(drivers)

# Which driver made the most money?
def highest_earning_driver(drivers)
highest_earnings = 0
highest_earner = ""
drivers.each do |driver, rides|
earnings = rides.map { |ride| ride[:cost] }.sum
if earnings > highest_earnings
highest_earnings = earnings
highest_earner = driver
end
end
puts "#{highest_earner} made the most money for a total of $#{'%.2f' % highest_earnings}"
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

highest_earning_driver(drivers)

# Which driver has the highest average rating?
def highest_average_rating(drivers)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you use max_by here somehow?

Comment on lines +202 to +214
def best_day(drivers)
drivers.each do |driver, rides|
highest_daily_earnings = 0
best_earnings_day = ""
rides.each do |ride|
if ride[:cost] > highest_daily_earnings
best_earnings_day = ride[:date]
highest_daily_earnings = ride[:cost]
end
end
puts "#{driver}'s best day was #{best_earnings_day} when they earned $#{'%.2f' % highest_daily_earnings}"
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also a place you could use max_by

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants