[Python] harvard cs109 HW0 goat_door

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I need someone to help explain what is happening at the last line of the code in the given solution, especially how the part result[bad] works and why bad.sum() is needed as an argument

    def goat_door(prizedoors, guesses):

    #strategy: generate random answers, and
    #keep updating until they satisfy the rule
    #that they aren't a prizedoor or a guess
    result = np.random.randint(0, 3, prizedoors.size)
    while True:
    bad = (result == prizedoors) | (result == guesses)
    if not bad.any():
    return result
    result[bad] = np.random.randint(0, 3, bad.sum())


    From what I understand, the last line is a way of replacing the bad and result variable with an empty array. I have improvised with my own code below but I still need to understand how the original solution works. Would appreciate any help thank you!

    def goat_door(prizedoors, guesses):

    result = np.random.randint(0,3, prizedoors.size)
    while True:
    global choose_again
    choose_again = np.logical_or((result == prizedoors),(result == guesses))

    if not np.array(choose_again).any():
    return result
    choose_again = []
    result = np.random.randint(0,3, prizedoors.size)

    Continue reading...

Compartilhe esta Página