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 +4 -4
- data/.ruby-version +1 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +3 -2
- data/README.md +18 -0
- data/lib/romanumerals/numeric.rb +2 -7
- data/lib/romanumerals/string.rb +56 -0
- data/lib/romanumerals/version.rb +1 -1
- data/lib/romanumerals.rb +1 -0
- data/sig/romanumerals/numeric.rbs +9 -0
- data/sig/romanumerals/string.rbs +13 -0
- data/sig/romanumerals.rbs +1 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce9a2c835b1abd6e4aede7c8aad10ca02abb19532da0c6389e6207deffacf96f
|
4
|
+
data.tar.gz: 8ca0e19c63694bb28ee450d88e64e9578d2a97a88a4f71b275bf35df45c93fcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6eb90ef45faf6f20cfd82c9cbc5f73139dd161f9bac6be11f85b30a41463a54c547dae130f3ae716e2bc9083ec9b4195f9ab34bcfbed9a3868dede773a70bb4a
|
7
|
+
data.tar.gz: b292909557427f0ab63deca96206de50cea434b63dc106590be8711e18c3d2fce03608013affe0b5d8d4e6611fa3c9f3b7e0c45c3107f23a7c2ecd85b589f7e9
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.2
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
romanumerals (0.1
|
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.
|
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.
|
data/lib/romanumerals/numeric.rb
CHANGED
@@ -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 =
|
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
|
data/lib/romanumerals/version.rb
CHANGED
data/lib/romanumerals.rb
CHANGED
data/sig/romanumerals.rbs
CHANGED
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
|
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-
|
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
|