Sort by

recency

|

213 Discussions

|

  • + 0 comments
    from fractions import Fraction
    from functools import reduce
    
    def product(fracs):
        t = reduce(Fraction.__mul__,fracs)
        return t.numerator, t.denominator
    
    if __name__ == '__main__':
        fracs = []
        for _ in range(int(input())):
            fracs.append(Fraction(*map(int, input().split())))
        result = product(fracs)
        print(*result)
    		
    
  • + 0 comments

    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

  • + 0 comments
    def product(fracs):
        t=reduce(lambda x,y:x*y,fracs)
        return t.numerator,t.denominator
    
    if __name__ == '__main__':
        fracs = []
        for _ in range(int(input())):
            fracs.append(Fraction(*map(int, input().split())))
        result = product(fracs)
        print(*result)
    		
    
  • + 0 comments

    Here is HackerRank Reduce Function in Python solution - https://programmingoneonone.com/hackerrank-reduce-function-solution-in-python.html

  • + 0 comments
    from fractions import Fraction
    from functools import reduce
    
    def product(fracs):
        t = reduce(lambda x, y : x * y, fracs)
        return t.numerator, t.denominator
    
    if __name__ == '__main__':
        fracs = []
        for _ in range(int(input())):
            fracs.append(Fraction(*map(int, input().split())))
        result = product(fracs)
        print(*result)