In this Hackerrank Reduce function problem, you have Given a list of rational numbers, find their product.

HackerRank Reduce Function solution in python


Problem solution in Python 2 programming.

from __future__ import print_function
from fractions import Fraction
import operator

def product(fracs):
    t =  reduce(operator.mul , fracs)
    return t.numerator, t.denominator

if __name__ == '__main__':
    fracs = []
    for _ in range(input()):
        fracs.append(Fraction(*map(int, raw_input().split())))
    result = product(fracs)
    print(*result)


Problem solution in Python 3 programming.

from fractions import Fraction
from functools import reduce
import operator
def product(fracs):
    t =  reduce(operator.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)


Problem solution in pypy programming.

from __future__ import print_function
from fractions import Fraction
import operator
def product(fracs):
    t = reduce(operator.mul , fracs)
    return t.numerator, t.denominator

if __name__ == '__main__':
    fracs = []
    for _ in range(input()):
        fracs.append(Fraction(*map(int, raw_input().split())))
    result = product(fracs)
    print(*result)


Problem solution in pypy3 programming.

from fractions import Fraction
from functools import reduce
import operator
def product(fracs):
    t = reduce(operator.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)