product_table 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ product_table (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ coderay (1.0.9)
10
+ diff-lcs (1.2.4)
11
+ method_source (0.8.2)
12
+ pry (0.9.12.2)
13
+ coderay (~> 1.0.5)
14
+ method_source (~> 0.8)
15
+ slop (~> 3.4)
16
+ rspec (2.14.1)
17
+ rspec-core (~> 2.14.0)
18
+ rspec-expectations (~> 2.14.0)
19
+ rspec-mocks (~> 2.14.0)
20
+ rspec-core (2.14.5)
21
+ rspec-expectations (2.14.3)
22
+ diff-lcs (>= 1.1.3, < 2.0)
23
+ rspec-mocks (2.14.3)
24
+ slop (3.4.6)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ product_table!
31
+ pry
32
+ rspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/Readme.md ADDED
@@ -0,0 +1,11 @@
1
+
2
+ ## Instalation
3
+
4
+ `gem install product_table`
5
+
6
+ ## Running Instructions
7
+
8
+ ### Display product table of first 10 prime numbers
9
+
10
+ `product_table -n 10`
11
+
data/bin/product_table ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+ require 'product_table'
5
+
6
+ options = ProductTable::OptParser.parse(ARGV)
7
+ primes = ProductTable::Calculator.calculate_primes(options.n)
8
+ primes_product = ProductTable::Calculator.calculate_product(primes)
9
+ ProductTable::Output.new(primes, primes_product).print_table
10
+
@@ -0,0 +1,36 @@
1
+ require 'matrix'
2
+
3
+ module ProductTable
4
+
5
+ class Calculator
6
+
7
+ class << self
8
+
9
+ def calculate_primes(count)
10
+ return [] if count == 0
11
+ prime_numbers = [2]
12
+ current = 2
13
+ while prime_numbers.size < count
14
+ current += 1
15
+ prime_numbers << current if is_prime?(current)
16
+ end
17
+
18
+ prime_numbers
19
+ end
20
+
21
+ def calculate_product(numbers_list)
22
+ (Matrix[numbers_list].transpose * Matrix[numbers_list]).to_a
23
+ end
24
+
25
+ private
26
+
27
+ def is_prime?(number)
28
+ return false if number < 2
29
+ (2..number-1).count { |n| number % n == 0 } == 0
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,33 @@
1
+ require 'optparse'
2
+ require 'ostruct'
3
+
4
+ module ProductTable
5
+
6
+ class OptParser
7
+
8
+ def self.parse(params)
9
+ options = OpenStruct.new
10
+
11
+ parser = OptionParser.new do |opts|
12
+
13
+ opts.banner = "Usage: example.rb [options]"
14
+
15
+ opts.on("-n N", Integer, "print product table of N numbers") do |n|
16
+ options.n = n
17
+ end
18
+
19
+ end
20
+
21
+ parser.parse!(params)
22
+
23
+ if params.empty?
24
+ puts parser.help()
25
+ exit
26
+ end
27
+
28
+ options
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,59 @@
1
+ module ProductTable
2
+
3
+ class Output
4
+
5
+ def initialize(number_list, product_table)
6
+ @number_list = number_list
7
+ @product_table = product_table
8
+ end
9
+
10
+ def print_table
11
+ puts build_table
12
+ end
13
+
14
+ def build_table
15
+ @column_size = get_column_size + 2
16
+ @base_format = "%#{@column_size}s"*@number_list.count
17
+
18
+ format, output = build_header "" , []
19
+ format, output = build_separator format, output
20
+ format, output = build_rows format, output
21
+
22
+ output.flatten!
23
+ sprintf format, *output
24
+ end
25
+
26
+ private
27
+
28
+ #find element with most characters
29
+ def get_column_size
30
+ all_elements = (@number_list + @product_table).flatten
31
+ all_elements.map(&:to_s).max { |element| element.length }.length
32
+ end
33
+
34
+ def build_header(format="", output=[])
35
+ format += "%#{@column_size}s | " + @base_format
36
+ output << " "
37
+ output << @number_list
38
+ [format, output]
39
+ end
40
+
41
+ def build_separator(format="", output=[])
42
+ format += "%s"
43
+ output << "\n" + "-" * ((@column_size * @number_list.count) + 10)
44
+ [format, output]
45
+ end
46
+
47
+ def build_rows(format="", output=[])
48
+ @product_table.each_with_index do |row, index|
49
+ row_num = @number_list[index]
50
+ format += "\n%#{@column_size}s | " + @base_format
51
+ output << row_num.to_s
52
+ output << row
53
+ end
54
+ [format, output]
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,3 @@
1
+ module ProductTable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+ require 'product_table/calculator'
3
+ require 'product_table/output'
4
+ require 'product_table/opt_parser'
5
+
@@ -0,0 +1,23 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "product_table/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "product_table"
6
+ s.version = ProductTable::VERSION
7
+ s.authors = ["Iuri Matias"]
8
+ s.email = ["iuri.matias@gmail.com"]
9
+ s.homepage = "https://github.com/iurimatias/product_table"
10
+ s.summary = %q{code kata - calculate the produt of a list of numbers}
11
+ s.description = %q{}
12
+
13
+ s.rubyforge_project = "product_table"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "pry"
21
+ s.add_development_dependency "rspec"
22
+
23
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ module ProductTable
4
+
5
+ describe Calculator do
6
+
7
+ describe "#calculate_primes" do
8
+
9
+ it "should return a list of n prime numbers" do
10
+ Calculator.calculate_primes(5).should == [2,3,5,7,11]
11
+ end
12
+
13
+ end
14
+
15
+ describe "#calculate_product" do
16
+
17
+ it "should return a product table of a number list" do
18
+ product_table = Calculator.calculate_product([2,2,3])
19
+ product_table.should == [
20
+ [4,4,6],
21
+ [4,4,6],
22
+ [6,6,9]
23
+ ]
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ module ProductTable
4
+
5
+ describe Output do
6
+
7
+ let(:list) { [2,3,5,7] }
8
+ let(:matrix) {
9
+ [ [4, 6, 10, 14],
10
+ [6, 9, 15, 21],
11
+ [10, 15, 25, 35],
12
+ [14, 21, 35, 49] ] }
13
+
14
+ let(:output) { Output.new(list, matrix) }
15
+
16
+ describe "#print_table" do
17
+
18
+ it "should print a table using a list as the headers and the matrix as the cells" do
19
+ correct_output = ""
20
+ correct_output += " | 2 3 5 7"
21
+ correct_output += "\n--------------------------"
22
+ correct_output += "\n 2 | 4 6 10 14"
23
+ correct_output += "\n 3 | 6 9 15 21"
24
+ correct_output += "\n 5 | 10 15 25 35"
25
+ correct_output += "\n 7 | 14 21 35 49"
26
+
27
+ output.build_table.should == correct_output
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'product_table'
3
+
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: product_table
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Iuri Matias
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-27 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: pry
17
+ requirement: &2166534280 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2166534280
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &2166533820 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2166533820
37
+ description: ''
38
+ email:
39
+ - iuri.matias@gmail.com
40
+ executables:
41
+ - product_table
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - Gemfile.lock
48
+ - Rakefile
49
+ - Readme.md
50
+ - bin/product_table
51
+ - lib/product_table.rb
52
+ - lib/product_table/calculator.rb
53
+ - lib/product_table/opt_parser.rb
54
+ - lib/product_table/output.rb
55
+ - lib/product_table/version.rb
56
+ - product_table.gemspec
57
+ - spec/lib/product_table/calculator_spec.rb
58
+ - spec/lib/product_table/output_spec.rb
59
+ - spec/spec_helper.rb
60
+ has_rdoc: true
61
+ homepage: https://github.com/iurimatias/product_table
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project: product_table
81
+ rubygems_version: 1.6.2
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: code kata - calculate the produt of a list of numbers
85
+ test_files:
86
+ - spec/lib/product_table/calculator_spec.rb
87
+ - spec/lib/product_table/output_spec.rb
88
+ - spec/spec_helper.rb