Module: Tournament::Algorithm::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/tournament/algorithm/util.rb

Overview

This module provides utility functions for helping implement other algorithms.

Instance Method Summary collapse

Instance Method Details

#all_min_by(array) {|element| ... } ⇒ Array<element>

Collect all values in an array with a minimum value.

Parameters:

  • array (Array<element>)

Yield Parameters:

  • element

    an element of the array

Yield Returns:

  • (#<, #==)

    some value to find the minimum of

Returns:

  • (Array<element>)

    all elements with the 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.

Parameters:

  • teams (Array<team>)

Returns:

  • (Array<team, nil>)


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.

Examples:

padded_teams_count(teams.length/) == padd_teams(teams).length

Parameters:

  • teams_count (Integer)

    the number of teams

Returns:

  • (Integer)


27
28
29
# File 'lib/tournament/algorithm/util.rb', line 27

def padded_teams_count(teams_count)
  (teams_count / 2.0).ceil * 2
end