How do Uber and Lyft figure out how much to charge passengers for their rides? They have a set of instructions (an algorithm) that takes in important information like distance, time, traffic, and number of passengers to figure out what price to set for a trip.
In this lab, you'll be writing ruby methods to calculate fares for various ride services. Take a look at this chart, which provides the most recent fares for Uber services.
Initial Fee | Price Per Mile | Price Per Minute | Service Cost | |
---|---|---|---|---|
uberX | $0.40 | $0.97 | $0.14 | $1.58 |
uberXL | $2.15 | $1.68 | $0.26 | $1.70 |
UberSELECT | $4.02 | $2.17 | $0.33 | $1.70 |
UberBLACK | $7.22 | $3.33 | $0.44 | $1.80 |
- Create a function called
simple_uberx_fare
that only accepts miles and returns the fare based on that number of miles.
Example: When I run simple_uberx_fare(10)
for 10 miles, it should return $9.70 because the uberX price per mile is $0.97.
- Create a function called
uberx_fare_calculator
that takes in both the distance and the amount of time for the ride. It should integrate the initial fee, price per mile, price per minute and the service cost to return a estimated fare for a ride.
Example: For a ride that is 2 miles and 10 minutes: uberx_fare_calculator(2, 10)
should return $5.32 ($0.40 initial fee, 2 miles at $0.97/mile, 10 minutes at $0.14/minute, $1.58 service cost)
- Create a more flexible fare calculator method called
fare_calculator
that takes in the distance, the time, and the type of ride.
Example: For a ride that is 2 miles and 10 minutes in an UberXL: fare_calculator(2, 10, "UberXL")
should return $9.81 ($2.15 initial fee, 2 miles at $1.68/mile, 10 minutes at $0.26/minute, $1.70 service cost)
Bonus: if no car type is given, have fare_calculator
default to "UberX"
Advanced Bonus Challenge:
- Uber, Lyft or Cab?: Look up the fares of using Lyft and yellow cabs in New York City and create a method
cheapest_ride
that accepts the distance and the time as parameters, and returns the cheapest service to take for that trip, along with the fare estimate. This is an open ended one - there are many ways to do this!