Class: EenieMeenie::Sorters::LateCoercion
- Inherits:
-
Base
- Object
- Base
- EenieMeenie::Sorters::LateCoercion
show all
- Defined in:
- lib/eenie_meenie/sorters/late_coercion.rb
Instance Method Summary
collapse
Methods inherited from Base
#load_option, #load_options
Constructor Details
#initialize(*args, options) ⇒ LateCoercion
Returns a new instance of LateCoercion.
4
5
6
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 4
def initialize(*args, options)
load_options(:groups, :population, options)
end
|
Instance Method Details
#assign ⇒ Object
18
19
20
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 18
def assign
coercion_threshold_reached? ? assign_with_coercion : assign_without_coercion
end
|
#assign_with_coercion ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 35
def assign_with_coercion
group = @groups.sample
group_tally = count_for_group(group)
other_group = (@groups - [group]).first
group = other_group if group_tally > count_for_group(other_group) + rand(leeway)
group
end
|
#assign_without_coercion ⇒ Object
22
23
24
25
26
27
28
29
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 22
def assign_without_coercion
selected_group = @groups.sample
if rand(@population) >= (@population / 2)
group = selected_group
else
group = the_other_group(selected_group)
end
end
|
#coercion_threshold_reached? ⇒ Boolean
56
57
58
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 56
def coercion_threshold_reached?
(current_population / @population) >= 0.9
end
|
#count_for_group(group) ⇒ Object
44
45
46
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 44
def count_for_group(group)
@results[group].to_f
end
|
#current_population ⇒ Object
52
53
54
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 52
def current_population
@results.values.inject(&:+).to_f
end
|
#leeway ⇒ Object
48
49
50
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 48
def leeway
((current_population / 2 ) * 0.01).round
end
|
#sort ⇒ Object
8
9
10
11
12
13
14
15
16
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 8
def sort
@results = {}
@groups.each { |group| @results.merge!(group => 0) }
@population.times do |i|
@results[assign] += 1
end
groups = @results
end
|
#the_other_group(group) ⇒ Object
31
32
33
|
# File 'lib/eenie_meenie/sorters/late_coercion.rb', line 31
def the_other_group group
(@groups - [group]).first
end
|