• + 0 comments

    Use binomial coefficient

    from math import gcd
    
    def prob(red_1, black_1, red_2, black_2):
        
        # Total outcomes
        total_1 = red_1 + black_1
        total_2 = red_2 + black_2
      
        total_outcomes = total_1 * (total_2 * (total_2 - 1) // 2)
        
        # Favorable outcomes
        # Black from Bag 1, 1 red + 1 black from Bag 2
        BRB = black_1 * (red_2 * black_2)   
        
        # Red from Bag 1, 2 blacks from Bag 2
        RBB = red_1 * (black_2 * (black_2 - 1) // 2)
        
        favorables = BRB + RBB
        
        divisor = gcd(favorables, total_outcomes)
        num = favorables // divisor
        den = total_outcomes // divisor
        
        return f"{num}/{den}"