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

marj-ride-share-project #36

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_store
.idea
8 changes: 8 additions & 0 deletions step_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1. What _things_ (objects, nouns) are represented or described in this file? We can think of at least six different things.
A. Driver, Date, Cost, Rider, Rating, One Ride,

2. From the things you listed in the previous question, all of those things have relationships to each other. (an ID belongs to a person, for instance. As an abstract, unrelated example a VIN belongs to a vehicle, and a vehicle has a VIN.) Consider the relationships between the pieces of data.
A. All of the information combines to form the data for one ride. You can find the amount of times or average times the driver or rider has a ride. You can find the average rating a rider gives or the average rating a driver gets. You can see this not just for one driver or rider, but for all as a grouped average. You can count how many rides on a given day, month, or year. You may be able to determine the possible cause for a high or low cost - distance, traffic / time of day, (split cost for ride share if an option)... but that would need more data like a time stamp. You could see if there is a link between cost and ratings. You can find rider trends in travel cost - are there more low cost shorter trips or higher cost long trips (although this may also need more data like start and stop locations).

3. Lastly, in this assignment, we will rearrange all of the data into one data structure (with a lot of nested layers), that can be held in one variable. List some ideas: considering all of the relationships listed in the last question, what piece of data can contain the others at the top-most level? (Compared to the json example before, think about what the top-most layer of the hash and what that represented.) There is more than one correct answer, so just list out the options at this moment.
A. An array can hold the other data at the top-most level, with each hash representing a ride. Since each row represents a ride, I think it best to maintain that structure as much as possible. If I think about the things that I want to get from the data, or what this data may be used to analyse, I still think using the table/column headings as keys in each ride hash is wise. If more columns are added in the future to collect more data, it will be easier to add the column to the ride hashes and the data of that column by following the same pattern.
Comment on lines +1 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

128 changes: 127 additions & 1 deletion worksheet.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
require 'date'
########################################################
# Step 1: Establish the layers

# In this section of the file, as a series of comments,
# create a list of the layers you identify.
# Layer 1 - Array of ride hashes
# Layer 2 - DRIVER_ID, DATE, COST, RIDER_ID, RATING
# Layer 3 - Day, Month, Year

# Which layers are nested in each other?
# Layer 3 is nested in Date of Layer 2, and Layer 2 is nested in Layer 1

# Which layers of data "have" within it a different layer?
# Date of Layer 2

# Which layers are "next" to each other?
# All of the column headings

########################################################
# Step 2: Assign a data structure to each layer

# Copy your list from above, and in this section
# determine what data structure each layer should have
# Layer 1 - Array of ride hashes
# Layer 2 - Hash with keys for DRIVER_ID, DATE, COST, RIDER_ID, RATING
# Layer 3 - Array of ints for Day, Month, Year


########################################################
# Step 3: Make the data structure!
Expand All @@ -23,12 +37,124 @@
# into this data structure, such as "DR0004"
# and "3rd Feb 2016" and "RD0022"

# data structure blueprint
# [
# {
# driver_id: str,
# date: [int, int, int],
# cost: int,
# rider_id: str,
# rating: int
# }
# ]

rides_data = [
['DRIVER_ID','DATE','COST','RIDER_ID','RATING'],
['DR0004','3rd Feb 2016','5','RD0022','5'],
['DR0001','3rd Feb 2016','10','RD0003','3'],
['DR0002','3rd Feb 2016','25','RD0073','5'],
['DR0001','3rd Feb 2016','30','RD0015','4'],
['DR0003','4th Feb 2016','5','RD0066','5'],
['DR0004','4th Feb 2016','10','RD0022','4'],
['DR0002','4th Feb 2016','15','RD0013','1'],
['DR0003','5th Feb 2016','50','RD0003','2'],
['DR0002','5th Feb 2016','35','RD0066','3'],
['DR0004','5th Feb 2016','20','RD0073','5'],
['DR0001','5th Feb 2016','45','RD0003','2']
]
Comment on lines +51 to +64
Copy link
Collaborator

Choose a reason for hiding this comment

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

You have a 2D array here, also called an array of arrays. This works, but you have to use the 1st row for the titles of each field in the subsequent sub-arrays.

You could instead have an array of hashes.

rides_data = [
  {
    driver_id: "DR0004",
    date: "3rd Feb 2016",
    cost: 5,
    rider_id: "RD0022",
    rating: 5,
  },
  ...
]


# return an array with integer representation of dates
def line_break
puts '--------------------------------------------------------'
end

def parse_date(date_string)
day_month_year = []
d = Date.parse(date_string)
return day_month_year << d.mday << d.mon << d.year
end

# create the data structure from the blueprint
def structure_ride_share(data)
top_array = []
# create default hash keys based on column headings
headings = data[0].map { |heading| heading.downcase.to_sym }

(data.length - 1).times do |index|
#skip heading
index += 1

#choose row of data
row = data[index]

#populate ride hashes - really long!
ride = Hash[headings[0], row[0], headings[1], parse_date(row[1]), headings[2], row[2].to_i, headings[3], row[3], headings[4], row[4].to_i]
top_array << ride
end
Comment on lines +91 to +93
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's really cool that you were able to write the code here to structure the data this way. That said, why not structure the data this way to start with since you were entering it manually in the beginning?

Copy link
Author

Choose a reason for hiding this comment

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

Very true. It would have saved me time and energy! I think in my mind, it was an exercise in what I think getting data from an csv file might be like.

return top_array
end

ride_share_data = structure_ride_share(rides_data)
line_break
puts 'Ride Share Data:'
line_break
pp ride_share_data
Comment on lines +97 to +101
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just for readability it's best to put your methods at the top of the file and the main program code all together at the bottom.

In reading this I was seeing

[method definitions]
some lines in the program here
[more method definitions]
more lines in the main program

So the methods here break up the flow of the regular program and make it harder to trace.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the feedback on this! I will re-arrange!


########################################################
# Step 4: Total Driver's Earnings and Number of Rides

# Use an iteration blocks to print the following answers:
def find_unique_values(value_type, data)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

unique_values = data.map { |ride_hash| ride_hash[value_type] }.uniq
return unique_values
end

# - the number of rides each driver has given
def count_total_rides(id, data)
count = data.count { |ride_hash| ride_hash.has_value? id }
return count
end

# - the total amount of money each driver has made
def total_ride_cost(id, data)
total_cost = 0
data.each{ |ride_hash| total_cost += ride_hash[:cost] if ride_hash.value?(id) }
return total_cost
end

# - the average rating for each driver
def calculate_average_rating(id, data)
total_rides = count_total_rides(id, data)
total_ratings = 0.to_f

data.each{ |ride_hash| total_ratings += ride_hash[:rating] if ride_hash.value?(id) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not use reduce or sum_by?

Copy link
Author

Choose a reason for hiding this comment

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

Will look into those methods to do refactoring with. Thank you for bringing my attention to those methods :)


average = total_ratings / total_rides
return average.round(1)
end

line_break
puts 'Driver Summary:'
line_break
driver_summaries = find_unique_values(:driver_id, ride_share_data).map do |driver|
{
driver_id: driver,
total_rides: count_total_rides(driver, ride_share_data),
total_cost: total_ride_cost(driver, ride_share_data),
average_rating: calculate_average_rating(driver, ride_share_data),
}
end
#test
pp driver_summaries
Copy link
Collaborator

Choose a reason for hiding this comment

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

This works to summarize the driver data, but it looks like an array of hashes instead of neat output appealing to an end-user.

Copy link
Author

Choose a reason for hiding this comment

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

:D Yes! During round tables today, another classmate had printed out great summaries for an end user, and I kept thinking, I need to do that for my code too! Thanks~!


# - Which driver made the most money?
# - Which driver has the highest average rating?
line_break
puts 'Driver that made the most money:'
line_break
p driver_summaries.max { |a_hash, b_hash| a_hash[:total_cost] <=> b_hash[:total_cost] }[:driver_id]

# - Which driver has the highest average rating?
line_break
puts 'Driver that has the highest average rating:'
line_break
p driver_summaries.max { |a_hash, b_hash| a_hash[:rating] <=> b_hash[:rating] }[:driver_id]