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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…