Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Reduce Function Hacker Rank Solution – Queslers

Problem : Reduce Function Hacker Rank Solution

Given a list of rational numbers,find their product.

Concept :The reduce() function applies a function of two arguments cumulatively on a list of objects in succession from left to right to reduce it to one value. Say you have a list, say [1,2,3] and you have to find its sum.

>>> reduce(lambda x, y : x + y,[1,2,3])
6

You can also define an initial value. If it is specified, the function will assume initial value as the value given, and then reduce. It is equivalent to adding the initial value at the beginning of the list. For example:

>>> reduce(lambda x, y : x + y, [1,2,3], -3)
3

>>> from fractions import gcd
>>> reduce(gcd, [2,4,8], 3)
1

Input Format :

First line contains n, the number of rational numbers.
The ith of next n lines contain two integers each, the numerator (Ni) and denominator(Di) of the ith rational number in the list.

Constraints :

  • 1 <= n <= 100
  • 1 <= Ni, Di <= 10^9

Output Format :

Print only one line containing the numerator and denominator of the product of the numbers in the list in its simplest form, i.e. numerator and denominator have no common divisor other than 1.


Sample Input :

3
1 2
3 4
10 6

Sample Output :

5 8

Explanation :

Required product is 1/2 3/4 10/6 = 5/8

Reduce Function Hacker Rank Solution in python 2

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)

Reduce Function Hacker Rank Solution in python 3

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)

Reduce Function Hacker Rank Solution in pypy

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)

Reduce Function Hacker Rank Solution in pypy 3

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)
Reduce Function Hacker Rank Solution Review:

In our experience, we suggest you solve this Reduce Function Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Reduce Function is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

Reduce Function Hacker Rank Solution Conclusion:

I hope this Reduce Function Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Mini-Max Sum Hacker Rank Solution

String Validators Hacker Rank Solution

Text Alignment Hacker Rank solution

String validators Hacker Rank Solution

Staircase Hacker Rank Solution

Leave a Reply

Your email address will not be published. Required fields are marked *