自动化无聊的东西 - 硬币翻转条纹 [英] Automate the boring stuff - Coin flip streaks

查看:15
本文介绍了自动化无聊的东西 - 硬币翻转条纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道现在有很多关于它的问题,即使是同样的问题,但我想我尝试了一些不同的方法.

I know there's tons of questions about it by now, even for the same problem, but I think I tried a bit of a different approach.

任务是对 10.000 个样本进行 100 次翻转,然后计算所有样本出现 6 次正面或反面连续的概率 - 据我所知.但在之前的问题中,编码问题被描述为有点模糊.因此,如果你们能指出代码中的错误,那就太好了:)

The task is to to 10.000 samples of 100 flips each and then compute the probability of a 6x heads or tails streak over all the samples - as far as I understand it. But in previous questions the coding problem was described as a bit fuzzy. Therefore, if you guys could just point out the errors in the code, that would be nice :)

我尽量保持懒惰,这导致我的 macbook 非常努力地工作.这是我的代码.我对当前值与之前值比较的第一次迭代是否有问题(据我所知,我会将索引 -1(然后是索引 100?)与当前值进行比较?)

I tried to be as lazy as possible which results in my macbook working really hard. This is my code. Do I have a problem with the first iteration of the comparison of current value to value before (as far as I understand it, I would compare index -1 (which then is index 100?) to the current one?)

import random

#variable declaration

numberOfStreaks = 0
CoinFlip = []
streak = 0

for experimentNumber in range(10000):
    # Code that creates a list of 100 'heads' or 'tails' values.
    for i in range(100):
        CoinFlip.append(random.randint(0,1))
    #does not matter if it is 0 or 1, H or T, peas or lentils. I am going to check if there is multiple 0 or 1 in a row        

    # Code that checks if there is a streak of 6 heads or tails in a row.
    for i in range(len(CoinFlip)):
        if CoinFlip[i] == CoinFlip[i-1]:  #checks if current list item is the same as before
            streak += 1 
        else:
            streak = 0

        if streak == 6:
            numberOfStreaks += 1

print('Chance of streak: %s%%' % (numberOfStreaks / 100))

我在哪里弄得一团糟?我真的看不出来!

Where did I make the mess? I can't really see it!

推荐答案

您需要重置 CoinFlip 列表.您当前的程序只是不断附加到 CoinFlip,这使得列表很长.这就是你的表现不佳的原因.我还添加了对 i==0 的检查,这样您就不会与列表的末尾进行比较,因为从技术上讲,这不是连续记录的一部分.

You need to reset the CoinFlip list. Your current program just keeps appending to CoinFlip, which makes for a very long list. This is why your performance isn't good. I also added a check for i==0 so that you're not comparing to the end of the list, because that's not technically part of the streak.

for experimentNumber in range(10000):
    # Code that creates a list of 100 'heads' or 'tails' values.
    for i in range(100):
        CoinFlip.append(random.randint(0,1))
    #does not matter if it is 0 or 1, H or T, peas or lentils. I am going to check if there is multiple 0 or 1 in a row

    # Code that checks if there is a streak of 6 heads or tails in a row.
    for i in range(len(CoinFlip)):
        if i==0:
            pass
        elif CoinFlip[i] == CoinFlip[i-1]:  #checks if current list item is the same as before
            streak += 1
        else:
            streak = 0

        if streak == 6:
            numberOfStreaks += 1

    CoinFlip = []

print('Chance of streak: %s%%' % (numberOfStreaks / (100*10000)))

我也认为你需要除以 100*10000 才能得到真实的概率.我不知道为什么他们的 "hint" 建议只除以 100.

I also think you need to divide by 100*10000 to get the real probability. I'm not sure why their "hint" suggest dividing by only 100.

这篇关于自动化无聊的东西 - 硬币翻转条纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆