-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore_iaaf.rb
executable file
·321 lines (251 loc) · 7.09 KB
/
score_iaaf.rb
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/ruby -w
require "iaaf"
def to_seconds(time)
array = time.split("/[:|.]/")
array = time.split(":")
# Reverse the order so seconds are first, then minutes, then hours
array = array.reverse
total = 0
# Convert the time to seconds
(0..2).each {|i|
total = total + (array[i].to_f)*(60**i)
}
return total
end
def print_array(array)
formatted = "x" + array[0].to_s + " = ["
first = true
array.each {|entry|
if not first
formatted = formatted + ", "
else
first = false
end
formatted = formatted + "\"" + entry.to_s + "\""
}
formatted = formatted + "]"
return formatted
end
# All possible event names and their type
event_names = {
'hj' => 'field',
'pv' => 'field',
'lj' => 'field',
'tj' => 'field',
'sp' => 'field',
'dt' => 'field',
'ht' => 'field',
'jt' => 'field',
'decathlon' => 'field',
'heptathlon' => 'field',
'2000mSC' => 'long',
'3000m' => 'long',
'3000mSC' => 'long',
'2miles' => 'long',
'5000m' => 'long',
'10000m' => 'long',
'600m' => 'middle',
'800m' => 'middle',
'1000m' => 'middle',
'1500m' => 'middle',
'mile' => 'middle',
'2000m' => 'middle',
'100m' => 'sprints',
'110mH' => 'sprints',
'200m' => 'sprints',
'300m' => 'sprints',
'400m' => 'sprints',
'400mH' => 'sprints',
'4x100m' => 'sprints',
'4x200m' => 'sprints',
'4x400m' => 'sprints'
}
# All possible event names and their type
event_columns = {
'hj' => 1,
'pv' => 2,
'lj' => 3,
'tj' => 4,
'sp' => 5,
'dt' => 6,
'ht' => 7,
'jt' => 8,
'decathlon' => 9,
'heptathlon' => 9,
'2000mSC' => 1,
'3000m' => 2,
'3000mSC' => 3,
'2miles' => 4,
'5000m' => 5,
'10000m' => 6,
'600m' => 1,
'800m' => 2,
'1000m' => 3,
'1500m' => 4,
'mile' => 5,
'2000m' => 6,
'100m' => 1,
'110mH' => 2,
'200m' => 3,
'300m' => 4,
'400m' => 5,
'400mH' => 6,
'4x100m' => 7,
'4x200m' => 8,
'4x400m' => 9
}
# Help
if ARGV[0] == "--h" or ARGV[0] == "--help"
print "Usage: score_iaaf.rb <gender> <event> <performance in time or distance>\n"
print "Example: score_iaaf.rb male 400m 43.21\n"
print "For valid event names, use --events\n"
exit(0)
end
if ARGV[0] == "--events"
for event_name in event_names
puts event_name[0]
end
exit(0)
end
# Gender can be 'male' or 'female'
if not ARGV[0] == "male" and not ARGV[0] == "female"
print "First agrument must be 'male' or 'female'. Bad arguments. Try --h for proper format\n"
exit(0)
end
gender = ARGV[0]
event_name = ARGV[1]
# Make sure the event name is one we recognize
if not event_names[ARGV[1]]
print "Unrecognized event name. Bad arguments. Try --h for proper format\n"
exit(0)
end
# Find the event type associated with the given event name
event_type = event_names[ARGV[1]]
# Make sure the distance/time is given in a format we recognize
performance = ARGV[2]
if event_type == "field"
if performance.to_f == 0.0
print "Distance in an improper format. Try --h for proper format\n"
exit(0)
else
performance = performance.to_f
end
end
# Make sure the distance/time is given in a format we recognize
if event_type != "field"
#match = (performance =~ /\A<0(x|X)(\d|[a-f]|[A-F])+>/) != nil
#match = (performance =~ /\A(\d):(\d)(\d).(\d)(\d)/\A)
#match = (performance =~ /\A(\d):(\d)(\d):(\d)(\d).(\d)(\d)/\A)
#match = (performance =~ /\A(\d){0,2}.(\d)(\d)\z/)
# Should be in format xx.xx or xx:xx.xx or xx:xx:xx.xx
match = (performance =~ /\A((((\d):)?(\d))?(\d):)?(\d){0,2}.(\d)(\d)\z/)
if not match
print "Time in an improper format. Try --h for proper format\n"
exit(0)
end
# Convert the string representation of the time to seconds
#puts performance
performance = to_seconds(performance)
#puts performance
end
if gender == "male" and event_type == "sprints"
big_array = Iaaf::MENS_SPRINTS # mens_sprints
elsif gender == "male" and event_type == "field"
big_array = Iaaf::MENS_FIELD # mens_field
elsif gender == "male" and event_type == "middle"
big_array = Iaaf::MENS_MIDDLE # mens_middle
elsif gender == "male" and event_type == "long"
big_array = Iaaf::MENS_LONG # mens_long
elsif gender == "female" and event_type == "sprints"
big_array = Iaaf::WOMENS_SPRINTS # womens_sprints
elsif gender == "female" and event_type == "field"
big_array = Iaaf::WOMENS_FIELD # womens_field
elsif gender == "female" and event_type == "middle"
big_array = Iaaf::WOMENS_MIDDLE # womens_middle
elsif gender == "female" and event_type == "long"
big_array = Iaaf::WOMENS_LONG # womens_long
end
# We want the gender, event name, and time/distance
# Gender can be 'male' or 'female'
# Events for males can be
# hj pv lj tj sp dt ht jt decathlon
# 2000mSC 3000m 3000mSC 2miles 5000m 10000m
# 600m 800m 1000m 1500m mile 2000m
# 100m 110mH 200m 300m 400m 400mH 4x100m 4x200m 4x400m
#
#
# hj pv lj tj sp dt ht jt heptathlon
# 2000mSC 3000m 3000mSC 2miles 5000m 10000m
# 600m 800m 1000m 1500m mile 2000m
# 100m 100mH 200m 300m 400m 400mH 4x100m 4x200m 4x400m
# Find the smallest number larger than the target
# Find the largest number smaller than the target
# event types 'sprints', 'middle', and 'long' are track events
track_event = event_type != "field"
target = performance.to_f
column = event_columns[event_name]
x1 = 0
x2 = 0
y1 = 0
y2 = 0
first = true
big_array.each {|array|
# The first time we iterate through this loop, we get the column labels, so skip it
if first
first = false
next
end
# Skip this entry if it doesn't have useful data
if array[column] == "-"
next
end
# Convert to seconds if it is a timed event, leave alone otherwise
if track_event
tmp = to_seconds(array[column])
else
tmp = array[column].to_f
end
# Try to find the smallest number that is still larger than the target
if tmp > target
x1 = tmp
y1 = array[0].to_f
if track_event
break
end
end
# Try to find the largest number that is still smaller than the target
if tmp < target
x2 = tmp
y2 = array[0].to_f
if not track_event
break
end
end
}
#p "smalger = #{x1}, #{y1}"
#p "largler = #{x2}, #{y2}"
x = target
#y = mx + b
#y = ((y1-y2)/(x1-x2))x + b
b = y1 - ((y1-y2)/(x1-x2))*x1
#p "y = ((#{y1}-#{y2})/(#{x1}-#{x2}))*x + #{b}"
#y = ((1400-1349)/(9.9-10.17))*x + 3270.0
y = ((y1-y2)/(x1-x2))*x + b
# Print the final result
#$stdout << y.round
print y.round.to_s + "\n"
#p big_array
# X - Find the two numbers that are closest to the target number on either side using a
# binary search algorithm twice
# Find the two numbers that are closest to the target number on either side using a linear
# search algorithm
# Use these points to construct a linear formula
# Solve the formula using our value for x and round to the nearest whole number
# Dead middle overestimates by 6 on the low end (102 when it should have been 96)
# (1295 when it should have been 1295)
# (795 when it should have been a 795)
# (495.5 when it should have been a 495)
# For field events, stop when we find a lower number
# For track events, stop when we find a higher number
# Convert minutes:seconds to seconds
# Create a big array with all events combined (seperately for men and women)