Class: Exc1SumSquares::Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/exc1_sum_squares/solver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_matrix = []) ⇒ Solver

Returns a new instance of Solver.



27
28
29
# File 'lib/exc1_sum_squares/solver.rb', line 27

def initialize(input_matrix = [])
  @matrix = input_matrix
end

Instance Attribute Details

#matrixObject

Returns the value of attribute matrix.



3
4
5
# File 'lib/exc1_sum_squares/solver.rb', line 3

def matrix
  @matrix
end

#maxObject (readonly)

Returns the value of attribute max.



3
4
5
# File 'lib/exc1_sum_squares/solver.rb', line 3

def max
  @max
end

#max_slotsObject (readonly)

Returns the value of attribute max_slots.



3
4
5
# File 'lib/exc1_sum_squares/solver.rb', line 3

def max_slots
  @max_slots
end

#minObject (readonly)

before_filter :calculate_sums, :only => [:min, :max]



12
13
14
# File 'lib/exc1_sum_squares/solver.rb', line 12

def min
  @min
end

#min_slotsObject (readonly)

Returns the value of attribute min_slots.



3
4
5
# File 'lib/exc1_sum_squares/solver.rb', line 3

def min_slots
  @min_slots
end

#sumsObject (readonly)

Returns the value of attribute sums.



3
4
5
# File 'lib/exc1_sum_squares/solver.rb', line 3

def sums
  @sums
end

Instance Method Details

#calculate_max_slots(matrix) ⇒ Object

Find out which slots of the sums array count as maximum slots



56
57
58
59
60
61
62
63
64
# File 'lib/exc1_sum_squares/solver.rb', line 56

def calculate_max_slots(matrix)
  output = []
  @sums.each.with_index do |e, i|
    if e == @max
      output << i
    end
  end
  output
end

#calculate_min_slots(matrix) ⇒ Object

Find out which slots of the sums array count as minimum slots



44
45
46
47
48
49
50
51
52
53
# File 'lib/exc1_sum_squares/solver.rb', line 44

def calculate_min_slots(matrix)
  output = []
  @sums.each.with_index do |e, i|
    if e == @min
      output << i
    end
  end
  #require 'pry';binding.pry
  output
end

#calculate_sums(matrix = @matrix) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/exc1_sum_squares/solver.rb', line 33

def calculate_sums(matrix = @matrix)
  validate_matrix
  
  @sums = choose_the_top_and_bottom_rows_to_calculate(@matrix)
  @min = @sums.min
  @max = @sums.max
  @min_slots = calculate_min_slots(@matrix)
  @max_slots = calculate_max_slots(@matrix)
end

#choose_the_top_and_bottom_rows_to_calculate(matrix) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/exc1_sum_squares/solver.rb', line 67

def choose_the_top_and_bottom_rows_to_calculate(matrix)
  sums = []
  for i in 0...(matrix.length-1) do
    top_row = matrix[i]
    bottom_row = matrix[i+1]
    
    sums = sum_each_square_set_of_the_chosen_rows(top_row, bottom_row, i, sums)
  end
  sums
end

#is_valid_matrix_contents?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
# File 'lib/exc1_sum_squares/solver.rb', line 95

def is_valid_matrix_contents?
  @matrix.each do |array|
    all_elements_are_numbers = array.all? {|element| element.class == Fixnum}
    return false unless all_elements_are_numbers
  end
  true
end

#is_valid_matrix_size?Boolean

what do you think of this method name? Is no arguments cool?

Returns:

  • (Boolean)


104
105
106
107
108
109
# File 'lib/exc1_sum_squares/solver.rb', line 104

def is_valid_matrix_size?
  for i in 0...(@matrix.length-1) do
    return false if @matrix[i].length != @matrix[i+1].length
  end
  true
end

#sum_each_square_set_of_the_chosen_rows(top_row, bottom_row, i, sums) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/exc1_sum_squares/solver.rb', line 78

def sum_each_square_set_of_the_chosen_rows(top_row, bottom_row, i, sums)
  for j in 0...(top_row.length-1) do
    a = top_row[j+0] 
    b = top_row[j+1] 
    c = bottom_row[j+0] 
    d = bottom_row[j+1]
    sums << (a+b+c+d)
  end
  sums
end

#validate_matrixObject



89
90
91
92
93
# File 'lib/exc1_sum_squares/solver.rb', line 89

def validate_matrix
  raise 'Invalid Matrix Error: Empty Matrix' if @matrix.empty?
  raise 'Invalid Matrix Error: Lengths' unless is_valid_matrix_size?
  raise 'Invalid Matrix Error: Contents' unless is_valid_matrix_contents?
end