質問のある選手が試合をビデオ撮影される確率の問題をPythonで解いてみました。(^_^;
数学的には、余事象を使って、ある選手が出る試合とビデオ撮影される試合が一度も重ならない確率を求めて、これを1から引けばよいです。
場合の数・確率の問題は、検算が難しいため、答えに確信が持てないことが多いですね。(^_^;
● SportsVideo.py
# coding: UTF-8 # SportsVideo.py def main(): import itertools from fractions import Fraction from time import time tm = time() # Timer Start total = count = 0 for v in itertools.combinations(range(12),4): for p in itertools.combinations(range(12),3): total+=1 if set(v).intersection(set(p)): #積集合 count+=1 print("%d / %d = %s"%(count,total,Fraction(count,total))) print("Runtime : %.3f [sec]"%(time()-tm)) # Timer Stop & Disp if __name__ == '__main__': main()
●実行結果
81180 / 108900 = 41/55 Runtime : 0.218 [sec]
※参考URL
●Python組み込みクラス(set 集合型)を使った論理式 - 今日のPython
http://blog1.erp2py.com/2012/02/pythonset.html