You want to decide whether you should drive your car to work or take the train. You know the one-way distance from your home to your place of work, and the fuel efficiency of your car (in miles per gallon). You also know the one-way price of a train ticket. You assume the cost of gas at $4 per gallon, and car maintenance at 5 cents per mile. Write an algorithm to decide which commute is cheaper.
Assume the cheapest commute is taking the train
Initial one-way price of a train ticket
Initial one-way cost of self-driving
Initial one-way distance
Initial the fuel efficiency is a gallon per distance
Initial the cost of gas per gallon is 4
Initial the cost of car maintenance per mile is 5c (0.05)
One-way cost of self-driving = one-way distance / fuel efficiency * the cost of gas + one-way distance * 0.05
If the one-way cost of self-driving is less than the one-way price of a train ticket
The cheapest commute is self-driving
Else if one-way cost of self-driving is equal to the one-way price of a train ticket
The cheapest commute is both
Else the cheapest commute is taking the train