romanumerals 0.1.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5ac06e5eb0a28e7a395360b07beec8393ad2b830f8cd96ef9de735846f8d7fd
4
- data.tar.gz: 0b2947002ff049ea538473d520df45455b2c69d3e5d309ec75a3cde1e801f993
3
+ metadata.gz: ce9a2c835b1abd6e4aede7c8aad10ca02abb19532da0c6389e6207deffacf96f
4
+ data.tar.gz: 8ca0e19c63694bb28ee450d88e64e9578d2a97a88a4f71b275bf35df45c93fcf
5
5
  SHA512:
6
- metadata.gz: fbc5e0a96c43aadd92300f11d3a7b397cd009d1d23b9857ec75cd73fbf7435092c0dec61c2cd19c24d311ddeef7fdf3f18c5b921539feaec1c9d27f97997cd73
7
- data.tar.gz: ae3519940702d8b3b367fabbf78d336a8d297a7e5ee06854c0a35d84d0378454eecac6525f16ba454b16cfe68fbe092cdd88f6e80def4dc830bfcd429777b490
6
+ metadata.gz: 6eb90ef45faf6f20cfd82c9cbc5f73139dd161f9bac6be11f85b30a41463a54c547dae130f3ae716e2bc9083ec9b4195f9ab34bcfbed9a3868dede773a70bb4a
7
+ data.tar.gz: b292909557427f0ab63deca96206de50cea434b63dc106590be8711e18c3d2fce03608013affe0b5d8d4e6611fa3c9f3b7e0c45c3107f23a7c2ecd85b589f7e9
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
data/Gemfile CHANGED
@@ -4,7 +4,7 @@ source 'https://rubygems.org'
4
4
 
5
5
  gemspec
6
6
 
7
+ gem 'codecov', require: false, group: :test
7
8
  gem 'rake', '~> 13.0'
8
9
  gem 'rspec', '~> 3.0'
9
10
  gem 'rubocop', '~> 1.21'
10
- gem 'codecov', require: false, group: :test
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- romanumerals (0.1.0)
4
+ romanumerals (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -53,6 +53,7 @@ GEM
53
53
 
54
54
  PLATFORMS
55
55
  x86_64-darwin-20
56
+ x86_64-darwin-21
56
57
  x86_64-linux
57
58
 
58
59
  DEPENDENCIES
@@ -63,4 +64,4 @@ DEPENDENCIES
63
64
  rubocop (~> 1.21)
64
65
 
65
66
  BUNDLED WITH
66
- 2.3.12
67
+ 2.3.14
data/README.md CHANGED
@@ -18,6 +18,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
18
18
 
19
19
  Usage is very simple:
20
20
 
21
+ #### Convert to roman:
21
22
  ```ruby
22
23
  10.to_roman # => "X"
23
24
  110.to_roman # => "CX"
@@ -27,6 +28,23 @@ Usage is very simple:
27
28
  2022.to_roman # => "MMXXII"
28
29
  ```
29
30
 
31
+ #### Convert from roman:
32
+ ```ruby
33
+ 'MMXXII'.to_numeral # => 2022
34
+ ```
35
+
36
+ #### Other cases:
37
+ ```ruby
38
+ 0.to_roman # => ""
39
+ 11.9.to_roman # => "XI"
40
+
41
+ # If a string does not contain roman numerals then the String#to_i will be used
42
+ ''.to_numeral # => 0
43
+ '0'.to_numeral # => 0
44
+ 'XIIi'.to_numeral # => 0
45
+ '5 contributors'.to_numeral # => 5
46
+ ```
47
+
30
48
  ## Contributing
31
49
 
32
50
  Bug reports and pull requests are welcome on GitHub at https://github.com/febonazzic/romanumerals.
@@ -6,19 +6,14 @@ module Romanumerals
6
6
  return '' if zero?
7
7
 
8
8
  decompose.each_with_object([]) do |(divider, count), result|
9
- result <<
10
- if count == 4
11
- DICTIONARY[divider] + DICTIONARY[divider * 5]
12
- else
13
- DICTIONARY[divider] * count
14
- end
9
+ result << DICTIONARY[divider] * count
15
10
  end.join
16
11
  end
17
12
 
18
13
  private
19
14
 
20
15
  def decompose
21
- number = self
16
+ number = dup
22
17
 
23
18
  DICTIONARY.keys.each_with_object({}) do |divider, result|
24
19
  dividers_count = number / divider
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Romanumerals
4
+ DICTIONARY_SORTED_BY_WEIGHT = {
5
+ 'CM' => 900,
6
+ 'CD' => 400,
7
+ 'XC' => 90,
8
+ 'XL' => 40,
9
+ 'IX' => 9,
10
+ 'IV' => 4,
11
+ 'M' => 1000,
12
+ 'D' => 500,
13
+ 'C' => 100,
14
+ 'L' => 50,
15
+ 'X' => 10,
16
+ 'V' => 5,
17
+ 'I' => 1
18
+ }.freeze
19
+
20
+ module String
21
+ def to_numeral
22
+ return to_i unless roman?
23
+
24
+ decompose.map { |value, count| DICTIONARY_SORTED_BY_WEIGHT[value] * count }.sum
25
+ end
26
+
27
+ def to_roman
28
+ to_i.to_roman
29
+ end
30
+
31
+ private
32
+
33
+ def roman?
34
+ match?(/^[IVXMCDL]*$/)
35
+ end
36
+
37
+ def decompose
38
+ initial_string = dup
39
+
40
+ DICTIONARY_SORTED_BY_WEIGHT.keys.each_with_object({}) do |value, result|
41
+ matches_count = initial_string.scan(/#{value}/).size
42
+ result[value] ||= 0
43
+
44
+ matches_count.times do
45
+ initial_string.sub!(/#{value}/, '')
46
+
47
+ result[value] += 1
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ class String
55
+ include Romanumerals::String
56
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Romanumerals
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.1'
5
5
  end
data/lib/romanumerals.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'romanumerals/version'
4
4
  require_relative 'romanumerals/numeric'
5
+ require_relative 'romanumerals/string'
5
6
 
6
7
  module Romanumerals
7
8
  class Error < StandardError; end
@@ -0,0 +1,9 @@
1
+ module Romanumerals
2
+ module Numeric
3
+ def to_roman: -> String
4
+
5
+ private
6
+
7
+ def decompose: -> Hash[Integer, Integer]
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module Romanumerals
2
+ DICTIONARY_SORTED_BY_WEIGHT: Hash[String, Integer]
3
+
4
+ module String
5
+ def to_numeral: -> Integer
6
+ def to_roman: -> ::String
7
+
8
+ private
9
+
10
+ def roman?: -> bool
11
+ def decompose: -> Hash[::String, Integer]
12
+ end
13
+ end
data/sig/romanumerals.rbs CHANGED
@@ -1,3 +1,4 @@
1
1
  module Romanumerals
2
2
  VERSION: String
3
+ DICTIONARY: Hash[Integer, String]
3
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: romanumerals
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - febonazzic
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-30 00:00:00.000000000 Z
11
+ date: 2022-06-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -19,6 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - ".rspec"
21
21
  - ".rubocop.yml"
22
+ - ".ruby-version"
22
23
  - CHANGELOG.md
23
24
  - Gemfile
24
25
  - Gemfile.lock
@@ -27,8 +28,11 @@ files:
27
28
  - Rakefile
28
29
  - lib/romanumerals.rb
29
30
  - lib/romanumerals/numeric.rb
31
+ - lib/romanumerals/string.rb
30
32
  - lib/romanumerals/version.rb
31
33
  - sig/romanumerals.rbs
34
+ - sig/romanumerals/numeric.rbs
35
+ - sig/romanumerals/string.rbs
32
36
  homepage: https://github.com/febonazzic/romanumerals
33
37
  licenses:
34
38
  - MIT