2017-수학-가형-홀수-4

문제

2017-수학-가형-홀수-4

풀이

from random import randint
from sympy import Rational
def random_subset(s):
    out = set()
    for el in s:                                                                                                                    
        # random coin flip
        if randint(0, 1) == 0:
            out.add(el)
    return out


def Prob(S):
    return Rational(len(S), len(U))


def complement(S):
    # difference between a global universal set U and the given set a
    return U - S


def intersection(a,b):
    return set(a) & set(b)
U= set(range(1,7))
Bc = {1,2}
B=complement(Bc)

count = 0
while count < 5:
  Anotfound = True;
  while Anotfound:
      A = random_subset(U)
      Ac = complement(B)
      if Prob(intersection(A, B)) == Prob(A)*Prob(B) and Rational(Prob(intersection(A, B)), Prob(B)) == Rational(1,2):
          Anotfound = False;
          print(["A found", A])
          count+=1
          break
  print(["P(A)*P(B)={ans}".format(ans = Prob(A)*Prob(B))])
['A found', {1, 3, 6}]
['P(A)*P(B)=1/3']
['A found', {2, 4, 6}]
['P(A)*P(B)=1/3']
['A found', {1, 4, 6}]
['P(A)*P(B)=1/3']
['A found', {1, 4, 6}]
['P(A)*P(B)=1/3']
['A found', {2, 4, 6}]
['P(A)*P(B)=1/3']