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.
Could you or someone provide an explanation for what is going on here? fracs comes out to something like [Fraction(1, 2), Fraction(3, 4), Fraction(5, 3)]. Does this multiply each fraction due to the operator.mul and then simplify them? If so, how does it simplify them? And are numerator and denominator fields on the Fraction type?
The Fraction type has a __mul__() method, which takes two Fractions and returns a Fraction as a result. It will always return the simplest (lowest common denominator) version of the faction. Any class with such a method is telling Python how to multiply it. So you can type
Fraction('1/2')*Fraction('1/3')
or
mul(Fraction('1/2'),Fraction('1/3'))
and get Fraction('1/6') as a result. mul() is just the equivalent of lambda x, y: x * y
what is the meaning of t.numerator, t.denominator??
we do not have any numerator and denominator defined.
can u explait what is the meaning of this line
As stated in the __main__, user <wilmer_henao> assumes that you know the elements of frac are instances of the Fraction class. Feel free to read the Python API on it. Alternatively, here is my more explicit HackerRank replacement.
Reduce Function
You are viewing a single comment's thread. Return to all comments →
easy 1 line
Could you or someone provide an explanation for what is going on here? fracs comes out to something like [Fraction(1, 2), Fraction(3, 4), Fraction(5, 3)]. Does this multiply each fraction due to the operator.mul and then simplify them? If so, how does it simplify them? And are numerator and denominator fields on the Fraction type?
is effectively doing this:
although more accurately, it's doing
The Fraction type has a __mul__() method, which takes two Fractions and returns a Fraction as a result. It will always return the simplest (lowest common denominator) version of the faction. Any class with such a method is telling Python how to multiply it. So you can type
or
and get Fraction('1/6') as a result. mul() is just the equivalent of lambda x, y: x * y
This took me forever, because I didn't know I was working with fraction objects, instead of a 2D list.
Same here! After scrolling down a bit in the description, I found the fractions.
UPDATED SOLUTION IS HERE
https://www.thecscience.com/2021/08/hackerrank-reduce-function-in-python-problem-solution.html
here is problem solution in python programming. https://programs.programmingoneonone.com/2021/02/hackerrank-reduce-function-solution-python.html
Without using reduce :)
too complicated:
Exactly what I've done.
how is this working? , i mean how can it possibly get the ratio?
what is the meaning of t.numerator, t.denominator?? we do not have any numerator and denominator defined. can u explait what is the meaning of this line
As stated in the
__main__
, user<wilmer_henao>
assumes that you know the elements offrac
are instances of theFraction
class. Feel free to read the Python API on it. Alternatively, here is my more explicit HackerRank replacement.numerator and denominator are properties of fraction object. The result t is a fraction.
i too done the same
Here what should be the value of fracs ? If its a list so I am giving value as [1,2,3] BUt no where I am giving the denominator value ?
What I m getting is always 1 as denominator
Can you clarify . How can I get denominator value as well ?
the output with this code is (6, 1).
One can avoid
import operator
as one knows whichmul
to use from the class type.def product(fracs):
this one is similar but doesn't require importing