#!/usr/bin/env python # coding: utf-8 # ### 확률 계산 # Q: 주머니 속에 숫자 1이 적힌 카드가 세 장, 숫자 2가 적힌 카드가 3장, 3이 적힌 카드가 3장이 들어있다. # 이 주머니에서 임의로 세장의 카드를 동시에 뽑을 때, 카드에 적힌 숫자의 합이 3의 배수가 될 확률은? # In[30]: 5/14 #답 # #### 시뮬레이션 # In[11]: card=[1,1,1,2,2,2,3,3,3] # In[12]: import random # In[13]: random.shuffle(card) # In[15]: card # In[17]: card[:3] # In[25]: count=100000 # In[26]: sus=0 # In[27]: for i in range(count): random.shuffle(card) if sum(card[:3]) % 3 ==0: sus=sus+1 print("성공확률은 {}이다".format(sus/count)) # #### 전수조사 # In[86]: carddic={'a1':1,'a2':1,'a3':1,'b1':2,'b2':2,'b3':2,'c1':3,'c2':3,'c3':3} # In[87]: carddic.keys() # In[88]: carddic.values() # In[89]: carddic.items() # In[90]: import itertools # In[91]: itertools.permutations(carddic.keys()) # In[92]: len(list(itertools.permutations(carddic.keys()))) # In[93]: 9*8*7*6*5*4*3*2*1 # In[94]: per_with_eq=itertools.permutations(carddic.values()) # In[95]: a=set((per_with_eq)) # In[98]: len(a) # #### 1680은 같은 것이 있는 순열의 수임. # In[99]: len(list(itertools.permutations(carddic.keys(),3))) # In[100]: 9*8*7 # In[109]: len(list(itertools.combinations(carddic.keys(),3))) # #### 84는 9C3 의 개수임 , 즉 위의 list가 근원사건의 집합이라고 볼 수 있음 # In[110]: standard=list(itertools.combinations(carddic.keys(),3)) # In[112]: standard # In[120]: def desc(se): ''' 값의 합이 3 배수인지 판단하는 함수''' return sum(list(map(lambda a : carddic[a],se)))%3 ==0 # In[121]: desc(('b3', 'c1', 'c2')) # In[122]: su=0 for i in standard: if desc(i): su=su+1 print(su) # 즉 해당경우는 30가지이며, 원하는 확률은 30/84 이다. # In[ ]: