如何编码:
参与人1总是掷骰子A,参与人B总是掷骰子B,比较结果5782次,也分别是A对C,B对C。
似乎我无法打印出骰子A更大,骰子B更大的次数,然后,骰子A和B具有相等的值。
问题是,显然,die_A_larger,die_B_larger和die_A_and_B_equal是未定义的。我想知道为什么以及如何解决这个问题。
到目前为止的代码:
import numpy as np
class die:
def __init__(self, side_values=[1,2,3,4,5,6]):
self.side_values = side_values
def roll(self):
i=np.random.randint(1,7)
return self.sides[i]
#variables
A = die([3,3,3,3,3,6])
B = die([2,2,2,5,5,5])
C = die([1,4,4,4,4,4])
nmax = 5782
def count_number_of_wins(A, B):
die_A_larger = 0
die_B_larger = 0
die_A_and_B_equal = 0
for n in range(nmax):
if A > B:
die_A_larger += 1
elif A < B:
die_B_larger +=1
else:
die_A_and_B_equal +=1
return (die_A_larger, die_B_larger, die_A_and_B_equal)
print (count_number_of_wins(A, B))
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
字符串
1条答案
按热度按时间mmvthczy1#
您需要通过
itertools
对您在问题中描述的组合进行矢量化和重定向:个字符