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 the next n lines contains two integers each, the numerator(Ni) and denominator(Di) of the ith rational number in the list.
- 1 <= n <= 100
- 1 <= Ni, Di <=109
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.
3
1 2
3 4
10 6
Sample Output:5 8
Explanation:- Required product is 1/2 3/4 10/6 = 5/8
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)
Disclaimer: The problem statement is given by hackerrank.com but the solution is generated by the Geek4Tutorial admin. We highly recommend you solve this on your own, however, you can refer to this in case of help. If there is any concern regarding this post or website, please contact us using the contact form. Thank you!
No comments:
Post a Comment