We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The simple line needed to complete the skeleton function is…
t = reduce(Fraction.__mul__, fracs)
Let Python do the work. There's no need to construct a lambda.
There are several solutions posted in this forum that do the reduction using the Fraction constructor to do the multiplication. It works, but it requires a little bit of special knowledge to set up the calculation and to return the result. The reduction must start with 1, to get the reciprocal of the first fraction. After the reduction, the reciprocal is returned by giving the numerator and denominator in the opposite order. Although the solution is clever and somewhat smaller, i don't recommend it, because the way it works isn't obvious to most people and is therefore difficult to maintain
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Reduce Function
You are viewing a single comment's thread. Return to all comments →
The simple line needed to complete the skeleton function is…
Let Python do the work. There's no need to construct a
lambda
.There are several solutions posted in this forum that do the reduction using the
Fraction
constructor to do the multiplication. It works, but it requires a little bit of special knowledge to set up the calculation and to return the result. The reduction must start with1
, to get the reciprocal of the first fraction. After the reduction, the reciprocal is returned by giving the numerator and denominator in the opposite order. Although the solution is clever and somewhat smaller, i don't recommend it, because the way it works isn't obvious to most people and is therefore difficult to maintain