exc1_sum_squares 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.project +5 -0
- data/bin/exc1_sum_squares +5 -1
- data/lib/exc1_sum_squares/solver.rb +31 -3
- data/lib/exc1_sum_squares/version.rb +1 -1
- data/lib/exc1_sum_squares.rb +1 -4
- data/spec/acceptance/acceptance_spec.rb +1 -1
- data/spec/unit/solver_spec.rb +9 -1
- metadata +2 -2
data/.project
CHANGED
data/bin/exc1_sum_squares
CHANGED
@@ -22,7 +22,11 @@ class Exc1SumSquaresRunner < Thor
|
|
22
22
|
|
23
23
|
desc "solve", "Solves the min and max square values of a 2D array"
|
24
24
|
def solve(input)
|
25
|
-
|
25
|
+
# first we need to get the string and convert it into an array...
|
26
|
+
yaml_array = input.gsub(/(\,)(\S)/, "\\1 \\2")
|
27
|
+
array = YAML::load(yaml_array)
|
28
|
+
|
29
|
+
s = Exc1SumSquares.solve(array)
|
26
30
|
|
27
31
|
output = "Min: #{s.min}\nMax: #{s.max}"
|
28
32
|
puts output
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Exc1SumSquares
|
2
2
|
class Solver
|
3
|
-
attr_reader :sums, :min, :max, :matrix
|
3
|
+
attr_reader :sums, :min, :max, :matrix, :min_slots, :max_slots
|
4
4
|
|
5
5
|
def matrix=(val)
|
6
6
|
@matrix = val
|
@@ -11,12 +11,12 @@ module Exc1SumSquares
|
|
11
11
|
|
12
12
|
def min
|
13
13
|
calculate_sums unless @matrix.empty?
|
14
|
-
@
|
14
|
+
@min
|
15
15
|
end
|
16
16
|
|
17
17
|
def max
|
18
18
|
calculate_sums unless @matrix.empty?
|
19
|
-
@
|
19
|
+
@max
|
20
20
|
end
|
21
21
|
|
22
22
|
def sums
|
@@ -34,8 +34,36 @@ module Exc1SumSquares
|
|
34
34
|
validate_matrix
|
35
35
|
|
36
36
|
@sums = choose_the_top_and_bottom_rows_to_calculate(@matrix)
|
37
|
+
@min = @sums.min
|
38
|
+
@max = @sums.max
|
39
|
+
@min_slots = calculate_min_slots(@matrix)
|
40
|
+
@max_slots = calculate_max_slots(@matrix)
|
37
41
|
end
|
38
42
|
|
43
|
+
# Find out which slots of the sums array count as minimum slots
|
44
|
+
def calculate_min_slots(matrix)
|
45
|
+
output = []
|
46
|
+
@sums.each.with_index do |e, i|
|
47
|
+
if e == @min
|
48
|
+
output << i
|
49
|
+
end
|
50
|
+
end
|
51
|
+
#require 'pry';binding.pry
|
52
|
+
output
|
53
|
+
end
|
54
|
+
|
55
|
+
# Find out which slots of the sums array count as maximum slots
|
56
|
+
def calculate_max_slots(matrix)
|
57
|
+
output = []
|
58
|
+
@sums.each.with_index do |e, i|
|
59
|
+
if e == @max
|
60
|
+
output << i
|
61
|
+
end
|
62
|
+
end
|
63
|
+
output
|
64
|
+
end
|
65
|
+
|
66
|
+
|
39
67
|
def choose_the_top_and_bottom_rows_to_calculate(matrix)
|
40
68
|
sums = []
|
41
69
|
for i in 0...(matrix.length-1) do
|
data/lib/exc1_sum_squares.rb
CHANGED
@@ -3,7 +3,7 @@ require 'exc1_sum_squares'
|
|
3
3
|
|
4
4
|
describe Exc1SumSquares do
|
5
5
|
it 'should solve a problem' do
|
6
|
-
output = Exc1SumSquares::solve(
|
6
|
+
output = Exc1SumSquares::solve([ [1, 2, 3, 4], [5, 6, 7, 8] ])
|
7
7
|
output.min.should be 14
|
8
8
|
output.max.should be 22
|
9
9
|
end
|
data/spec/unit/solver_spec.rb
CHANGED
@@ -10,6 +10,7 @@ describe Exc1SumSquares::Solver do
|
|
10
10
|
[9, 2, 0, 4, 6],
|
11
11
|
[1, 2, 6, 1, 0] ]
|
12
12
|
@solver = Exc1SumSquares::Solver.new @test_array
|
13
|
+
@solver.calculate_sums
|
13
14
|
end
|
14
15
|
|
15
16
|
describe @solver do
|
@@ -29,7 +30,6 @@ describe Exc1SumSquares::Solver do
|
|
29
30
|
end
|
30
31
|
|
31
32
|
its(:sums) {
|
32
|
-
#require 'pry'; binding.pry
|
33
33
|
@solver.sums.should be_a_kind_of Array
|
34
34
|
}
|
35
35
|
|
@@ -49,6 +49,14 @@ describe Exc1SumSquares::Solver do
|
|
49
49
|
@solver.max.should eq(26)
|
50
50
|
end
|
51
51
|
|
52
|
+
it '.min_slots should contain an array of slots which constitute the minimum of the sums array' do
|
53
|
+
@solver.min_slots.should eq [9]
|
54
|
+
end
|
55
|
+
|
56
|
+
it '.max_slots should be the correct answer' do
|
57
|
+
@solver.max_slots.should eq [1]
|
58
|
+
end
|
59
|
+
|
52
60
|
end
|
53
61
|
|
54
62
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: exc1_sum_squares
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|