romanumerals 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c6316fd1d20b09b718a0baa50104cae0d94a18496bc8e745825581c868afd88
4
- data.tar.gz: fa0ceb0e8b1547edc94d28325e8d4c68dfc13dfdcb68a7302d536aeb620794f9
3
+ metadata.gz: 89ee3edbf76f972387873e7c65abb886ffacfc0c20f4c5cd761dcbfbea6cd4c0
4
+ data.tar.gz: b92715147ea27d486218dba49c450b4dc7697f1c2c38c87f9451a6aa44133401
5
5
  SHA512:
6
- metadata.gz: f62649057cd5cbd19708294ec2b63c37712728ec74f5254ce1e3cc80081a173d405779b8db796f400483a1d41a0f9a8b6ab17ed42dfe6647579042e7d67f50b9
7
- data.tar.gz: 37321963dcf7911e33c819269447eb05b9825be5a94a7278f465a4a1e163b7c2acb5919b85bdf81ea8db41eabee35323fef6363c7f1a21b304a32dee5f5fab83
6
+ metadata.gz: edeb84a0cc156f64f236b13b91e7fac471c6b5064f0be2a715fadb941aa38138bbf2599ccb6239fa40fcc1ec7d371989806f6abcec2f3651a19f5560feab0c5a
7
+ data.tar.gz: 762a9db1bff5156091188d631fff66123f3ce411b6bc8077c4e5e1f49095d4b06722d23d08e739d927f648ce16f0f70169caf449f8ab7740494115835c07f553
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.1)
4
+ romanumerals (0.1.2)
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
@@ -18,7 +18,7 @@ module Romanumerals
18
18
  private
19
19
 
20
20
  def decompose
21
- number = self
21
+ number = dup
22
22
 
23
23
  DICTIONARY.keys.each_with_object({}) do |divider, result|
24
24
  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.1'
4
+ VERSION = '0.2.0'
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,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.1
4
+ version: 0.2.0
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,9 +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
32
34
  - sig/romanumerals/numeric.rbs
35
+ - sig/romanumerals/string.rbs
33
36
  homepage: https://github.com/febonazzic/romanumerals
34
37
  licenses:
35
38
  - MIT