質問のコインの確率の問題をPythonで解いてみた。

 質問のコインの確率の問題Pythonで解いてみました。(^_^;

 A、Bが以下のルールで勝負したとき、Aが勝利する確率を計算してください。
[ルール1]コインを投げて、表が出たらAのポイント、裏が出たらBのポイントとする。
[ルール2]先に6ポイント獲得した方が、その時に相手に2ポイント以上の差をつけていた場合に勝利とする。どちらかが6ポイント獲得した際のポイント差が0または1の場合は、2ポイントの差がつくまでコインを投げ続けることとする。
[ルール3]1回目はコインの表が出たと仮定して計算することとする。
 Aがコインを投げる回数12回以下で勝利する確率を求めてください。

 確率の問題は、検算が難しいため、答えに確信が持てないことが多いですね。(^_^;

● ProbOfCoin1.py

# coding: UTF-8
# ProbOfCoin1.py

from fractions import Fraction
from time import time
import itertools

def toStr(li):
    return '%s'%''.join(li)

def main():
    tm = time()  # Timer Start
    C = 'AB'    # コインの表(A)裏(B)
    total = count = 0
    for c in itertools.product(C,repeat=11):
        total+=1
        s = 'A'+toStr(c)    # 結果を文字列にして並べる
        for i in range(6,12+1):
            t = s[:i]
            a,b = t.count('A'), t.count('B')
            if b>=6 and b>=a+2: break
            if a>=6 and a>=b+2:
##                print(t)
                count+=1
                break

    print("%d/%d = %s"%(count,total,Fraction(count,total)))
    print("Runtime : %.3f [sec]"%(time()-tm))   # Timer Stop & Disp

if __name__ == '__main__':
    main()

●実行結果

1150/2048 = 575/1024
Runtime : 0.032 [sec]

※参考URL
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q13191407788
https://okwave.jp/qa/q9505686.html