-
Notifications
You must be signed in to change notification settings - Fork 0
/
World Cup Bracket Ranker.py
201 lines (123 loc) · 5.23 KB
/
World Cup Bracket Ranker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from xlrd import open_workbook
from xlwt import Workbook
#Nathan Owen, [email protected]
#Needs for use: Two excel sheets, both in very particular formates. One MUST be named World Cup Rankings and the other
#MUST be named World Cup Scores. See Kelvin Abrokwa for details about excel sheet format.
#-----------------------------------------------------------------------------------------------------------------------
#Algorthim Outline
# Where each bracket is an order on teams, 1-32, 1 being the best and 32 being the worst,
# for each football match, the difference between the favorable* team's score and the unfavored team's score will be
# multiplied against the distance in between the two teams found on the bracket. For instance, one might rank Team A
# as number 7 and team B as number 23. Result of 1-3 (A-B) would yield a multiplier of -2, and because the distance
# between the teams is 16, this game yields a subtraction of 32 points from this brackets overall accuracy score.
# If a match is tied, then the multiplier will automatically be -0.5 times the distance between the teams.
# *A team is favorable in a match if they are higher in a bracket then the competing team. Converse for unfavorable.
#-----------------------------------------------------------------------------------------------------------------------
#Misc functions
def findMultiplier(score, firstMinusSecond):
first = float(score[0])
second = float(score[2])
if firstMinusSecond:
return first - second
else:
return second - first
#-----------------------------------------------------------------------------------------------------------------------
#Collect brackets and matches data
brackets = open_workbook('World Cup Rankings.xlsx')
matches = open_workbook('World Cup Scores.xlsx')
#Brackets will be of type python list, element 0 is bracket author name, element 1 is bracket name,
#and 2-33 are the teams in order from best (2) to worst (33)
listOfBrackets = []
for sheet in brackets.sheets():
for row in range(sheet.nrows):
bracketList = []
for column in range(sheet.ncols):
bracketList += [sheet.cell(row,column).value]
if 'Your Name' not in bracketList:
#print bracketList[0]
listOfBrackets += [bracketList]
#print '\n\n\n'
#print listOfBrackets
#print '\n\n\n'
# Matches will be of type python list where the first two elements are the teams, the third the winner, and the fourth
# the score.
listOfMatches = []
for sheet in matches.sheets():
for row in range(sheet.nrows):
matchList = []
for column in range(sheet.ncols):
matchList += [sheet.cell(row,column).value]
if 'tbd' not in matchList:
#print matchList
listOfMatches += [matchList]
#print listOfMatches
#-----------------------------------------------------------------------------------------------------------------------
#Run Algorithm
textFile = open('Bracket Results.txt', 'w')
scoreList = []
bracketDict = {}
for bracket in listOfBrackets:
numUnrecordedMatches = 0
bracketScore = 0
for match in listOfMatches:
#For each match, add or subtract from the person's score.
teamOne = match[0]
teamTwo = match[1]
score = match[3]
#print '\n'
#print teamOne
#print teamTwo
#print score
try:
#Determine whether the first or second team is 'prefered', and then subtract to get multiplier accordingly.
teamOneRank = bracket.index(teamOne)
teamTwoRank = bracket.index(teamTwo)
if teamOneRank < teamTwoRank:
firstMinusSecond = True
else:
firstMinusSecond = False
distance = float(abs(teamOneRank - teamTwoRank))
if distance == 0:
multiplier = -0.5
else:
multiplier = findMultiplier(score, firstMinusSecond)
#print teamOneRank
#print teamTwoRank
#print distance
#print multiplier
#print distance * multiplier
bracketScore += (float(distance) * float(multiplier))
except:
#print "Bracket " + bracket[0] + " is incomplete because it does not contain or correctly record either " + \
#teamOne + " or " + teamTwo
#textFile.write("Bracket " + bracket[0] + " is incomplete because it does not contain or correctly record either " + \
#teamOne + " or " + teamTwo + '\n')
numUnrecordedMatches += 1
bracket[2] = numUnrecordedMatches
if bracketScore not in scoreList:
bracketDict[bracketScore] = bracket
scoreList += [bracketScore]
else:
bracketScore += .1
if bracketScore not in scoreList:
bracketDict[bracketScore] = bracket
scoreList += [bracketScore]
else:
bracketScore += .1
bracketDict[bracketScore] = bracket
scoreList += [bracketScore]
scoreList.sort(reverse = True)
#print scoreList
#-----------------------------------------------------------------------------------------------------------------------
#Show results
rank = 1
for score in scoreList:
bracket = bracketDict[score]
print 'Rank: {:<2} Name: {:<25s} Bracket Name: {:<40s} Score: {:<15} # of Matches not recorded due to misentry: {:<2}'.\
format(rank, bracket[0], bracket[1], score, bracket[2])
textFile.write('Rank: {:<2} Name: {:<25s} Bracket Name: {:<40s} Score: {:<15} # of Matches not recorded due to misentry: {:<2}'.\
format(rank, bracket[0], bracket[1], score, bracket[2]))
textFile.write('\n')
rank += 1
#end = input("Press any key to end this program.")
textFile.close()