• + 1 comment

    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:

    def solve(a, b, c):
        # # Write your code here
        if c >= a + b:
            return "1/1"
        else:
            big_triangle = c ** 2
            if c > a:
                big_triangle -= (c-a)**2 
            if c > b:
                big_triangle -= (c-b)**2 
            frac = Fraction(big_triangle, 2 * a * b)
            den, num = frac.denominator, frac.numerator
            ans = f"{num}/{den}"
            return ans
        
    

    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.