Sort by

recency

|

17 Discussions

|

  • + 0 comments

    from fractions import Fraction

    Bg1 = {"Red": 4, "Black": 5} Bg2 = {"Red": 3, "Black": 7}

    Case_1 = Fraction(Bg1["Red"], Bg1["Red"] + Bg1["Black"]) * Fraction(Bg2["Black"], Bg2["Red"] + Bg2["Black"]) * Fraction(Bg2["Black"]-1, Bg2["Red"] + Bg2["Black"]-1)

    Case_2 = Fraction(Bg1["Black"], Bg1["Red"] + Bg1["Black"]) * Fraction(Bg2["Black"], Bg2["Red"] + Bg2["Black"]) * Fraction(Bg2["Red"], Bg2["Red"] + Bg2["Black"]-1)

    Case_3 = Fraction(Bg1["Black"], Bg1["Red"] + Bg1["Black"]) * Fraction(Bg2["Black"], Bg2["Red"] + Bg2["Black"]-1) * Fraction(Bg2["Red"], Bg2["Red"] + Bg2["Black"])

    print(Case_1 + Case_2 + Case_3)

  • + 0 comments

    I tried to do it both pythonically and with clear decipherability from the problem description. Pythonically meant, in this context, making good use of the unpacking operators.

    from math import gcd
    from itertools import product, combinations
    
    b1 = (*'r'*4, *'b'*5)
    b2 = (*'r'*3, *'b'*7)
    total = [(*a, *b) for a, b in product(combinations(b1, 1), combinations(b2, 2))]
    n = len([_ for _ in total if _.count('b') == 2])
    d = len(total)
    factor = gcd(n, d)
    print(n//factor, '/', d//factor, sep='')
    
  • + 0 comments

    from math import comb

    total_ways = comb(9, 1) * comb(10, 2)

    ways_case_A = comb(4, 1) * comb(7, 2)

    ways_case_B = comb(5, 1) * comb(7, 1) * comb(3, 1)

    successful_ways = ways_case_A + ways_case_B

    from fractions import Fraction probability = Fraction(successful_ways, total_ways)

    print(probability)

  • + 0 comments

    check out the link for full working code in python with explanation of where it's used in AI and ML, so probability is important while learning AI and ML, https://youtu.be/M3WcDNj4YJ8?si=2xMBBjqkuBsSoo0c

  • + 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}"