wildfalcon-skating_system 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +7 -0
- data/lib/skating_system.rb +5 -0
- data/lib/skating_system/performance_results.rb +102 -0
- data/lib/skating_system/scorer.rb +14 -0
- data/spec/spec.opts +6 -0
- data/spec/spec/performance_results_spec.rb +213 -0
- data/spec/spec/scorer_spec.rb +31 -0
- data/spec/spec_helper.rb +9 -0
- metadata +60 -0
data/README
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
module SkatingSystem
|
2
|
+
class PerformanceResults
|
3
|
+
|
4
|
+
def score(marks)
|
5
|
+
@marks = marks
|
6
|
+
@majority = (@marks[@marks.keys.first].size/2).ceil
|
7
|
+
|
8
|
+
tally_marks
|
9
|
+
|
10
|
+
compute_results
|
11
|
+
|
12
|
+
@marks.each do |entree, marks|
|
13
|
+
@marks[entree].merge!(@tally[entree])
|
14
|
+
@marks[entree].merge!(@results[entree])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def sum_marks(entree, position)
|
19
|
+
sum=0
|
20
|
+
@marks[entree].each do |judge, place|
|
21
|
+
sum += place if place <= position
|
22
|
+
end
|
23
|
+
sum
|
24
|
+
end
|
25
|
+
|
26
|
+
def compute_results
|
27
|
+
@results={ }
|
28
|
+
@marks.each do |entree, marks|
|
29
|
+
@results[entree] ||= { }
|
30
|
+
end
|
31
|
+
|
32
|
+
@next_result_to_compute = 1
|
33
|
+
|
34
|
+
1.upto(@marks.size) do |place|
|
35
|
+
|
36
|
+
|
37
|
+
sorted_candidate = candidates(place).sort do |entree1, entree2|
|
38
|
+
comparator = @tally[entree2][place] <=> @tally[entree1][place]
|
39
|
+
if comparator==0
|
40
|
+
comparator = sum_marks(entree2, place) <=> sum_marks(entree1, place)
|
41
|
+
end
|
42
|
+
comparator
|
43
|
+
end
|
44
|
+
|
45
|
+
sorted_candidate.each do |entree|
|
46
|
+
@results[entree][:result]=@next_result_to_compute
|
47
|
+
@next_result_to_compute += 1
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
#Computes an array of entrees that are candidates for this position
|
53
|
+
def candidates(place)
|
54
|
+
candidates = []
|
55
|
+
@tally.each do |entree, places|
|
56
|
+
unless @results[entree][:result]
|
57
|
+
candidates << entree if places[place] > @majority
|
58
|
+
end
|
59
|
+
end
|
60
|
+
candidates
|
61
|
+
end
|
62
|
+
|
63
|
+
def [](key)
|
64
|
+
@marks[key]
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
#Tallys the number of judges who marked each entree in each place or heigher
|
69
|
+
# and writes the results to @tally
|
70
|
+
# eg given the results
|
71
|
+
# A B C
|
72
|
+
# 1 1 1 3
|
73
|
+
# 2 2 3 2
|
74
|
+
# 3 3 2 3
|
75
|
+
# it assigns the tally
|
76
|
+
# 1 2 3
|
77
|
+
# 1 2 2 3
|
78
|
+
# 2 0 2 3
|
79
|
+
# 3 0 1 3
|
80
|
+
def tally_marks
|
81
|
+
@tally={ }
|
82
|
+
@marks.each do |entree, marks|
|
83
|
+
@tally[entree] ||= { }
|
84
|
+
1.upto(@marks.size) do |place|
|
85
|
+
@tally[entree][place] = 0
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
@marks.each do |entree, marks|
|
90
|
+
marks.each do |judge, placing|
|
91
|
+
placing.upto(@marks.size) do |place|
|
92
|
+
@tally[entree][place] += 1
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module SkatingSystem
|
2
|
+
class Scorer
|
3
|
+
def score(scorable)
|
4
|
+
score_performance(scorable) if scorable.respond_to?(:couples_marks)
|
5
|
+
end
|
6
|
+
|
7
|
+
def score_performance(performance)
|
8
|
+
results = PerformanceResults.new
|
9
|
+
results.score(performance.couples_marks)
|
10
|
+
results
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
require File.dirname(__FILE__)+'/../spec_helper'
|
2
|
+
|
3
|
+
describe SkatingSystem::PerformanceResults, "tallying the marks" do
|
4
|
+
before(:all) do
|
5
|
+
@performance_resuts = SkatingSystem::PerformanceResults.new
|
6
|
+
|
7
|
+
@judge1 = mock("Judge")
|
8
|
+
@judge2 = mock("Judge")
|
9
|
+
@judge3 = mock("Judge")
|
10
|
+
|
11
|
+
@entree1 = mock("Entree")
|
12
|
+
@entree2 = mock("Entree")
|
13
|
+
@entree3 = mock("Entree")
|
14
|
+
|
15
|
+
# A B C 1 2 3
|
16
|
+
# 1 1 1 3 2 2 3 1
|
17
|
+
# 2 2 3 2 0 2 3 2
|
18
|
+
# 3 3 2 3 0 1 3 3
|
19
|
+
@couples_marks = { @entree1=>{@judge1=>1, @judge2=>1, @judge3=>3 },
|
20
|
+
@entree2=>{@judge1=>2, @judge2=>3, @judge3=>2 },
|
21
|
+
@entree3=>{@judge1=>3, @judge2=>2, @judge3=>3 }}
|
22
|
+
|
23
|
+
@performance_resuts.score(@couples_marks)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should know how many first places entree1 got" do
|
27
|
+
@performance_resuts[@entree1][1].should be(2)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should know how many first places entree2 got" do
|
31
|
+
@performance_resuts[@entree2][1].should be(0)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should know how many first places entree3 got" do
|
35
|
+
@performance_resuts[@entree3][1].should be(0)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should know how many 2nd or better places entree1 got" do
|
39
|
+
@performance_resuts[@entree1][2].should be(2)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should know how many 2nd or better places entree2 got" do
|
43
|
+
@performance_resuts[@entree2][2].should be(2)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should know how many 2nd or better places entree3 got" do
|
47
|
+
@performance_resuts[@entree3][2].should be(1)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should know how many 3rd or better places entree1 got" do
|
51
|
+
@performance_resuts[@entree1][3].should be(3)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should know how many 3rd or better places entree2 got" do
|
55
|
+
@performance_resuts[@entree2][3].should be(3)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should know how many 3rd or better places entree3 got" do
|
59
|
+
@performance_resuts[@entree3][3].should be(3)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
#
|
64
|
+
#
|
65
|
+
#describe "scoring a final that can be decided on just rule 5" do
|
66
|
+
# before(:all) do
|
67
|
+
# @performance_resuts = PerformanceResults.new
|
68
|
+
#
|
69
|
+
# @judge1 = mock_model(Judge)
|
70
|
+
# @judge2 = mock_model(Judge)
|
71
|
+
# @judge3 = mock_model(Judge)
|
72
|
+
#
|
73
|
+
# @entree1 = mock_model(Entree)
|
74
|
+
# @entree2 = mock_model(Entree)
|
75
|
+
# @entree3 = mock_model(Entree)
|
76
|
+
#
|
77
|
+
# # A B C 1 2 3
|
78
|
+
# # 1 1 1 3 2 2 3 1
|
79
|
+
# # 2 2 3 2 0 2 3 2
|
80
|
+
# # 3 3 2 3 0 1 3 3
|
81
|
+
# @couples_marks = { @entree1=>{@judge1=>1, @judge2=>1, @judge3=>3 },
|
82
|
+
# @entree2=>{@judge1=>2, @judge2=>3, @judge3=>2 },
|
83
|
+
# @entree3=>{@judge1=>3, @judge2=>2, @judge3=>3 }}
|
84
|
+
#
|
85
|
+
# @performance_resuts.score(@couples_marks)
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# it "should list entree 1 as first place" do
|
89
|
+
# @performance_resuts[@entree1][:result].should be(1)
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# it "should list entree 2 as second place" do
|
93
|
+
# @performance_resuts[@entree2][:result].should be(2)
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# it "should list entree 3 as third place" do
|
97
|
+
# @performance_resuts[@entree3][:result].should be(3)
|
98
|
+
# end
|
99
|
+
#end
|
100
|
+
#
|
101
|
+
#
|
102
|
+
#describe "scoring a final that requires rule 6" do
|
103
|
+
#
|
104
|
+
# before(:all) do
|
105
|
+
# @performance_resuts = PerformanceResults.new
|
106
|
+
#
|
107
|
+
# @judgea = mock_model(Judge)
|
108
|
+
# @judgeb = mock_model(Judge)
|
109
|
+
# @judgec = mock_model(Judge)
|
110
|
+
# @judged = mock_model(Judge)
|
111
|
+
# @judgee = mock_model(Judge)
|
112
|
+
#
|
113
|
+
# @entree1 = mock_model(Entree, :number=>1)
|
114
|
+
# @entree2 = mock_model(Entree, :number=>2)
|
115
|
+
# @entree3 = mock_model(Entree, :number=>3)
|
116
|
+
# @entree4 = mock_model(Entree, :number=>4)
|
117
|
+
# @entree5 = mock_model(Entree, :number=>5)
|
118
|
+
#
|
119
|
+
# # A B C D E 1 2 3 4 5
|
120
|
+
# # 1 1 1 1 5 5 3 1
|
121
|
+
# # 2 4 2 4 1 2 1 3 3
|
122
|
+
# # 3 2 4 2 2 1 1 4 2
|
123
|
+
# # 4 3 5 3 4 4 2 5
|
124
|
+
# # 5 5 3 5 3 3 3 4
|
125
|
+
#
|
126
|
+
# @couples_marks = {
|
127
|
+
# @entree1=>{@judgea=>1, @judgeb=>1, @judgec=>1, @judged=>5, @judgee=>5 },
|
128
|
+
# @entree2=>{@judgea=>4, @judgeb=>2, @judgec=>4, @judged=>1, @judgee=>2 },
|
129
|
+
# @entree3=>{@judgea=>2, @judgeb=>4, @judgec=>2, @judged=>2, @judgee=>1 },
|
130
|
+
# @entree4=>{@judgea=>3, @judgeb=>5, @judgec=>3, @judged=>4, @judgee=>4 },
|
131
|
+
# @entree5=>{@judgea=>5, @judgeb=>3, @judgec=>5, @judged=>3, @judgee=>3 }
|
132
|
+
# }
|
133
|
+
#
|
134
|
+
# @performance_resuts.score(@couples_marks)
|
135
|
+
# end
|
136
|
+
#
|
137
|
+
# it "should list entree 1 as first place" do
|
138
|
+
# @performance_resuts[@entree1][:result].should be(1)
|
139
|
+
# end
|
140
|
+
#
|
141
|
+
# it "should list entree 2 as third place" do
|
142
|
+
# @performance_resuts[@entree2][:result].should be(3)
|
143
|
+
# end
|
144
|
+
#
|
145
|
+
# it "should list entree 3 as second place" do
|
146
|
+
# @performance_resuts[@entree3][:result].should be(2)
|
147
|
+
# end
|
148
|
+
#
|
149
|
+
# it "should list entree 4 as fifth place" do
|
150
|
+
# @performance_resuts[@entree4][:result].should be(5)
|
151
|
+
# end
|
152
|
+
#
|
153
|
+
# it "should list entree 5 as forth place" do
|
154
|
+
# @performance_resuts[@entree5][:result].should be(4)
|
155
|
+
# end
|
156
|
+
#end
|
157
|
+
#
|
158
|
+
#
|
159
|
+
#describe "scoring a final that requires rule 7" do
|
160
|
+
#
|
161
|
+
# before(:all) do
|
162
|
+
# @performance_resuts = PerformanceResults.new
|
163
|
+
#
|
164
|
+
# @judgea = mock_model(Judge)
|
165
|
+
# @judgeb = mock_model(Judge)
|
166
|
+
# @judgec = mock_model(Judge)
|
167
|
+
# @judged = mock_model(Judge)
|
168
|
+
# @judgee = mock_model(Judge)
|
169
|
+
#
|
170
|
+
# @entree1 = mock_model(Entree, :number=>1)
|
171
|
+
# @entree2 = mock_model(Entree, :number=>2)
|
172
|
+
# @entree3 = mock_model(Entree, :number=>3)
|
173
|
+
# @entree4 = mock_model(Entree, :number=>4)
|
174
|
+
# @entree5 = mock_model(Entree, :number=>5)
|
175
|
+
#
|
176
|
+
# # A B C D E 1 2 3 4 5
|
177
|
+
# # 1 1 1 1 5 5 3 1
|
178
|
+
# # 2 2 4 3 2 2 3 3
|
179
|
+
# # 3 4 2 4 1 1 2 3 2
|
180
|
+
# # 4 3 5 2 4 4 1 2 5
|
181
|
+
# # 5 5 3 5 3 3 3 4
|
182
|
+
#
|
183
|
+
# @couples_marks = {
|
184
|
+
# @entree1=>{@judgea=>1, @judgeb=>1, @judgec=>1, @judged=>5, @judgee=>5 },
|
185
|
+
# @entree3=>{@judgea=>2, @judgeb=>3, @judgec=>4, @judged=>2, @judgee=>2 },
|
186
|
+
# @entree2=>{@judgea=>4, @judgeb=>2, @judgec=>4, @judged=>1, @judgee=>1 },
|
187
|
+
# @entree4=>{@judgea=>3, @judgeb=>5, @judgec=>2, @judged=>4, @judgee=>4 },
|
188
|
+
# @entree5=>{@judgea=>5, @judgeb=>3, @judgec=>5, @judged=>3, @judgee=>3 }
|
189
|
+
# }
|
190
|
+
#
|
191
|
+
# @performance_resuts.score(@couples_marks)
|
192
|
+
# end
|
193
|
+
#
|
194
|
+
# it "should list entree 1 as first place" do
|
195
|
+
# @performance_resuts[@entree1][:result].should be(1)
|
196
|
+
# end
|
197
|
+
#
|
198
|
+
# it "should list entree 2 as third place" do
|
199
|
+
# @performance_resuts[@entree2][:result].should be(3)
|
200
|
+
# end
|
201
|
+
#
|
202
|
+
# it "should list entree 3 as second place" do
|
203
|
+
# @performance_resuts[@entree3][:result].should be(2)
|
204
|
+
# end
|
205
|
+
#
|
206
|
+
# it "should list entree 4 as fifth place" do
|
207
|
+
# @performance_resuts[@entree4][:result].should be(5)
|
208
|
+
# end
|
209
|
+
#
|
210
|
+
# it "should list entree 5 as forth place" do
|
211
|
+
# @performance_resuts[@entree5][:result].should be(4)
|
212
|
+
# end
|
213
|
+
#end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe "scoring a final performance" do
|
5
|
+
before(:each) do
|
6
|
+
@scorer = SkatingSystem::Scorer.new
|
7
|
+
@performance = mock("Performance")
|
8
|
+
@couples_marks = {:key=>:value }
|
9
|
+
SkatingSystem::PerformanceResults.stub!(:new).and_return(@pr=mock(SkatingSystem::PerformanceResults, :score=>nil))
|
10
|
+
@performance.stub!(:couples_marks).and_return(@couples_marks)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should check if the scorable thing respondes to :couples_marks" do
|
14
|
+
@performance.should_receive(:respond_to?).with(:couples_marks).and_return(true)
|
15
|
+
@scorer.score(@performance)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should delegate to score_performance" do
|
19
|
+
@scorer.should_receive(:score_performance).with(@performance)
|
20
|
+
@scorer.score(@performance)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return a the new Performance" do
|
24
|
+
@scorer.score_performance(@performance).should be(@pr)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should ask the PerformanceResults to score itself" do
|
28
|
+
@pr.should_receive(:score).with(@couples_marks)
|
29
|
+
@scorer.score(@performance)
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wildfalcon-skating_system
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Laurie Young
|
8
|
+
autorequire: skating_system
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-25 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: skating_system@wildfalcon.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- lib/skating_system.rb
|
26
|
+
- lib/skating_system/scorer.rb
|
27
|
+
- lib/skating_system/performance_results.rb
|
28
|
+
- spec/spec.opts
|
29
|
+
- spec/spec_helper.rb
|
30
|
+
- spec/spec/scorer_spec.rb
|
31
|
+
- spec/spec/performance_results_spec.rb
|
32
|
+
- README
|
33
|
+
has_rdoc: true
|
34
|
+
homepage:
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: Implemetation of the skating system.
|
59
|
+
test_files: []
|
60
|
+
|