HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

Prime multiplication table

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
primetablemultiplication

Problem

I am new to Ruby (1 year as hobby project on weekends) and solved a code challenge and it was not as good as they wanted. I want to know how i could make it better and how to make the structure better. In summary, how would a mid or senior ruby developer do this challenge?

This is the task:


Write a program that prints a multiplication table of primes numbers.
The program should take an argument from the command line that
specifies the amount of prime numbers to generate and print out a
multiplication table for these prime numbers.


An example of the way the application may run:


executable_script_name --­­count 10


An example of the output (using the terminal­table gem):

| 2 3 5 7 11 13 17 19 23 29

­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

2 | 4 6 10 14 22 26 34 38 46 58

3 | 6 9 15 21 33 39 51 57 69 87

5 | 10 15 25 35 55 65 85 95 115 145

7 | 14 21 35 49 77 91 119 133 161 203

11 | 22 33 55 77 121 143 187 209 253 319

13 | 26 39 65 91 143 169 221 247 299 377

17 | 34 51 85 119 187 221 289 323 391 493

19 | 38 57 95 133 209 247 323 361 437 551

23 | 46 69 115 161 253 299 391 437 529 667

29 | 58 87 145 203 319 377 493 551 667 841




Notes



  • Consider code readability/complexity



  • Consider SOLID principles, but do not over­engineer



  • Consider extensibility



  • Feel free to use any library or gem in both implementation and tests, but please write your own code for the prime number generator.



  • Consider how you can prove the correctness of your application



  • Write it in Ruby




Below starts my code challenge:

```
require 'rubygems'
require 'bundler/setup'

require 'optparse'
require './calculate_prime'

# Just parsing the options here and
# initialize the Calculater to present
# the table of results.

options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: prime_multiply.rb [options]'

opts.on('-v', '--count N', Integer, 'Select amount of primes') do |v|
options[:count] = v
end
end.pa

Solution

Some notes on your code:

  • OptionParser is a good choice because it's in the stdlib, but it's pretty cumbersome to use. I'd recommend some other library (i.e: trollop)



  • There are hundreds of answers in SO and CR about primes generation, so I am not going to add more noise to it, others users can help you there. For this, I'll use the prime library.



  • def multiply_by(x). For such a simply operation, you just write the map where you need it, this does not add any useful abstraction IMO.



  • This kind of problems ask for functional solutions. No each, push, shift and so on.



  • table = some_expr and then table. That's not necessary, just return some_expr.



I'd write:

require 'prime'
require 'trollop'
require 'terminal-table'

module PrettyPrimes
  def self.multiplication_table(size)
    primes = Prime.first(size)
    header_rows = [[nil] + primes, :separator]
    product_rows = primes.map { |p1| [p1] + primes.map { |p2| p1 * p2 } }
    Terminal::Table.new(rows: header_rows + product_rows)
  end
end

if __FILE__ == $0
  opts = Trollop::options do
    opt(:count, "Select amount of primes", type: :integer, default: 10) 
  end
  $stdout.puts(PrettyPrimes.multiplication_table(opts[:count]))
end


Output:

$ ruby primes_multiplication_table.rb --count=5
+----+----+----+----+----+-----+
|    | 2  | 3  | 5  | 7  | 11  |
+----+----+----+----+----+-----+
| 2  | 4  | 6  | 10 | 14 | 22  |
| 3  | 6  | 9  | 15 | 21 | 33  |
| 5  | 10 | 15 | 25 | 35 | 55  |
| 7  | 14 | 21 | 35 | 49 | 77  |
| 11 | 22 | 33 | 55 | 77 | 121 |
+----+----+----+----+----+-----+

Code Snippets

require 'prime'
require 'trollop'
require 'terminal-table'

module PrettyPrimes
  def self.multiplication_table(size)
    primes = Prime.first(size)
    header_rows = [[nil] + primes, :separator]
    product_rows = primes.map { |p1| [p1] + primes.map { |p2| p1 * p2 } }
    Terminal::Table.new(rows: header_rows + product_rows)
  end
end

if __FILE__ == $0
  opts = Trollop::options do
    opt(:count, "Select amount of primes", type: :integer, default: 10) 
  end
  $stdout.puts(PrettyPrimes.multiplication_table(opts[:count]))
end
$ ruby primes_multiplication_table.rb --count=5
+----+----+----+----+----+-----+
|    | 2  | 3  | 5  | 7  | 11  |
+----+----+----+----+----+-----+
| 2  | 4  | 6  | 10 | 14 | 22  |
| 3  | 6  | 9  | 15 | 21 | 33  |
| 5  | 10 | 15 | 25 | 35 | 55  |
| 7  | 14 | 21 | 35 | 49 | 77  |
| 11 | 22 | 33 | 55 | 77 | 121 |
+----+----+----+----+----+-----+

Context

StackExchange Code Review Q#122278, answer score: 2

Revisions (0)

No revisions yet.