CodeSignal put out a challenge by Uber that involves writing a "fare estimator" that involves a function dependent on cost per minute, cost per mile, ride time and ride distance.
The formula is along the lines of...
Given that: cₐ stands for cost per minute, rₐ stands for ride time, cₑ stands for cost per mile and rₑ ride distance, the estimation formula is: $$E = c_a \cdot r_a + c_e \cdot r_e $$
My solution:
/* CodeSignal Company Challenges
*
* A solution to Uber's "fareEstimator" challenge
* By A. S. "Aleksey" Ahmann, [email protected]
*
*/
double estimation_forumla(int ride_time, int ride_distance, double cost_per_minute, double cost_per_mile) {
return (cost_per_minute * ride_time) + (cost_per_mile * ride_distance);
}
vector<double> fareEstimator(int ride_time, int ride_distance, vector<double> cost_per_minute, vector<double> cost_per_mile) {
vector<double> estimates;
for (int i = 0; i < cost_per_minute.size(); i++)
estimates.insert(estimates.end(), estimation_forumla(ride_time, ride_distance, cost_per_minute[i], cost_per_mile[i]));
return estimates;
}
I initially solved this in Python and ported it to C++ whilst taking into account the feedback of other programmers on that question.
They recommended not using any fancy code golf, as shorter code usually implies difficulty to maintain but does not imply faster. In C++, it's more difficult to "code golf" my solution, so no fancy tricks there (I hope). They also recommended that I use better names to my variables. Do you feel that I did a decent job naming my variables?
Finally, they recommended that I use type hints. I believe that the newer versions of C++ allow for type inference (though please correct me on that if I'm wrong :P), but I decided to declare variables by their type.
Do you feel that I was able to follow the advice given and did better than my initial attempt? Also, just give me any feedback that you have for my code. What could I have done better? What did you like (or dislike) about it? Let me know!
You can check out the full challenge (but you might need to sign up).
And here's my solution on GitHub if you're interested.