prime_products 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +22 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +5 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +75 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/Rakefile +10 -0
- data/benchmarks/prime_numbers.rb +12 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/prime_products +6 -0
- data/lib/prime_products.rb +30 -0
- data/lib/prime_products/cli.rb +20 -0
- data/lib/prime_products/prime_numbers.rb +40 -0
- data/lib/prime_products/products_table.rb +81 -0
- data/lib/prime_products/version.rb +5 -0
- data/prime_products.gemspec +35 -0
- metadata +191 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c89a59e2546e8634e232c6a6061743a774327d482bb8848f49dfbb9ac2dbf39e
|
4
|
+
data.tar.gz: 995d3b55063f6961309571a108abf290911c55feb6e33b0fa85a909ae6769e1d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3137a492c281a242fa36e46a2fe3f3dda3ce4632ba91ab8e7594292f9a10f52fddbbb50ab25d374d0fce7c972eb942a842b29005f6db706ba49cd58cc7ce6154
|
7
|
+
data.tar.gz: 9fed6a69ea93e510de70f7a08aa18bb8bde65754cb37974367cbf2f04a638fc1840ebfea99b214901ab2c8e59b6d51932a9d09128deafd2dd1514b992ac391a5
|
@@ -0,0 +1,22 @@
|
|
1
|
+
---
|
2
|
+
version: 2
|
3
|
+
jobs:
|
4
|
+
build:
|
5
|
+
working_directory: ~/prime_products
|
6
|
+
docker:
|
7
|
+
- image: circleci/ruby:2.5.3
|
8
|
+
steps:
|
9
|
+
- checkout
|
10
|
+
- run: gem install bundler --pre
|
11
|
+
- type: cache-restore
|
12
|
+
name: Restore Bundler cache
|
13
|
+
key: prime-products-{{ checksum "Gemfile.lock" }}
|
14
|
+
- run: bundle install --path vendor/bundle
|
15
|
+
- type: cache-save
|
16
|
+
name: Save Bundler cache
|
17
|
+
key: prime-products-{{ checksum "Gemfile.lock" }}
|
18
|
+
paths:
|
19
|
+
- vendor/bundle
|
20
|
+
- run: bundle exec rake
|
21
|
+
- run: bundle exec rake lint
|
22
|
+
- run: bundle exec ruby benchmarks/prime_numbers.rb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.3
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at me@timrogers.co.uk. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
prime_products (0.1.0)
|
5
|
+
hanami-cli (~> 0.3.0)
|
6
|
+
immutable-ruby (~> 0.0.3)
|
7
|
+
terminal-table (~> 1.8.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
ast (2.4.0)
|
13
|
+
concurrent-ruby (1.0.5)
|
14
|
+
diff-lcs (1.3)
|
15
|
+
gc_ruboconfig (2.3.13)
|
16
|
+
rubocop (>= 0.57, < 0.61)
|
17
|
+
rubocop-rspec (~> 1.25)
|
18
|
+
hanami-cli (0.3.0)
|
19
|
+
concurrent-ruby (~> 1.0)
|
20
|
+
hanami-utils (~> 1.3)
|
21
|
+
hanami-utils (1.3.0)
|
22
|
+
concurrent-ruby (~> 1.0)
|
23
|
+
transproc (~> 1.0)
|
24
|
+
immutable-ruby (0.0.3)
|
25
|
+
concurrent-ruby (~> 1.0.0)
|
26
|
+
jaro_winkler (1.5.1)
|
27
|
+
parallel (1.12.1)
|
28
|
+
parser (2.5.3.0)
|
29
|
+
ast (~> 2.4.0)
|
30
|
+
powerpack (0.1.2)
|
31
|
+
rainbow (3.0.0)
|
32
|
+
rake (10.5.0)
|
33
|
+
rspec (3.8.0)
|
34
|
+
rspec-core (~> 3.8.0)
|
35
|
+
rspec-expectations (~> 3.8.0)
|
36
|
+
rspec-mocks (~> 3.8.0)
|
37
|
+
rspec-core (3.8.0)
|
38
|
+
rspec-support (~> 3.8.0)
|
39
|
+
rspec-expectations (3.8.2)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.8.0)
|
42
|
+
rspec-mocks (3.8.0)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.8.0)
|
45
|
+
rspec-support (3.8.0)
|
46
|
+
rubocop (0.60.0)
|
47
|
+
jaro_winkler (~> 1.5.1)
|
48
|
+
parallel (~> 1.10)
|
49
|
+
parser (>= 2.5, != 2.5.1.1)
|
50
|
+
powerpack (~> 0.1)
|
51
|
+
rainbow (>= 2.2.2, < 4.0)
|
52
|
+
ruby-progressbar (~> 1.7)
|
53
|
+
unicode-display_width (~> 1.4.0)
|
54
|
+
rubocop-rspec (1.30.1)
|
55
|
+
rubocop (>= 0.60.0)
|
56
|
+
ruby-progressbar (1.10.0)
|
57
|
+
terminal-table (1.8.0)
|
58
|
+
unicode-display_width (~> 1.1, >= 1.1.1)
|
59
|
+
transproc (1.0.2)
|
60
|
+
unicode-display_width (1.4.0)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
ruby
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
bundler (~> 1.17.1)
|
67
|
+
gc_ruboconfig (~> 2.3.13)
|
68
|
+
prime_products!
|
69
|
+
rake (~> 10.0)
|
70
|
+
rspec (~> 3.0)
|
71
|
+
rubocop (~> 0.60.0)
|
72
|
+
rubocop-rspec (~> 1.30.1)
|
73
|
+
|
74
|
+
BUNDLED WITH
|
75
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Tim Rogers
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Prime Products
|
2
|
+
|
3
|
+
Prime Products is a command-line tool which outputs the multiplication table of the first `n` prime numbers.
|
4
|
+
|
5
|
+
So, for instance, where `n=5`, you'd get a result like this:
|
6
|
+
|
7
|
+
```
|
8
|
+
+----+----+----+----+----+-----+
|
9
|
+
| * | 2 | 3 | 5 | 7 | 11 |
|
10
|
+
| 2 | 4 | 6 | 10 | 14 | 22 |
|
11
|
+
| 3 | 6 | 9 | 15 | 21 | 33 |
|
12
|
+
| 5 | 10 | 15 | 25 | 35 | 55 |
|
13
|
+
| 7 | 14 | 21 | 35 | 49 | 77 |
|
14
|
+
| 11 | 22 | 33 | 55 | 77 | 121 |
|
15
|
+
+----+----+----+----+----+-----+
|
16
|
+
```
|
17
|
+
|
18
|
+
The first row and column of the table contains the first 5 primes. Each other cell contains the product of the primes for the corresponding row and column.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Install the gem by running `gem install prime_products`.
|
23
|
+
|
24
|
+
In development, you can install this gem by cloning the repository and running `bundle`.
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
To generate a table of prime products, run the `prime_products` executable, providing the size of the table as the first argument:
|
29
|
+
|
30
|
+
```bash
|
31
|
+
# If you have installed this gem from source, you will need to run
|
32
|
+
# `bundle exec prime_products 10`.
|
33
|
+
prime_products 10
|
34
|
+
```
|
35
|
+
|
36
|
+
If you do not provide an argument, we will use the first 5 by default.
|
37
|
+
|
38
|
+
## Benchmarking
|
39
|
+
|
40
|
+
This gem uses a naive method of finding the first *n* primes which does not scale especially well. However, this seems reasonable given that the primes will be displayed on-screen, and so it is unlikely you will want to see a large number at once.
|
41
|
+
|
42
|
+
Despite being a naive solution, this is still moderately performant. On my machine:
|
43
|
+
|
44
|
+
* finding the first 10 takes ~0.00004s
|
45
|
+
* finding the first 100 takes ~0.001s
|
46
|
+
* finding the first 1000 takes ~0.2s
|
47
|
+
* finding the first 10000 takes ~31s
|
48
|
+
|
49
|
+
It is unlikely that you would want a table of more than 1000, which only takes ~0.2 seconds. If we wanted to use these numbers elsewhere, and needed more, then I would optimise this code.
|
50
|
+
|
51
|
+
You can see full benchmark results, comparing the Ruby standard library's implementation (`Prime.first`) to mine, by running `ruby benchmarks/prime_numbers.rb`.
|
52
|
+
|
53
|
+
## Development
|
54
|
+
|
55
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. Run `rake lint` to check the code style with Rubocop.
|
56
|
+
|
57
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
+
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "benchmark"
|
4
|
+
require "prime"
|
5
|
+
require_relative "../lib/prime_products/prime_numbers"
|
6
|
+
|
7
|
+
Benchmark.bm do |b|
|
8
|
+
[10, 100, 1000, 10_000].each do |n|
|
9
|
+
b.report("naive_#{n}") { PrimeProducts::PrimeNumbers.first(n) }
|
10
|
+
b.report("stdlib_#{n}") { Prime.first(n) }
|
11
|
+
end
|
12
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "prime_products"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/prime_products
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "prime_products/cli"
|
4
|
+
require "prime_products/prime_numbers"
|
5
|
+
require "prime_products/products_table"
|
6
|
+
require "prime_products/version"
|
7
|
+
|
8
|
+
module PrimeProducts
|
9
|
+
# Generates an ASCII "multiplications table" of the first `number_of_primes` prime
|
10
|
+
# numbers, starting from zero, suitable for presentation in a shell.
|
11
|
+
#
|
12
|
+
# For example, if you pass in `5`, the result will be:
|
13
|
+
#
|
14
|
+
# +---+---+----+----+----+----+
|
15
|
+
# | * | 1 | 2 | 3 | 4 | 5 |
|
16
|
+
# | 1 | 1 | 2 | 3 | 4 | 5 |
|
17
|
+
# | 2 | 2 | 4 | 6 | 8 | 10 |
|
18
|
+
# | 3 | 3 | 6 | 9 | 12 | 15 |
|
19
|
+
# | 4 | 4 | 8 | 12 | 16 | 20 |
|
20
|
+
# | 5 | 5 | 10 | 15 | 20 | 25 |
|
21
|
+
# +---+---+----+----+----+----+
|
22
|
+
#
|
23
|
+
# @param numbers [Array<Integer>, Immutable::SortedSet<Integer>] the number of
|
24
|
+
# prime numbers, starting from 0, you want to the multiplication table of
|
25
|
+
# @return [String] the generated table
|
26
|
+
def self.generate_table(number_of_primes:)
|
27
|
+
prime_numbers = PrimeNumbers.first(number_of_primes)
|
28
|
+
ProductsTable.generate(prime_numbers)
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PrimeProducts
|
4
|
+
module CLI
|
5
|
+
DEFAULT_NUMBER_OF_PRIMES = 10
|
6
|
+
|
7
|
+
# Generates a multiplication table of the first N primes (specified by a
|
8
|
+
# command line argument by default) and outputs it to the specified output (`STDOUT
|
9
|
+
# by default).
|
10
|
+
#
|
11
|
+
# @param output [IO] the output to print the table to
|
12
|
+
# @param arguments [Array] the CLI arguments (where the first argument is
|
13
|
+
# expected to contain the number of primes to use, starting from zero)
|
14
|
+
def self.call(output: $stdout, arguments: ARGV)
|
15
|
+
number_of_primes = arguments[0]&.to_i || DEFAULT_NUMBER_OF_PRIMES
|
16
|
+
|
17
|
+
output.puts PrimeProducts.generate_table(number_of_primes: number_of_primes)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "immutable"
|
4
|
+
|
5
|
+
module PrimeProducts
|
6
|
+
module PrimeNumbers
|
7
|
+
INFINITY = (1.0 / 0)
|
8
|
+
|
9
|
+
# Finds the first `n` prime numbers using a naive, but easy to understand, method.
|
10
|
+
# Alternative solutions are easy to find and implement.
|
11
|
+
#
|
12
|
+
# However, I am opting to stick with a simpler more friendly option to keep
|
13
|
+
# complexity low, given that __we will be displaying results on a screen__, making it
|
14
|
+
# unlikely that you will show huge quantities.
|
15
|
+
#
|
16
|
+
# Despite being a naive solution, this is still moderately performant. On my machine:
|
17
|
+
#
|
18
|
+
# * finding the first 10 takes ~0.00004s
|
19
|
+
# * finding the first 100 takes ~0.001s
|
20
|
+
# * finding the first 1000 takes ~0.2s
|
21
|
+
# * finding the first 10000 takes ~31s
|
22
|
+
#
|
23
|
+
# It is unlikely that you would want a table of more than 1000, which only takes
|
24
|
+
# ~0.2 seconds. If we wanted to use these numbers elsewhere, and needed more, then I
|
25
|
+
# would optimise this code.
|
26
|
+
#
|
27
|
+
# @param n [Integer] the number of prime numbers, starting from 0, you want to find
|
28
|
+
# @return [Immutable::SortedSet<Integer>] the first `n` prime numbers
|
29
|
+
def self.first(number_of_primes)
|
30
|
+
# In Ruby 2.6, you can generate a Range up to infinity with `(2..)` 😍
|
31
|
+
primes = (2..INFINITY).
|
32
|
+
lazy.
|
33
|
+
reject { |i| (2...i).any? { |divisor| (i % divisor).zero? } }.
|
34
|
+
take(number_of_primes).
|
35
|
+
to_a
|
36
|
+
|
37
|
+
Immutable::SortedSet[*primes]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "terminal-table"
|
4
|
+
|
5
|
+
module PrimeProducts
|
6
|
+
module ProductsTable
|
7
|
+
class << self
|
8
|
+
# Generates an ASCII "multiplication table" of the supplied `numbers`, suitable
|
9
|
+
# for presentation in a shell.
|
10
|
+
#
|
11
|
+
# For example, if you pass in `[1, 2, 3, 4, 5]`, the result will be:
|
12
|
+
#
|
13
|
+
# +---+---+----+----+----+----+
|
14
|
+
# | * | 1 | 2 | 3 | 4 | 5 |
|
15
|
+
# | 1 | 1 | 2 | 3 | 4 | 5 |
|
16
|
+
# | 2 | 2 | 4 | 6 | 8 | 10 |
|
17
|
+
# | 3 | 3 | 6 | 9 | 12 | 15 |
|
18
|
+
# | 4 | 4 | 8 | 12 | 16 | 20 |
|
19
|
+
# | 5 | 5 | 10 | 15 | 20 | 25 |
|
20
|
+
# +---+---+----+----+----+----+
|
21
|
+
#
|
22
|
+
# @param numbers [Array<Integer>, Immutable::SortedSet<Integer>] A list of numbers
|
23
|
+
# which behaves like an Array, to build a multiplication table of. Using an
|
24
|
+
# immutable list is recommended to reduce the chance of accidental mutations.
|
25
|
+
# @return [String] the generated table
|
26
|
+
def generate(numbers)
|
27
|
+
::Terminal::Table.new(rows: generate_rows(numbers)).to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# Generates the rows for our ASCII "multiplication table", based on a list of
|
33
|
+
# numbers provided
|
34
|
+
#
|
35
|
+
# @param numbers [Array<Integer>, Immutable::SortedSet<Integer>] A list of numbers
|
36
|
+
# which behaves like an Array, to build a multiplication table of.
|
37
|
+
# @return [Array<Array<String>] the complete set of rows for the table
|
38
|
+
def generate_rows(numbers)
|
39
|
+
[
|
40
|
+
generate_header(numbers),
|
41
|
+
*generate_body_rows(numbers),
|
42
|
+
]
|
43
|
+
end
|
44
|
+
|
45
|
+
# Generates the header row for our ASCII "multiplication table", with an asterisk
|
46
|
+
# followed by our numbers
|
47
|
+
#
|
48
|
+
# @param numbers [Array<Integer>, Immutable::SortedSet<Integer>] A list of numbers
|
49
|
+
# which behaves like an Array, to build a multiplication table of.
|
50
|
+
# @return [Array<String>] the header row for the table
|
51
|
+
def generate_header(numbers)
|
52
|
+
["*"] + numbers
|
53
|
+
end
|
54
|
+
|
55
|
+
# Generates the body rows (everything apart from the header) for our ASCII
|
56
|
+
# "multiplication table", based on a list of numbers provided
|
57
|
+
#
|
58
|
+
# @param numbers [Array<Integer>, Immutable::SortedSet<Integer>] A list of numbers
|
59
|
+
# which behaves like an Array, to build a multiplication table of.
|
60
|
+
# @return [Array<Array<String>] the body rows for the table, with the multiplier
|
61
|
+
# followed by the products
|
62
|
+
def generate_body_rows(numbers)
|
63
|
+
numbers.map { |n| generate_body_row(numbers, constant_multiplier: n) }
|
64
|
+
end
|
65
|
+
|
66
|
+
# Generates a body row for our multiplication table, starting with our
|
67
|
+
# `constant_multiplier`, and then the products of each of our `numbers`
|
68
|
+
# and the `constant_multiplier`
|
69
|
+
#
|
70
|
+
# @param numbers [Array<Integer>, Immutable::SortedSet<Integer>] A list of numbers
|
71
|
+
# which behaves like an Array, to build a multiplication table of.
|
72
|
+
# @param constant_multiplier [Integer] the constant multiplier to multiply each of
|
73
|
+
# the `numbers` by
|
74
|
+
# @return [Array<Array<String>] a body row for the table, with the multiplier
|
75
|
+
# followed by the products
|
76
|
+
def generate_body_row(numbers, constant_multiplier:)
|
77
|
+
[constant_multiplier] + numbers.map { |n| n * constant_multiplier }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "prime_products/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "prime_products"
|
9
|
+
spec.version = PrimeProducts::VERSION
|
10
|
+
spec.authors = ["Tim Rogers"]
|
11
|
+
spec.email = ["me@timrogers.co.uk"]
|
12
|
+
|
13
|
+
spec.summary = "Output the multiplication table of the first `n` prime numbers"
|
14
|
+
spec.required_ruby_version = ">= 2.3"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "hanami-cli", "~> 0.3.0"
|
26
|
+
spec.add_dependency "immutable-ruby", "~> 0.0.3"
|
27
|
+
spec.add_dependency "terminal-table", "~> 1.8.0"
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.17.1"
|
30
|
+
spec.add_development_dependency "gc_ruboconfig", "~> 2.3.13"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
spec.add_development_dependency "rubocop", "~> 0.60.0"
|
34
|
+
spec.add_development_dependency "rubocop-rspec", "~> 1.30.1"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prime_products
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Rogers
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hanami-cli
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: immutable-ruby
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.0.3
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.0.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: terminal-table
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.8.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.8.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.17.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.17.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: gc_ruboconfig
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.3.13
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.3.13
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.60.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.60.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop-rspec
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 1.30.1
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 1.30.1
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- me@timrogers.co.uk
|
142
|
+
executables:
|
143
|
+
- prime_products
|
144
|
+
extensions: []
|
145
|
+
extra_rdoc_files: []
|
146
|
+
files:
|
147
|
+
- ".circleci/config.yml"
|
148
|
+
- ".gitignore"
|
149
|
+
- ".rspec"
|
150
|
+
- ".rubocop.yml"
|
151
|
+
- ".ruby-version"
|
152
|
+
- CODE_OF_CONDUCT.md
|
153
|
+
- Gemfile
|
154
|
+
- Gemfile.lock
|
155
|
+
- LICENSE.txt
|
156
|
+
- README.md
|
157
|
+
- Rakefile
|
158
|
+
- benchmarks/prime_numbers.rb
|
159
|
+
- bin/console
|
160
|
+
- bin/setup
|
161
|
+
- exe/prime_products
|
162
|
+
- lib/prime_products.rb
|
163
|
+
- lib/prime_products/cli.rb
|
164
|
+
- lib/prime_products/prime_numbers.rb
|
165
|
+
- lib/prime_products/products_table.rb
|
166
|
+
- lib/prime_products/version.rb
|
167
|
+
- prime_products.gemspec
|
168
|
+
homepage:
|
169
|
+
licenses: []
|
170
|
+
metadata: {}
|
171
|
+
post_install_message:
|
172
|
+
rdoc_options: []
|
173
|
+
require_paths:
|
174
|
+
- lib
|
175
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - ">="
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: '2.3'
|
180
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
181
|
+
requirements:
|
182
|
+
- - ">="
|
183
|
+
- !ruby/object:Gem::Version
|
184
|
+
version: '0'
|
185
|
+
requirements: []
|
186
|
+
rubyforge_project:
|
187
|
+
rubygems_version: 2.7.6
|
188
|
+
signing_key:
|
189
|
+
specification_version: 4
|
190
|
+
summary: Output the multiplication table of the first `n` prime numbers
|
191
|
+
test_files: []
|