Sort by

recency

|

40 Discussions

|

  • + 0 comments

    first i thought its like a return rand(0,99); thing but it's not. seriously i spent like 1,5 hour to manage this :D

  • + 0 comments

    I tried:

    def solve(a, b, c):
        #what is the maximum possible value of x that guarantees that x+y<C?
        x_val_guarantee_success = c-b
        #and what is the probability of getting that x value?
        p_guaranteed_success = max([min([x_val_guarantee_success,a])/a,0])
        #so it might be that we never get that value, but we could do.
        middle_group_lbound = max([x_val_guarantee_success,0])
        #next; what is the minimum possible value of x that guarantees x+y>=C, failure?
        #that'll be when x>=C even without considering Y
        middle_group_ubound = min([c,a])
        p_guaranteed_failure = max([0,(a-c)/a])
        #now, the probability of sitting somewhere in the middle
        p_contingent_on_y = 1-p_guaranteed_success-p_guaranteed_failure
        #now, the probability of the limits on each side
        p_lower = (c-middle_group_lbound)/b
        #ok and now at the upper limit, i.e., x<C
        #proportion of possible values of y for which x+y<C
        #y_value_needed_at_p_higher<-C-min(x_val_guarantee_failure,A)
        p_higher = (c-middle_group_ubound)/b
    
        p_total  = p_guaranteed_success+sum([p_lower,p_higher])/2*p_contingent_on_y+p_guaranteed_failure*0
    
    
        if(p_total==1):
            retval = "1/1"
        else:
            retval = str(Fraction(p_total).limit_denominator(max_denominator=10000000000))
            
        return(retval)
    

    Seems that getting the fractions right is really tricky--a couple of my answers were off a tiny bit, and I'm not sure whether it's because the math I used leaves me with a wrong answer, or if the fractional answers are wrong.

  • + 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.

  • + 0 comments

    I have a valid solution that cannot pass any of the tests. I have checked the output with other solutions that pass the hidden test cases. What am I doing wrong?

    def solve(a, b, c):
        # Write your code here
        
        M = max(a, b)
        m = min(a, b)
        
        A = max(0, min(M, c - m))
        B = max(0, min(c, M))
        H = max(0, min(c, m))
        
        nom = (A + B) * H
        denom = 2 * M * m
        
        gcd = math.gcd(nom, denom)
        return '%s/%s' % (nom // gcd, denom // gcd)
    
  • + 0 comments

    Not simple

    def solve(a, b, c):
    
        if a + b <= c:
            return("1/1")
        else:
            big_triangle = c ** 2
            
            if c > a:
                big_triangle -= (c-a) * (c-a)
            if c > b:
                big_triangle -= (c-b) * (c-b)
                
            result = F.Fraction(big_triangle, 2 * a * b)
    
            return str(result)