Module: Tournament::Algorithm::Util
Overview
This module provides utility functions for helping implement other algorithms.
Instance Method Summary collapse
-
#all_min_by(array) {|element| ... } ⇒ Array<element>
Collect all values in an array with a minimum value.
-
#padd_teams(teams) ⇒ Array<team, nil>
Padd an array of teams to be even.
-
#padded_teams_count(teams_count) ⇒ Integer
Padd the count of teams to be even.
Instance Method Details
#all_min_by(array) {|element| ... } ⇒ Array<element>
Collect all values in an array with a minimum value.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tournament/algorithm/util.rb', line 39 def all_min_by(array) min_elements = [] min_value = nil array.each do |element| value = yield element if !min_value || value < min_value min_elements = [element] min_value = value elsif value == min_value min_elements << element end end min_elements end |
#padd_teams(teams) ⇒ Array<team, nil>
Padd an array of teams to be even.
12 13 14 15 16 17 18 |
# File 'lib/tournament/algorithm/util.rb', line 12 def padd_teams(teams) if teams.length.odd? teams + [nil] else teams end end |
#padded_teams_count(teams_count) ⇒ Integer
Padd the count of teams to be even.
27 28 29 |
# File 'lib/tournament/algorithm/util.rb', line 27 def padded_teams_count(teams_count) (teams_count / 2.0).ceil * 2 end |