计算200次硬币抛掷中最长的正面和反面序列。我这样做了——在python中有更好的方法吗?(不要太混淆)importrandomdeftoss(n):count=[0,0]longest=[0,0]foriinxrange(n):coinface=random.randrange(2)count[coinface]+=1count[notcoinface]=0ifcount[coinface]>longest[coinface]:longest[coinface]=count[coinface]#printcoinface,count,longestprint"longestseque
在无偏掷硬币中,H或T出现50%的次数。但我想模拟硬币,它给出概率为“p”的H和概率为“(1-p)”的T。类似这样的:defflip(p):'''thisfunctionreturnHwithprobabilityp'''#dosomethingreturnresult>>[flip(0.8)foriinxrange(10)][H,H,T,H,H,H,T,H,H,H] 最佳答案 random.random()返回[0,1)范围内的均匀分布伪随机float。该数字小于[0,1)范围内的给定数字p,概率为p。因此:defflip(p):
我正在尝试实现一个硬币问题,问题规范是这样的创建一个函数来计算可用于给定数量的所有可能的硬币组合。Allpossiblecombinationsforgivenamount=15,cointypes=1671)1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2)1,1,1,1,1,1,1,1,1,6,3)1,1,1,1,1,1,1,1,7,4)1,1,1,6,6,5)1,1,6,7,6)1,7,7,函数原型(prototype):intfindCombinationsCount(intamount,intcoins[])假设硬币数组已排序。对于上面的例子,这个函数应该返回6