cousin_roman 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/cousin_roman.gemspec +24 -0
- data/lib/cousin_roman.rb +3 -0
- data/lib/cousin_roman/conversion.rb +83 -0
- data/lib/cousin_roman/string_extension.rb +11 -0
- data/lib/cousin_roman/version.rb +3 -0
- data/spec/cousin_roman_spec.rb +198 -0
- data/spec/spec_helper.rb +2 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e08d7223e1f6cbac0596e6d4c353926caa690a8
|
4
|
+
data.tar.gz: 101f30339215263b1532cfb542224bd3d9870bf1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4913128c893097b9eb09af1dea51b9816412f3680153168836c90a28600ea9db28bc7caf321a13ba7fe9f806c5978a2473f0168b5b049948c3bd2c80f424de95
|
7
|
+
data.tar.gz: 375f6152b384cafae84d4ae299fff46f9b895c40ad59559981922e64f15e5073c0679a10572d71be784d1883879febc7bcdea22e3674e885da6493ff7604cfb7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Artem Pyanykh
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# CousinRoman
|
2
|
+
|
3
|
+
`CousinRoman` is a `String` extension that allows you to convert
|
4
|
+
string representation of Roman numbers
|
5
|
+
(following modern subtractive notation)
|
6
|
+
to Arabic numbers (i.e. Integers).
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'cousin_roman'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install cousin_roman
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Just use `#to_arabic` or `#to_arabic!` method.
|
25
|
+
|
26
|
+
For example:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
'MMXIII'.to_arabic # => 2013
|
30
|
+
'MMYUOX'.to_arabic # => nil
|
31
|
+
'MMYOUX'.to_arabic! # => TypeError: not a valid roman number
|
32
|
+
```
|
33
|
+
|
34
|
+
## Testing
|
35
|
+
|
36
|
+
Just run
|
37
|
+
|
38
|
+
$ rake
|
39
|
+
|
40
|
+
under gem folder.
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cousin_roman/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cousin_roman"
|
8
|
+
spec.version = CousinRoman::VERSION
|
9
|
+
spec.authors = ["Artem Pyanykh"]
|
10
|
+
spec.email = ["artem.pyanykh@gmail.com"]
|
11
|
+
spec.description = %q{String extension adding capaibility to convert Roman number to Integers.}
|
12
|
+
spec.summary = %q{Conversion from Roman numbers to Arabic ones.}
|
13
|
+
spec.homepage = "https://github.com/ArtemPyanykh/cousin_roman"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/cousin_roman.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
module CousinRoman
|
2
|
+
LITERALS = [
|
3
|
+
'i', 'I',
|
4
|
+
'v', 'V',
|
5
|
+
'x', 'X',
|
6
|
+
'l', 'L',
|
7
|
+
'c', 'C',
|
8
|
+
'd', 'D',
|
9
|
+
'm', 'M'
|
10
|
+
]
|
11
|
+
|
12
|
+
ONES = {
|
13
|
+
'i' => 1,
|
14
|
+
'x' => 10,
|
15
|
+
'c' => 100,
|
16
|
+
'm' => 1000
|
17
|
+
}
|
18
|
+
|
19
|
+
FIVES = {
|
20
|
+
'v' => 5,
|
21
|
+
'l' => 50,
|
22
|
+
'd' => 500,
|
23
|
+
}
|
24
|
+
|
25
|
+
SUBTRACTIVE = {
|
26
|
+
'iv' => 4,
|
27
|
+
'ix' => 9,
|
28
|
+
'xl' => 40,
|
29
|
+
'xc' => 90,
|
30
|
+
'cd' => 400,
|
31
|
+
'cm' => 900,
|
32
|
+
}
|
33
|
+
|
34
|
+
FACTORS = ONES.merge(FIVES).merge(SUBTRACTIVE)
|
35
|
+
|
36
|
+
class << self
|
37
|
+
def valid?(number)
|
38
|
+
matches = number.match roman_regex
|
39
|
+
matches and !matches[0].empty?
|
40
|
+
end
|
41
|
+
|
42
|
+
# Convert Roman +number+ represented as a string to an Integer.
|
43
|
+
#
|
44
|
+
# It is essential that +numer+ is a valid Roman numeral.
|
45
|
+
#
|
46
|
+
# The steps are:
|
47
|
+
#
|
48
|
+
# 1. Replace factors with their numeric values
|
49
|
+
# starting from subtractive factors (cause they are compound).
|
50
|
+
# 2. Sum this numeric values to get the final answer
|
51
|
+
def convert(number)
|
52
|
+
intermediate = number.dup.downcase
|
53
|
+
SUBTRACTIVE.each do |factor, value|
|
54
|
+
intermediate.gsub!(factor, "(#{value})")
|
55
|
+
end
|
56
|
+
ONES.merge(FIVES).each do |factor, value|
|
57
|
+
intermediate.gsub!(factor, "(#{value})")
|
58
|
+
end
|
59
|
+
|
60
|
+
intermediate.scan(/\((\d*)\)/).reduce(0) do |sum, term|
|
61
|
+
sum + term.first.to_i
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_arabic(number)
|
66
|
+
clean = number.strip
|
67
|
+
convert clean if valid? clean
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_arabic!(number)
|
71
|
+
clean = number.strip
|
72
|
+
if valid? clean
|
73
|
+
convert clean
|
74
|
+
else
|
75
|
+
raise TypeError, 'not a valid roman number'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def roman_regex
|
80
|
+
/^(M{0,3})(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$/i
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CousinRoman do
|
4
|
+
it 'should have a version number' do
|
5
|
+
CousinRoman::VERSION.should_not be_nil
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'valid?' do
|
9
|
+
describe 'literals validation' do
|
10
|
+
ASCII = 0.upto(127).map(&:chr)
|
11
|
+
NON_ROMAN_LITERALS = ASCII - CousinRoman::LITERALS
|
12
|
+
|
13
|
+
CousinRoman::LITERALS.each do |l|
|
14
|
+
it "should allow #{l}" do
|
15
|
+
CousinRoman.valid?(l).should be_true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
NON_ROMAN_LITERALS.each do |l|
|
20
|
+
it "should not allow #{l}" do
|
21
|
+
CousinRoman.valid?(l).should be_false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should not allow mix of allowed and disallowed literals' do
|
26
|
+
CousinRoman.valid?('MYCXI').should be_false
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should not allow empty string' do
|
30
|
+
CousinRoman.valid?('').should be_false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'factors validation' do
|
35
|
+
CousinRoman::ONES.keys.each do |factor|
|
36
|
+
1.upto(3).each do |reps|
|
37
|
+
it "should allow #{reps} repetitions of #{factor}" do
|
38
|
+
CousinRoman.valid?(factor*reps).should be_true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not allow more than 3 repetitions of #{factor}" do
|
43
|
+
CousinRoman.valid?(factor*4).should be_false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
CousinRoman::FIVES.merge(CousinRoman::SUBTRACTIVE).keys.each do |factor|
|
48
|
+
it "should not allow repetition of #{factor}" do
|
49
|
+
CousinRoman.valid?(factor*2).should be_false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should allow ones and fives of the same power' do
|
54
|
+
CousinRoman::FIVES.keys.each_with_index do |five, index|
|
55
|
+
1.upto(3).each do |reps|
|
56
|
+
corresponding_one = CousinRoman::ONES.keys[index]
|
57
|
+
CousinRoman.valid?(five + (corresponding_one * reps)).should be_true
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should not allow subtractives and ones or fives of the same power' do
|
63
|
+
CousinRoman.valid?('ivi').should be_false
|
64
|
+
CousinRoman.valid?('ixi').should be_false
|
65
|
+
CousinRoman.valid?('xlx').should be_false
|
66
|
+
CousinRoman.valid?('xcx').should be_false
|
67
|
+
CousinRoman.valid?('cdc').should be_false
|
68
|
+
CousinRoman.valid?('cmc').should be_false
|
69
|
+
|
70
|
+
CousinRoman.valid?('ivv').should be_false
|
71
|
+
CousinRoman.valid?('ixv').should be_false
|
72
|
+
CousinRoman.valid?('xll').should be_false
|
73
|
+
CousinRoman.valid?('xcl').should be_false
|
74
|
+
CousinRoman.valid?('cdd').should be_false
|
75
|
+
CousinRoman.valid?('cmd').should be_false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe 'ordering validation' do
|
80
|
+
context 'when validating that factors are ordered by descreasing of power' do
|
81
|
+
ORDERINGS_RIGHT = [
|
82
|
+
'md', 'mc',
|
83
|
+
'cl', 'cx',
|
84
|
+
'xv', 'xi',
|
85
|
+
'mcm', 'mcd',
|
86
|
+
'cxc', 'cxl',
|
87
|
+
'xix', 'xiv'
|
88
|
+
]
|
89
|
+
ORDERINGS_WRONG = [
|
90
|
+
'dm', 'lc', 'vx',
|
91
|
+
'cmm', 'cdm',
|
92
|
+
'xcc', 'xlc',
|
93
|
+
'ixx', 'ivx'
|
94
|
+
]
|
95
|
+
|
96
|
+
ORDERINGS_RIGHT.each do |pair|
|
97
|
+
high, low = pair.split('', 2)
|
98
|
+
it "should allow #{high}#{low}" do
|
99
|
+
CousinRoman.valid?(high + low).should be_true
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
ORDERINGS_WRONG.each do |pair|
|
104
|
+
low, high = pair.split('', 2)
|
105
|
+
it "should not allow #{low}#{high}" do
|
106
|
+
CousinRoman.valid?(low + high).should be_false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe 'convert' do
|
114
|
+
context 'singular factors' do
|
115
|
+
CousinRoman::FACTORS.each do |factor, number|
|
116
|
+
it "should convert #{factor} to #{number}" do
|
117
|
+
CousinRoman.convert(factor).should == number
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'repetitive factors' do
|
123
|
+
CousinRoman::ONES.each do |factor, number|
|
124
|
+
1.upto(3).each do |reps|
|
125
|
+
it "should convert #{factor*reps} to #{number*reps}" do
|
126
|
+
CousinRoman.convert(factor*reps).should == (number*reps)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context 'different power factors' do
|
133
|
+
it 'should honor ones' do
|
134
|
+
CousinRoman.convert('mcxi').should == 1111
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'should honor fives' do
|
138
|
+
CousinRoman.convert('dlv').should == 555
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'should honor ones and fives together' do
|
142
|
+
CousinRoman.convert('mdclxvi').should == 1666
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'should honor subtractives ones and fives together' do
|
146
|
+
CousinRoman.convert('mcmlxxxix').should == 1989
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'should honor max number' do
|
150
|
+
CousinRoman.convert('mmmcmxcix').should == 3999
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should honor the longest number' do
|
154
|
+
CousinRoman.convert('dccclxxxviii').should == 888
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'should be case insensitive' do
|
158
|
+
number = 'MCXI'
|
159
|
+
CousinRoman.convert(number).should == CousinRoman.convert(number.downcase)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe 'to_arabic' do
|
165
|
+
it 'should call convert on valid number' do
|
166
|
+
CousinRoman.should_receive(:convert).and_call_original
|
167
|
+
CousinRoman.to_arabic('MMM').should == 3000
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'should return nil on invalid number' do
|
171
|
+
CousinRoman.to_arabic('YRU?!').should be_nil
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
describe 'to_arabic!' do
|
176
|
+
it 'should call convert on valid number' do
|
177
|
+
CousinRoman.should_receive(:convert).and_call_original
|
178
|
+
CousinRoman.to_arabic!('MMM').should == 3000
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'should throw TypeError on invalid number' do
|
182
|
+
expect { CousinRoman.to_arabic!('YRU?!') }.to raise_error(TypeError)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
end
|
187
|
+
|
188
|
+
describe String do
|
189
|
+
[:to_arabic, :to_arabic!].each do |meth|
|
190
|
+
it { should respond_to(meth) }
|
191
|
+
|
192
|
+
it "should call CousinRoman.#{meth} on String##{meth}" do
|
193
|
+
roman = 'i'
|
194
|
+
CousinRoman.should_receive(meth).with(roman).and_call_original
|
195
|
+
roman.send(meth).should == 1
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cousin_roman
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Artem Pyanykh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: String extension adding capaibility to convert Roman number to Integers.
|
56
|
+
email:
|
57
|
+
- artem.pyanykh@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- cousin_roman.gemspec
|
70
|
+
- lib/cousin_roman.rb
|
71
|
+
- lib/cousin_roman/conversion.rb
|
72
|
+
- lib/cousin_roman/string_extension.rb
|
73
|
+
- lib/cousin_roman/version.rb
|
74
|
+
- spec/cousin_roman_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
homepage: https://github.com/ArtemPyanykh/cousin_roman
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.1.8
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Conversion from Roman numbers to Arabic ones.
|
100
|
+
test_files:
|
101
|
+
- spec/cousin_roman_spec.rb
|
102
|
+
- spec/spec_helper.rb
|