exc1_sum_squares 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.project +12 -0
- data/Gemfile +4 -0
- data/Rakefile +34 -0
- data/bin/exc1_sum_squares +40 -0
- data/cpc.rb +23 -0
- data/exc1_sum_squares.gemspec +27 -0
- data/lib/exc1_sum_squares/solver.rb +84 -0
- data/lib/exc1_sum_squares/version.rb +3 -0
- data/lib/exc1_sum_squares.rb +20 -0
- data/spec/acceptance/acceptance_spec.rb +10 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/unit/solver_spec.rb +107 -0
- metadata +139 -0
data/.project
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<projectDescription>
|
3
|
+
<name>exc1_sum_squares</name>
|
4
|
+
<comment></comment>
|
5
|
+
<projects>
|
6
|
+
</projects>
|
7
|
+
<buildSpec>
|
8
|
+
</buildSpec>
|
9
|
+
<natures>
|
10
|
+
<nature>com.aptana.ruby.core.rubynature</nature>
|
11
|
+
</natures>
|
12
|
+
</projectDescription>
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
include Rake::DSL
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
require "rspec/core/rake_task" # RSpec 2.0
|
6
|
+
|
7
|
+
# this is another test, run 'rake spec' and it goes through your spec folder running tests
|
8
|
+
RSpec::Core::RakeTask.new(:unit) do |spec|
|
9
|
+
#spec.pattern = 'spec/*/*_spec.rb'
|
10
|
+
spec.pattern = 'spec/unit/*_spec.rb'
|
11
|
+
spec.rspec_opts = ['--backtrace']
|
12
|
+
end
|
13
|
+
|
14
|
+
# this is another test, run 'rake spec' and it goes through your spec folder running tests
|
15
|
+
RSpec::Core::RakeTask.new(:acceptance) do |spec|
|
16
|
+
#spec.pattern = 'spec/*/*_spec.rb'
|
17
|
+
spec.pattern = 'spec/acceptance/*_spec.rb'
|
18
|
+
spec.rspec_opts = ['--backtrace']
|
19
|
+
end
|
20
|
+
|
21
|
+
# this is another test, run 'rake spec' and it goes through your spec folder running tests
|
22
|
+
RSpec::Core::RakeTask.new(:all) do |spec|
|
23
|
+
spec.pattern = 'spec/*/*_spec.rb'
|
24
|
+
end
|
25
|
+
|
26
|
+
# this is for running tests that you've marked current... eg: it 'should work', :current => true do
|
27
|
+
RSpec::Core::RakeTask.new(:current) do |spec|
|
28
|
+
spec.pattern = 'spec/*/*_spec.rb'
|
29
|
+
spec.rspec_opts = ['--tag current']
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
task :default => :all
|
34
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# The CLI interface is generated thanks to 'thor'. To use thor in your apps:
|
4
|
+
# 1) Add thor as a dependency in *.gemspecnd do a bundle install
|
5
|
+
# 2) Create a bin folder, and create a bin script that you'd like exposed to your shell path
|
6
|
+
# 3) Put the shebang at the top of the bin script
|
7
|
+
# 4) Requirements - Requir in thor and also your gem's name
|
8
|
+
# 5) *Runner Class - Create a class that inherits from Thor.
|
9
|
+
# 6) Customing your CLI commands as demonstrated in this file
|
10
|
+
# 7) Start
|
11
|
+
|
12
|
+
require 'thor'
|
13
|
+
require 'exc1_sum_squares'
|
14
|
+
|
15
|
+
class Exc1SumSquaresRunner < Thor
|
16
|
+
default_task :help # create a default task
|
17
|
+
|
18
|
+
desc "help", "Explains the gem's usage"
|
19
|
+
def help
|
20
|
+
Exc1SumSquares.help
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "solve", "Solves the min and max square values of a 2D array"
|
24
|
+
def solve(input)
|
25
|
+
s = Exc1SumSquares.solve(input)
|
26
|
+
|
27
|
+
output = "Min: #{s.min}\nMax: #{s.max}"
|
28
|
+
puts output
|
29
|
+
return 0
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "version", "Prints Gas's version"
|
33
|
+
def version
|
34
|
+
Gas.version
|
35
|
+
end
|
36
|
+
map %w(-v --version) => :version
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
Exc1SumSquaresRunner.start
|
data/cpc.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Notes: This file is customized to work with gems under development.
|
3
|
+
# It is expected to be run from the root of your project directory.
|
4
|
+
# It expects there to be a lib folder and within that folder, an .rb file with the
|
5
|
+
# same name as your project folder's name. From there you can work with your
|
6
|
+
# library right on the console.
|
7
|
+
|
8
|
+
#$: << File.dirname(__FILE__) + "/lib"
|
9
|
+
$: << Dir.getwd + "/lib"
|
10
|
+
|
11
|
+
if e = ARGV.shift
|
12
|
+
ENV['RACK_ENV'] = e
|
13
|
+
end
|
14
|
+
|
15
|
+
project_name = File.basename(Dir.getwd)
|
16
|
+
|
17
|
+
require "#{project_name}.rb"
|
18
|
+
#require 'application.rb'
|
19
|
+
require 'irb'
|
20
|
+
|
21
|
+
puts "CooperPress Console"
|
22
|
+
|
23
|
+
IRB.start
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "exc1_sum_squares/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "exc1_sum_squares"
|
7
|
+
s.version = Exc1SumSquares::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["TheNotary"]
|
10
|
+
s.email = ["no@email.plz"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{This gem is used for sum all squares in a matrix}
|
13
|
+
s.description = %q{Just an example gem}
|
14
|
+
|
15
|
+
s.rubyforge_project = "exc1_sum_squares"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency 'rake'
|
23
|
+
s.add_development_dependency 'rspec'
|
24
|
+
s.add_development_dependency 'rr'
|
25
|
+
s.add_development_dependency 'thor'
|
26
|
+
s.add_development_dependency 'pry'
|
27
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Exc1SumSquares
|
2
|
+
class Solver
|
3
|
+
attr_reader :sums, :min, :max, :matrix
|
4
|
+
|
5
|
+
def matrix=(val)
|
6
|
+
@matrix = val
|
7
|
+
calculate_sums
|
8
|
+
end
|
9
|
+
|
10
|
+
# before_filter :calculate_sums, :only => [:min, :max]
|
11
|
+
|
12
|
+
def min
|
13
|
+
calculate_sums unless @matrix.empty?
|
14
|
+
@sums.min
|
15
|
+
end
|
16
|
+
|
17
|
+
def max
|
18
|
+
calculate_sums unless @matrix.empty?
|
19
|
+
@sums.max
|
20
|
+
end
|
21
|
+
|
22
|
+
def sums
|
23
|
+
calculate_sums unless @matrix.empty?
|
24
|
+
@sums
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(input_matrix = [])
|
28
|
+
@matrix = input_matrix
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
def calculate_sums(matrix = @matrix)
|
34
|
+
validate_matrix
|
35
|
+
|
36
|
+
@sums = choose_the_top_and_bottom_rows_to_calculate(@matrix)
|
37
|
+
end
|
38
|
+
|
39
|
+
def choose_the_top_and_bottom_rows_to_calculate(matrix)
|
40
|
+
sums = []
|
41
|
+
for i in 0...(matrix.length-1) do
|
42
|
+
top_row = matrix[i]
|
43
|
+
bottom_row = matrix[i+1]
|
44
|
+
|
45
|
+
sums = sum_each_square_set_of_the_chosen_rows(top_row, bottom_row, i, sums)
|
46
|
+
end
|
47
|
+
sums
|
48
|
+
end
|
49
|
+
|
50
|
+
def sum_each_square_set_of_the_chosen_rows(top_row, bottom_row, i, sums)
|
51
|
+
for j in 0...(top_row.length-1) do
|
52
|
+
a = top_row[j+0]
|
53
|
+
b = top_row[j+1]
|
54
|
+
c = bottom_row[j+0]
|
55
|
+
d = bottom_row[j+1]
|
56
|
+
sums << (a+b+c+d)
|
57
|
+
end
|
58
|
+
sums
|
59
|
+
end
|
60
|
+
|
61
|
+
def validate_matrix
|
62
|
+
raise 'Invalid Matrix Error: Empty Matrix' if @matrix.empty?
|
63
|
+
raise 'Invalid Matrix Error: Lengths' unless is_valid_matrix_size?
|
64
|
+
raise 'Invalid Matrix Error: Contents' unless is_valid_matrix_contents?
|
65
|
+
end
|
66
|
+
|
67
|
+
def is_valid_matrix_contents?
|
68
|
+
@matrix.each do |array|
|
69
|
+
all_elements_are_numbers = array.all? {|element| element.class == Fixnum}
|
70
|
+
return false unless all_elements_are_numbers
|
71
|
+
end
|
72
|
+
true
|
73
|
+
end
|
74
|
+
|
75
|
+
# what do you think of this method name? Is no arguments cool?
|
76
|
+
def is_valid_matrix_size?
|
77
|
+
for i in 0...(@matrix.length-1) do
|
78
|
+
return false if @matrix[i].length != @matrix[i+1].length
|
79
|
+
end
|
80
|
+
true
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# You place you code within modules. To call a function from within a module, it looks like this...
|
2
|
+
# Exc1SumSquares::hello();
|
3
|
+
require 'exc1_sum_squares/solver'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
module Exc1SumSquares
|
7
|
+
def self.solve(input)
|
8
|
+
yaml_array = input.gsub(/(\,)(\S)/, "\\1 \\2")
|
9
|
+
array = YAML::load(yaml_array)
|
10
|
+
|
11
|
+
s = Exc1SumSquares::Solver.new array
|
12
|
+
|
13
|
+
return s
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.help
|
17
|
+
puts "This is a CLI app that solves something for you... use it like this..."
|
18
|
+
puts "$ exc1_sum_squares '[[1, 2, 3, 4], [5, 6, 7, 8]]'"
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'exc1_sum_squares'
|
3
|
+
|
4
|
+
|
5
|
+
describe Exc1SumSquares::Solver do
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
@test_array = [ [1, 4, 6, 1, 9],
|
9
|
+
[3, 7, 9, 2, 3],
|
10
|
+
[9, 2, 0, 4, 6],
|
11
|
+
[1, 2, 6, 1, 0] ]
|
12
|
+
@solver = Exc1SumSquares::Solver.new @test_array
|
13
|
+
end
|
14
|
+
|
15
|
+
describe @solver do
|
16
|
+
it 'should be able to make an object and it should be the right type' do
|
17
|
+
should be_an_instance_of(Exc1SumSquares::Solver)
|
18
|
+
end
|
19
|
+
|
20
|
+
it { should respond_to(:min, :max) }
|
21
|
+
|
22
|
+
it 'should respond to .sums which consists of all summed squares' do
|
23
|
+
should respond_to(:sums)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should have the input matrix exposed' do
|
27
|
+
should respond_to(:matrix)
|
28
|
+
@solver.matrix.should be @test_array
|
29
|
+
end
|
30
|
+
|
31
|
+
its(:sums) {
|
32
|
+
#require 'pry'; binding.pry
|
33
|
+
@solver.sums.should be_a_kind_of Array
|
34
|
+
}
|
35
|
+
|
36
|
+
it '.sums contains correct sums' do
|
37
|
+
@solver.sums[0].should be 15
|
38
|
+
@solver.sums[4].should be 21
|
39
|
+
@solver.sums[9].should be 10
|
40
|
+
end
|
41
|
+
|
42
|
+
it '.min should be the correct answer' do
|
43
|
+
@solver.min.should be @solver.sums.min
|
44
|
+
@solver.min.should be 10
|
45
|
+
end
|
46
|
+
|
47
|
+
it '.max should be the correct answer' do
|
48
|
+
@solver.max.should be @solver.sums.max
|
49
|
+
@solver.max.should eq(26)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
it 'should work for large arrays', :current => true do # tagging this test current allows you to single out the test on the command line with '$ rspec --tag current'
|
62
|
+
array = [ [1, 4, 6, 1, 9,2000],
|
63
|
+
[3, 7, 9, 2, 3,2],
|
64
|
+
[9, 2, 0, 4, 6,0],
|
65
|
+
[1, 2, 6, 1, 0,0],
|
66
|
+
[1, 2, 6, 1, 0,1] ]
|
67
|
+
solver = Exc1SumSquares::Solver.new array
|
68
|
+
|
69
|
+
solver.min.should be 1
|
70
|
+
solver.max.should be 2014
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'can take an array late' do
|
74
|
+
solver = Exc1SumSquares::Solver.new
|
75
|
+
solver.matrix = @test_array
|
76
|
+
|
77
|
+
solver.min.should be 10
|
78
|
+
end
|
79
|
+
|
80
|
+
it "shouldn't be capable of outputting inaccurate results" do
|
81
|
+
lambda { @solver.sums = [1, 2] }.should raise_error
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
describe 'Validates Matrix Input' do
|
86
|
+
it 'rejects misshapen matricies' do
|
87
|
+
invalid_matrix = [[1, 2, 3, 4],
|
88
|
+
[1, 2, 3]]
|
89
|
+
lambda do
|
90
|
+
solver = Exc1SumSquares::Solver.new invalid_matrix
|
91
|
+
solver.validate_matrix
|
92
|
+
end.should raise_error 'Invalid Matrix Error: Lengths'
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'rejects matricies that contain non-numbers' do
|
96
|
+
invalid_matrix = [[1, 2, 3, 4],
|
97
|
+
[1, 2, 3, "a"]]
|
98
|
+
lambda do
|
99
|
+
solver = Exc1SumSquares::Solver.new invalid_matrix
|
100
|
+
solver.validate_matrix
|
101
|
+
end.should raise_error 'Invalid Matrix Error: Contents'
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: exc1_sum_squares
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- TheNotary
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rr
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: thor
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: pry
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Just an example gem
|
95
|
+
email:
|
96
|
+
- no@email.plz
|
97
|
+
executables:
|
98
|
+
- exc1_sum_squares
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- .project
|
104
|
+
- Gemfile
|
105
|
+
- Rakefile
|
106
|
+
- bin/exc1_sum_squares
|
107
|
+
- cpc.rb
|
108
|
+
- exc1_sum_squares.gemspec
|
109
|
+
- lib/exc1_sum_squares.rb
|
110
|
+
- lib/exc1_sum_squares/solver.rb
|
111
|
+
- lib/exc1_sum_squares/version.rb
|
112
|
+
- spec/acceptance/acceptance_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
- spec/unit/solver_spec.rb
|
115
|
+
homepage: ''
|
116
|
+
licenses: []
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project: exc1_sum_squares
|
135
|
+
rubygems_version: 1.8.24
|
136
|
+
signing_key:
|
137
|
+
specification_version: 3
|
138
|
+
summary: This gem is used for sum all squares in a matrix
|
139
|
+
test_files: []
|