• + 5 comments
    import operator
    def product(fracs):
        t =  reduce(operator.mul , fracs) # complete this line with a reduce statement
        return t.numerator, t.denominator
    

    easy 1 line

    • + 3 comments

      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?

      • + 2 comments
        reduce(mul, [1, 2, 3, 4])
        

        is effectively doing this:

        mul(mul(mul(1, 2), 3), 4)
        

        although more accurately, it's doing

            accumulator = fracs[0]
            for frac in fracs[1:]:
                accumulator = mul(accumulator, frac)
            return accumulator
        

        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

        • + 1 comment

          This took me forever, because I didn't know I was working with fraction objects, instead of a 2D list.

          • + 0 comments

            Same here! After scrolling down a bit in the description, I found the fractions.

        • + 0 comments

          UPDATED SOLUTION IS HERE

          https://www.thecscience.com/2021/08/hackerrank-reduce-function-in-python-problem-solution.html

      • + 0 comments

        here is problem solution in python programming. https://programs.programmingoneonone.com/2021/02/hackerrank-reduce-function-solution-python.html

      • + 0 comments

        Without using reduce :)

        def product(fracs):     
            num=1
            deno=1
            for x in list(fracs):
                num*=x.numerator
                deno*=x.denominator
            return str(Fraction(num,deno)).split('/')
        
    • + 4 comments

      too complicated:

      def product(fracs):
          t = reduce(lambda x, y : x * y, fracs)# complete this line with a reduce statement
          return t.numerator, t.denominator
      
      • + 0 comments

        Exactly what I've done.

      • + 0 comments

        how is this working? , i mean how can it possibly get the ratio?

      • + 2 comments

        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

        • + 0 comments

          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.

        • + 0 comments

          numerator and denominator are properties of fraction object. The result t is a fraction.

      • + 0 comments

        i too done the same

    • + 0 comments

      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).

    • + 0 comments

      One can avoid import operator as one knows which mul to use from the class type.

          t = reduce(Fraction.__mul__, fracs)
      
    • + 0 comments

      def product(fracs):

      t = reduce(lambda x,y:x*y,fracs)
      return t.numerator, t.denominator
      

      this one is similar but doesn't require importing