Sort by

recency

|

13 Discussions

|

  • + 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)))
    
  • + 0 comments

    My Python solution with explanation:

    import math
    
    # setup situation
    bag1 = ['r']*4 + ['b']*5    # 1 pick
    bag2 = ['r']*3 + ['b']*7    # 2 picks
                                # b b r
    
    # initialize counting variables
    numerator = 0
    denominator = 0
    
    # collect event occurrences
    for X in bag1:  # 1 pick from bag1
        
        for Y1 in bag2:  # 1 pick from bag2
            bag2reduced = bag2.copy()  # create a COPY of bag2
            bag2reduced.remove(Y1)  # remove already picked element from COPY of bag2
            
            for Y2 in bag2reduced:  # 1 pick from EDITED bag2
                tup = (X, Y1, Y2)  # event
                
                if all([tup.count('r') == 1,
                        tup.count('b') == 2]):  # conditions of interest
                    numerator += 1
                
                denominator += 1
    
    gcd = math.gcd(numerator, denominator)
    
    numerator //= gcd
    denominator //= gcd
    
    print(f'{numerator}/{denominator}')
    
  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import fractions
    from itertools import combinations
    from itertools import product
    
    
    Bag1 = ['r']*4 + ['b']*5
    Bag2 = ['r']*3 + ['b']*7
    
    Bag2_comb = list(combinations(Bag2,2))
    
    matched = 0
    total = len(Bag1) * len(list(combinations(Bag2,2)))
    
    for x in list(product(Bag1, Bag2_comb)):
        if [x[0], x[1][0], x[1][1]].count('b')==2 and [x[0], x[1][0], x[1][1]].count('r')==1:
            matched += 1
    
    print(fractions.Fraction(matched, total))
    
  • + 0 comments

    Python 3 code

    from fractions import Fraction
    
    bag1 = ['r']*4 + ['b']*5
    bag2 = ['r']*3 + ['b']*7
    
    combo = []
    visited = 0
    
    for i in bag1:
        for e, j in enumerate(bag2):
            for k in bag2[e+1:]:
                temp = [i, j, k]
                if (temp.count('r') == 1) and (temp.count('b') == 2):
                    combo.append(temp)
                
                visited += 1
                
    print(Fraction(len(combo), visited))