Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

python - Finding largest value when rolling two dice among a list of three dice

I have the following function which takes as input two (2) six-sided dice and determines which die among them has the larger number. Note that the dice do not necessarily have the numbers 1-6.

def count_number_of_wins(die_1, die_2):

    die_1_larger = 0
    die_2_larger = 0

    for i in range(6):
        for j in range(6):
            if die_1[i] > die_2[j]:
                die_1_larger += 1
            elif die_1[i] < die_2[j]:
                die_2_larger +=1
        

    return (die_1_larger, die_2_larger)

Now, I use this function to determine which die is "best" among them an input list of three dice. I take a list of dice and determine whether one die is better than the others in the list. A die is "better" if it wins more frequently. In this case, I return its 0-based index. If no such die exists, then I return -1.

Here is the function for that:

def determine_best_die(dice):
    assert all(len(dice) == 6 for die in dice)

    wins = [0] * len(dice)
    
    for i in range(len(dice)):
        for j in range(i+1, len(dice)):
            a, b = count_number_of_wins(dice[i], dice[j])
            if a > b:
                wins[i] = wins[i] + 1
            else:
                wins[j] = wins[j] + 1
    for i in wins:
        if (i == (len(dice)-1)):
            return wins.index(i)

This function is failing for this case:

[1, 1, 6, 6, 8, 8], [2, 2, 4, 4, 9, 9], [3, 3, 5, 5, 7, 7]

I would expect it to return -1, and it's returning 0.

Can anyone see where the issue is in the determine_best_die() function?

Thanks!

question from:https://stackoverflow.com/questions/65877410/finding-largest-value-when-rolling-two-dice-among-a-list-of-three-dice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
def count_number_of_wins(die_1, die_2):

    die_1_larger = 0
    die_2_larger = 0

    for i in range(6):
        for j in range(6):
            if die_1[i] > die_2[j]:
                die_1_larger += 1
            elif die_1[i] < die_2[j]:
                die_2_larger +=1
        

    return (die_1_larger, die_2_larger)

def determine_best_die(dice):
    assert all(len(die) == 6 for die in dice)

    wins = [0] * len(dice)
    
    for i in range(len(dice)):
        for j in range(i+1, len(dice)):
            a, b = count_number_of_wins(dice[i], dice[j])
            if a > b:
                wins[i] = wins[i] + 1
            else:
                wins[j] = wins[j] + 1
    for i in wins:
        if (i == (len(dice)-1)):
            return wins.index(i)
    return -1

I added the case for -1 which happens when there is no dice that wins all the rest. The assertion statement was also checking the len(dice) instead of len(die)

print (determine_best_die([[3,3, 6, 6, 8, 8], [2, 2, 4, 4, 9, 9], [3, 3, 5, 5, 7, 7]])) 

returns 0 as the first die wins both the second and third die, while the original failed test case

print (determine_best_die([[1, 1, 6, 6, 8, 8], [2, 2, 4, 4, 9, 9], [3, 3, 5, 5, 7, 7]]))

returns -1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...