We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I guess it is easy once you remember how one should approach the problem. It took me a while since my prob skills are rusty.
Solution:
defsolve(a,b,c):# # Write your code hereifc>=a+b:return"1/1"else:big_triangle=c**2ifc>a:big_triangle-=(c-a)**2ifc>b:big_triangle-=(c-b)**2frac=Fraction(big_triangle,2*a*b)den,num=frac.denominator,frac.numeratorans=f"{num}/{den}"returnans
A brief explanation:
1. Think of a coordinate system (x, y). Draw a rectangle of width a and height b. Set the bottom left of the rectangle as the origin.
2. Any combination of a and b that sum up to c can be expressed by a line: y= -x + c. The question becomes how to find the area under this curve that overlaps with the area of the rectangle.
... (That is what User: skypehopert and I are doing.
3. Find the area, divide it by the area of the rectangle. That is the probability we want.
4. After that, express the answer in the form HackerRank wants. This gave me some trouble. I still dont like the format of the output.
Knowing about the fractions module and Fraction function is the least obvious part of this problem. Would be nice to include the import statement in your solution.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Random number generator
You are viewing a single comment's thread. Return to all comments →
I guess it is easy once you remember how one should approach the problem. It took me a while since my prob skills are rusty.
Solution:
A brief explanation: 1. Think of a coordinate system (x, y). Draw a rectangle of width
a
and heightb
. Set the bottom left of the rectangle as the origin. 2. Any combination of a and b that sum up to c can be expressed by a line: y= -x + c. The question becomes how to find the area under this curve that overlaps with the area of the rectangle. ... (That is what User: skypehopert and I are doing. 3. Find the area, divide it by the area of the rectangle. That is the probability we want. 4. After that, express the answer in the form HackerRank wants. This gave me some trouble. I still dont like the format of the output.Knowing about the fractions module and Fraction function is the least obvious part of this problem. Would be nice to include the import statement in your solution.