prime_test 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3add2a480a7c54e375030bfd34f4c9cd8c1e74ed
4
+ data.tar.gz: aafd0199068f7a1a43788d921774a02434b9548f
5
+ SHA512:
6
+ metadata.gz: addc3682165246e888bc264d1b4c2e39069326c9061f4360b8eb64521b57475192788f6c77cec2859da5997902ae959031ef2503eeeb81505130f77be75f9fcb
7
+ data.tar.gz: f38b3f9077370d79dd59351b4f68daf8937b5c9cec1f3fe1e3f4b54c12661390ac2d9f90170af905a692cc1464a04369e81675b372536bd62b09d09a6bc5d08d
data/bin/prime_test ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'prime_test'
4
+ PrimeTest.print_table(ARGV[0] || 10)
@@ -0,0 +1,32 @@
1
+ module PrimeTest
2
+ class PrimeCalculator
3
+
4
+ LOWEST_PRIME = 2
5
+
6
+ def self.first_n(n, i=LOWEST_PRIME, results=[])
7
+ n = n.to_i
8
+
9
+ if results.length < n
10
+ results << i if prime?(i)
11
+ first_n(n, i+1, results)
12
+ end
13
+ results
14
+ end
15
+
16
+ def self.sieve_up_to(max)
17
+ list = (0..max).to_a
18
+ list[0] = list[1] = nil
19
+
20
+ list.each do |p|
21
+ next unless p
22
+ break if p*p > max
23
+ (p*p).step(max, p) { |m| list[m] = nil }
24
+ end
25
+ list.compact
26
+ end
27
+
28
+ def self.prime?(n)
29
+ sieve_up_to(n.to_i).include?(n.to_i)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ module PrimeTest
2
+
3
+ class ProductMatrix
4
+ attr_reader :list
5
+
6
+ def initialize(list)
7
+ @list = list.map(&:to_i)
8
+ end
9
+
10
+ def map
11
+ rows = [[nil] + list]
12
+ list.each do |i|
13
+ rows << [i] + list.collect {|x| x * i}
14
+ end
15
+ rows
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,37 @@
1
+ module PrimeTest
2
+ class TerminalTable
3
+
4
+ attr_reader :table_data
5
+ def initialize(table_data)
6
+ @table_data = table_data
7
+ validate_data!
8
+ end
9
+
10
+
11
+ def rows
12
+ max_size = table_data.flatten.map(&:to_s).max_by(&:length).size
13
+ r = table_data.map do |row|
14
+ '| ' + row.map {|x| format_cell(x, max_size) }.join(" | ") + ' |'
15
+ end
16
+ blank_row = "_" * r.first.size
17
+
18
+ r.unshift(blank_row)
19
+ r.push(blank_row)
20
+ end
21
+
22
+ private
23
+
24
+ def validate_data!
25
+ rows = table_data.size
26
+ max_cols = table_data.max_by(&:size).size
27
+ min_cols = table_data.min_by(&:size).size
28
+ raise ArgumentError.new("data matrix must have equal row/col length") if rows != max_cols || max_cols != min_cols
29
+ end
30
+
31
+ def format_cell(cell, length)
32
+ # keep width of all cells uniform by formatting to max cell length
33
+
34
+ (" " * (length - cell.to_s.length)) + cell.to_s
35
+ end
36
+ end
37
+ end
data/lib/prime_test.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'prime_test/prime_calculator'
2
+ require 'prime_test/product_matrix'
3
+ require 'prime_test/terminal_table'
4
+
5
+ module PrimeTest
6
+
7
+ def self.print_table(n)
8
+ return if n.to_i == 0
9
+ primes_array = PrimeCalculator.first_n(n)
10
+ matrix = ProductMatrix.new(primes_array).map
11
+
12
+ table = TerminalTable.new(matrix)
13
+ puts ""
14
+ table.rows.each do |row|
15
+ puts row
16
+ end
17
+ return nil
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+ require 'prime_test/prime_calculator'
3
+
4
+ RSpec.describe PrimeTest::PrimeCalculator do
5
+
6
+ describe '::first_n' do
7
+ context 'returns n amount of prime numbers as an array' do
8
+ it 'with 10' do
9
+ expect(described_class.first_n(10)).to eq [2,3,5,7,11,13,17,19,23,29]
10
+ end
11
+
12
+ it 'with 100' do
13
+ expect(described_class.first_n(100)).to eq [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]
14
+ end
15
+
16
+ it 'with nil' do
17
+ expect(described_class.first_n(nil)).to eq []
18
+ end
19
+ end
20
+ end
21
+
22
+ describe '::prime?' do
23
+ it "strings can be converted to numbers" do
24
+ expect(described_class.prime?('11')).to eq true
25
+ end
26
+ it 'blank is false' do
27
+ expect(described_class.prime?('')).to eq false
28
+ end
29
+ it 'nil is false' do
30
+ expect(described_class.prime?(nil)).to eq false
31
+ end
32
+ it '1 is false' do
33
+ expect(described_class.prime?(1)).to eq false
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'prime_test/product_matrix'
3
+
4
+ RSpec.describe PrimeTest::ProductMatrix do
5
+ describe '#map' do
6
+
7
+ it 'accepts numbers and number like strings in an array' do
8
+ result = described_class.new([2,3,'5',7,11,13,'17',19,23,29]).map
9
+
10
+ expect(result).to eq([
11
+ [nil, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29],
12
+ [2, 4, 6, 10, 14, 22, 26, 34, 38, 46, 58],
13
+ [3, 6, 9, 15, 21, 33, 39, 51, 57, 69, 87],
14
+ [5, 10, 15, 25, 35, 55, 65, 85, 95, 115, 145],
15
+ [7, 14, 21, 35, 49, 77, 91, 119, 133, 161, 203],
16
+ [11, 22, 33, 55, 77, 121, 143, 187, 209, 253, 319],
17
+ [13, 26, 39, 65, 91, 143, 169, 221, 247, 299, 377],
18
+ [17, 34, 51, 85, 119, 187, 221, 289, 323, 391, 493],
19
+ [19, 38, 57, 95, 133, 209, 247, 323, 361, 437, 551],
20
+ [23, 46, 69, 115, 161, 253, 299, 391, 437, 529, 667],
21
+ [29, 58, 87, 145, 203, 319, 377, 493, 551, 667, 841]])
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'prime_test/terminal_table'
3
+
4
+ RSpec.describe PrimeTest::TerminalTable do
5
+
6
+ describe '#initialize' do
7
+
8
+ it 'throws error if matrix row size and column size are all the same' do
9
+ data = [
10
+ [1,2],
11
+ [1,2,3],
12
+ [4,5,6]
13
+ ]
14
+ expect{described_class.new(data)}.to raise_error(ArgumentError)
15
+ end
16
+ end
17
+
18
+ describe '#rows' do
19
+ it 'formats matrix as array of strings formatted as row columns with uniform width' do
20
+ data = [
21
+ [12512, 123, 121, 9],
22
+ [6,0, 1235, 12],
23
+ [99, 9999, 1235, 12],
24
+ [98, 6, 12, 2]
25
+ ]
26
+ result = described_class.new(data).rows
27
+ expect(result).to eq([
28
+ "_________________________________",
29
+ "| 12512 | 123 | 121 | 9 |",
30
+ "| 6 | 0 | 1235 | 12 |",
31
+ "| 99 | 9999 | 1235 | 12 |",
32
+ "| 98 | 6 | 12 | 2 |",
33
+ "_________________________________"
34
+ ])
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ require 'prime_test'
3
+
4
+ RSpec.describe PrimeTest do
5
+ it {results = PrimeTest.print_table(10)}
6
+ end
@@ -0,0 +1,102 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+
17
+ require 'pry'
18
+ RSpec.configure do |config|
19
+ # rspec-expectations config goes here. You can use an alternate
20
+ # assertion/expectation library such as wrong or the stdlib/minitest
21
+ # assertions if you prefer.
22
+ config.expect_with :rspec do |expectations|
23
+ # This option will default to `true` in RSpec 4. It makes the `description`
24
+ # and `failure_message` of custom matchers include text for helper methods
25
+ # defined using `chain`, e.g.:
26
+ # be_bigger_than(2).and_smaller_than(4).description
27
+ # # => "be bigger than 2 and smaller than 4"
28
+ # ...rather than:
29
+ # # => "be bigger than 2"
30
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31
+ end
32
+
33
+ # rspec-mocks config goes here. You can use an alternate test double
34
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
35
+ config.mock_with :rspec do |mocks|
36
+ # Prevents you from mocking or stubbing a method that does not exist on
37
+ # a real object. This is generally recommended, and will default to
38
+ # `true` in RSpec 4.
39
+ mocks.verify_partial_doubles = true
40
+ end
41
+
42
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
43
+ # have no way to turn it off -- the option exists only for backwards
44
+ # compatibility in RSpec 3). It causes shared context metadata to be
45
+ # inherited by the metadata hash of host groups and examples, rather than
46
+ # triggering implicit auto-inclusion in groups with matching metadata.
47
+ config.shared_context_metadata_behavior = :apply_to_host_groups
48
+
49
+ # The settings below are suggested to provide a good initial experience
50
+ # with RSpec, but feel free to customize to your heart's content.
51
+ =begin
52
+ # This allows you to limit a spec run to individual examples or groups
53
+ # you care about by tagging them with `:focus` metadata. When nothing
54
+ # is tagged with `:focus`, all examples get run. RSpec also provides
55
+ # aliases for `it`, `describe`, and `context` that include `:focus`
56
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
57
+ config.filter_run_when_matching :focus
58
+
59
+ # Allows RSpec to persist some state between runs in order to support
60
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
61
+ # you configure your source control system to ignore this file.
62
+ config.example_status_persistence_file_path = "spec/examples.txt"
63
+
64
+ # Limits the available syntax to the non-monkey patched syntax that is
65
+ # recommended. For more details, see:
66
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
67
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
68
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
69
+ config.disable_monkey_patching!
70
+
71
+ # This setting enables warnings. It's recommended, but in some cases may
72
+ # be too noisy due to issues in dependencies.
73
+ config.warnings = true
74
+
75
+ # Many RSpec users commonly either run the entire suite or an individual
76
+ # file, and it's useful to allow more verbose output when running an
77
+ # individual spec file.
78
+ if config.files_to_run.one?
79
+ # Use the documentation formatter for detailed output,
80
+ # unless a formatter has already been configured
81
+ # (e.g. via a command-line flag).
82
+ config.default_formatter = "doc"
83
+ end
84
+
85
+ # Print the 10 slowest examples and example groups at the
86
+ # end of the spec run, to help surface which specs are running
87
+ # particularly slow.
88
+ config.profile_examples = 10
89
+
90
+ # Run specs in random order to surface order dependencies. If you find an
91
+ # order dependency and want to debug it, you can fix the order by providing
92
+ # the seed, which is printed after each run.
93
+ # --seed 1234
94
+ config.order = :random
95
+
96
+ # Seed global randomization in this process using the `--seed` CLI option.
97
+ # Setting this allows you to use `--seed` to deterministically reproduce
98
+ # test failures related to randomization by passing the same `--seed` value
99
+ # as the one that triggered the failure.
100
+ Kernel.srand config.seed
101
+ =end
102
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prime_test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Calvin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ description: Print n prime numbers as multiplicative table to terminal
34
+ email: calvjd@gmail.com
35
+ executables:
36
+ - prime_test
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - bin/prime_test
41
+ - lib/prime_test.rb
42
+ - lib/prime_test/prime_calculator.rb
43
+ - lib/prime_test/product_matrix.rb
44
+ - lib/prime_test/terminal_table.rb
45
+ - spec/lib/prime_test/prime_calculator_spec.rb
46
+ - spec/lib/prime_test/product_matrix_spec.rb
47
+ - spec/lib/prime_test/terminal_table_spec.rb
48
+ - spec/lib/prime_test_spec.rb
49
+ - spec/spec_helper.rb
50
+ homepage: http://rubygems.org/gems/jc_prime_test
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.6.14
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Prime Test
74
+ test_files:
75
+ - spec/lib/prime_test/prime_calculator_spec.rb
76
+ - spec/lib/prime_test/product_matrix_spec.rb
77
+ - spec/lib/prime_test/terminal_table_spec.rb
78
+ - spec/lib/prime_test_spec.rb
79
+ - spec/spec_helper.rb