Sort by

recency

|

16 Discussions

|

  • + 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}"
    
  • + 0 comments
    from fractions import Fraction 
    from itertools import combinations
    
    bag_1=['r','r','r','r','b','b','b','b','b']
    bag_2=['r','r','r','b','b','b','b','b','b','b']
    all_pairs=[]
    num=0
    for item1_bag_1 in (bag_1):
      for item2_bag_2,item3_bag2 in (combinations(bag_2,2)):
        all_pairs.append((item1_bag_1,item2_bag_2,item3_bag2))
    for bbr in all_pairs:
      if bbr.count('r')==1 and bbr.count('b')==2:
        num+=1
    print(Fraction(num,len(all_pairs)))