import random
#
# Choose the number of times to run the simulation
num_sim = 10_000_000
#
# Count the number of times A5 beats E5
a5_win = 0
#
# g_list keeps a tally of relative rank of horses A1-A5, B1-B5 etc.
g_list=[[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0],[0,0,0,0,0]]
#
for k in range(0, num_sim):
horses = list(range(1,26))
random.shuffle(horses)
#
# Split the random selection into the five groups (first races)
#
g_0 = [horses[0],horses[1],horses[2],horses[3],horses[4]]
g_1 = [horses[5],horses[6],horses[7],horses[8],horses[9]]
g_2 = [horses[10],horses[11],horses[12],horses[13],horses[14]]
g_3 = [horses[15],horses[16],horses[17],horses[18],horses[19]]
g_4 = [horses[20],horses[21],horses[22],horses[23],horses[24]]
#
# sorting each group equates to running the first set of races
#
g_0.sort()
g_1.sort()
g_2.sort()
g_3.sort()
g_4.sort()
#
# sorting the list of lists equates to running the WOW race (by default Python sorts by the first nunber in each list)
g_temp = [g_0,g_1, g_2, g_3, g_4]
#
g_temp.sort()
#
# Check whether A5 is faster than E5)
#
if g_temp[0][4] < g_temp[4][4]:
a5_win += 1
#
# Add the rankings for this race to the overall tally:
for i in range(0, 5):
for j in range(0, 5):
g_list[i][j] += g_temp[i][j]
#
# Calculate the average rank for each horse:
for i in range(0, 5):
for j in range(0, 5):
g_list[i][j] = round(g_list[i][j]/num_sim,2)
#
print("A5 is faster than E5 {:.2f}".format(a5_win/num_sim))
print("Average rank for group A", g_list[0])
print("Average rank for group B", g_list[1])
print("Average rank for group C", g_list[2])
print("Average rank for group D", g_list[3])
print("Average rank for group E", g_list[4])