Easy 0 solved 0% acceptance

Driving Costs

Work out the fuel cost of several trips from a car's miles per gallon and the gas price.

What you'll learn

  • Convert miles to gallons using miles per gallon
  • Turn gallons into dollars
  • Build a result list in the same order as the input

Problem

Before a road trip, Maya wants to know what each leg will cost in gas. Her car goes miles_per_gallon miles on one gallon, and gas costs dollars_per_gallon dollars a gallon. A trip of m miles uses m / miles_per_gallon gallons, and each gallon costs dollars_per_gallon.

Implement driving_costs(miles_per_gallon, dollars_per_gallon, trips).

Input

miles_per_gallon: How many miles the car drives on one gallon (greater than 0).

dollars_per_gallon: The price of one gallon in dollars.

trips: The length of each trip in miles.

Output

Return the cost in dollars of each trip, in the same order as trips. Do not round; answers are checked to within 0.000001. An empty list of trips gives an empty list.

Constraints

0 < miles_per_gallon ≤ 200

0 ≤ dollars_per_gallon ≤ 20

0 ≤ length of trips ≤ 100, each trip between 0 and 10,000 miles

Examples

Example 1
Input:
miles_per_gallon = 25.0
dollars_per_gallon = 3.2
trips = [10.0, 50.0, 400.0]

Expected output:
expected = [1.2800000000000002, 6.4, 51.2]
Input:
double miles_per_gallon = 25.0;
double dollars_per_gallon = 3.2;
std::vector<double> trips = {10.0, 50.0, 400.0};

Expected output:
std::vector<double> expected = {1.2800000000000002, 6.4, 51.2};
Input:
double miles_per_gallon = 25.0;
double dollars_per_gallon = 3.2;
double[] trips = new double[] {10.0, 50.0, 400.0};

Expected output:
double[] expected = new double[] {1.2800000000000002, 6.4, 51.2};

Explanation

10 miles uses 0.4 gallons, which costs 0.4 × 3.2 = 1.28 dollars; 50 miles costs 6.4 and 400 miles costs 51.2.

Example 2
Input:
miles_per_gallon = 40.0
dollars_per_gallon = 4.0
trips = [0.0, 20.0]

Expected output:
expected = [0.0, 2.0]
Input:
double miles_per_gallon = 40.0;
double dollars_per_gallon = 4.0;
std::vector<double> trips = {0.0, 20.0};

Expected output:
std::vector<double> expected = {0.0, 2.0};
Input:
double miles_per_gallon = 40.0;
double dollars_per_gallon = 4.0;
double[] trips = new double[] {0.0, 20.0};

Expected output:
double[] expected = new double[] {0.0, 2.0};

Explanation

A 0 mile trip costs nothing, and 20 miles uses half a gallon, so [0.0, 2.0].

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Gallons used is the distance divided by miles per gallon.

Hint 2

Multiply the gallons by the price per gallon.

Hint 3

Do the same calculation for every trip and keep the order.

Expected complexity

O(n) time, O(n) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

The best passing solution of each coder. Faster, smaller and better-reviewed solutions rank higher.

No passing solutions yet. Solve it to take first place.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.