Top Level Namespace

Defined Under Namespace

Modules: Average Classes: Array

Instance Method Summary collapse

Instance Method Details

#array_contain_digits?(array) ⇒ Boolean

Returns:

  • (Boolean)

9
10
11
12
13
14
15
16
17
18
# File 'lib/average/average_helper.rb', line 9

def array_contain_digits?(array)
  # Go through all elements
  result = true
  array.each do |array_element|
    if not value_can_be_handled?(array_element)
      result = false
    end
  end
  result
end

#calculate_median(array) ⇒ Object


7
8
9
10
11
12
13
14
# File 'lib/average/median.rb', line 7

def calculate_median(array)
  array.sort!
  if array.length.even?
    get_mean( middle_items_to_average(array) )
  else
    array[ array.length / 2.to_f ]
  end
end

#clean_array(array) ⇒ Object


36
37
38
39
40
41
42
43
# File 'lib/average/average_helper.rb', line 36

def clean_array(array)
  array.each_with_index do |val, index|
    if val.is_a?(String)
      val.include?('.') ? (array[index] = val.to_f) : (array[index] = val.to_i)
    end
  end
  array
end

#get_mean(array) ⇒ Object


1
2
3
4
5
# File 'lib/average/mean.rb', line 1

def get_mean(array)
  return if not valid_array?(array)
  array = clean_array(array)
  array.inject(0) { |sum, x| sum += x } / array.size.to_f
end

#get_median(array) ⇒ Object


1
2
3
4
5
# File 'lib/average/median.rb', line 1

def get_median(array)
  return if not valid_array?(array)
  array = clean_array(array)
  calculate_median(array)
end

#get_mode(array) ⇒ Object


1
2
3
4
5
6
# File 'lib/average/mode.rb', line 1

def get_mode(array)
  return if not valid_array?(array)
  array  = clean_array(array)
  result = repetition_hash(array)
  result[:hash_result].select { |key, value| value == result[:max_repetition] }.keys.collect { |float| float.to_i }
end

#looks_like_a_digit?(digit) ⇒ Boolean

Returns:

  • (Boolean)

26
27
28
29
30
31
32
33
34
# File 'lib/average/average_helper.rb', line 26

def looks_like_a_digit?(digit)
  if digit.is_a?(String) && digit !~ /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/
    # is _not_ a string that could be turned to a digit
    false
  else
    # is a string that could be a digit
    true
  end
end

#middle_item(array) ⇒ Object


16
17
18
# File 'lib/average/median.rb', line 16

def middle_item(array)
  array.size / 2
end

#middle_items_to_average(array) ⇒ Object


20
21
22
# File 'lib/average/median.rb', line 20

def middle_items_to_average(array)
  array[ middle_item(array) -1 .. middle_item(array) ]
end

#repetition_hash(array) ⇒ Object


8
9
10
11
12
13
# File 'lib/average/mode.rb', line 8

def repetition_hash(array)
  return if !valid_array?(array)
  array  = clean_array(array)
  result = array.inject({}) { |key, value| key[value] = array.count(value.to_f); key }
  { hash_result: result, max_repetition: result.values.max }
end

#unique_mode(array) ⇒ Object


15
16
17
18
19
20
# File 'lib/average/mode.rb', line 15

def unique_mode(array)
  # only for Ruby versions bigger than 1.8.7 -> http://apidock.com/ruby/v1_8_7_72/Enumerable/max_by
  return if !valid_array?(array)
  array  = clean_array(array)
  array.max_by { |x| array.count(x) }
end

#valid_array?(array) ⇒ Boolean

Returns:

  • (Boolean)

1
2
3
4
5
6
7
# File 'lib/average/average_helper.rb', line 1

def valid_array?(array)
  # Valid if:
  # - the class of the parameter 'array' respond to Array
  # - the parameter 'Array' is not null or empty.
  # - The array given contains Integers, Floats, and Strings that could be turned to a valid digit.
  ( array.is_a?(Array) && !array.nil? && !array.empty? && array_contain_digits?(array) )
end

#value_can_be_handled?(element) ⇒ Boolean

Returns:

  • (Boolean)

20
21
22
23
# File 'lib/average/average_helper.rb', line 20

def value_can_be_handled?(element)
  # Check if the elements of the array 'look like numbers'.
  element.is_a?(Integer) || element.is_a?(Float) || looks_like_a_digit?(element)
end