roman_numbers 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +47 -1
- data/lib/roman_numbers/version.rb +1 -1
- data/roman_numbers.gemspec +1 -0
- data/spec/helpers.rb +28 -0
- data/spec/roman_numbers/roman_number_spec.rb +60 -216
- data/spec/spec_helper.rb +1 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27973d5bbc313c37e4508cdeadcbfa53b0b667cc
|
4
|
+
data.tar.gz: 5d225eb7a22504978a130c4a1f80a494fa4b909b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afcdcbb98fb0304c6faabd6f553dfa645c38c52805d29fc435104862b25f70495ebdb42e949cd701935559844b7687d9080991dc09a1952ca2bc0c81451d029d
|
7
|
+
data.tar.gz: 37ba09d44403228e0e421fc9df56dccb8fca78a82942f64efa4e2a3b22e149841447ad8c9b1894fd2ae96bfa6c328c419a0a2565dda677c708f5a3c85c10b776
|
data/README.md
CHANGED
@@ -2,6 +2,33 @@
|
|
2
2
|
|
3
3
|
Conversion between Integers and Roman Numbers
|
4
4
|
|
5
|
+
Roman numerals are based on seven symbols:
|
6
|
+
|
7
|
+
| Symbol | Value |
|
8
|
+
|:----------- |:--------- |
|
9
|
+
| I | 1 |
|
10
|
+
| V | 5 |
|
11
|
+
| X | 10 |
|
12
|
+
| L | 50 |
|
13
|
+
| C | 100 |
|
14
|
+
| D | 500 |
|
15
|
+
| M | 1000 |
|
16
|
+
|
17
|
+
|
18
|
+
Numbers are formed by combining symbols together and adding the values. For example, MMVI is 1000 + 1000 + 5 + 1 = 2006.
|
19
|
+
|
20
|
+
Generally, symbols are placed in order of value, starting with the largest values. When smaller values precede larger values, the smaller values are subtracted from the larger values, and the result is added to the total. For example MCMXLIV = 1000 + (1000 − 100) + (50 − 10) + (5 − 1) = 1944.
|
21
|
+
|
22
|
+
* The symbols "I", "X", "C", and "M" can be repeated three times in succession, but no more. (They may appear four times if the third and fourth are separated by a smaller value, such as XXXIX.) "D", "L", and "V" can never be repeated.
|
23
|
+
|
24
|
+
* "I" can be subtracted from "V" and "X" only. "X" can be subtracted from "L" and "C" only. "C" can be subtracted from "D" and "M" only. "V", "L", and "D" can never be subtracted.
|
25
|
+
|
26
|
+
* Only one small-value symbol may be subtracted from any large-value symbol.
|
27
|
+
|
28
|
+
* A number written in Arabic numerals can be broken into digits. For example, 1903 is composed of 1, 9, 0, and 3. To write the Roman numeral, each of the non-zero digits should be treated separately. In the above example, 1,000 = M, 900 = CM, and 3 = III. Therefore, 1903 = MCMIII.
|
29
|
+
|
30
|
+
Refer: http://en.wikipedia.org/wiki/Roman_numerals
|
31
|
+
|
5
32
|
## Installation
|
6
33
|
|
7
34
|
Add this line to your application's Gemfile:
|
@@ -18,26 +45,45 @@ Or install it yourself as:
|
|
18
45
|
|
19
46
|
## Usage
|
20
47
|
|
21
|
-
|
48
|
+
By explicitly using RomanNumber class:
|
49
|
+
|
50
|
+
|
51
|
+
> require 'roman_numbers'
|
52
|
+
|
22
53
|
|
23
54
|
> RomanNumbers::RomanNumber.new(3999).convert_decimal_to_roman
|
55
|
+
|
24
56
|
"MMMCMXCIX"
|
25
57
|
|
58
|
+
|
26
59
|
> RomanNumbers::RomanNumber.new('MMMCMXCIX').convert_roman_to_decimal
|
60
|
+
|
27
61
|
3999
|
28
62
|
|
63
|
+
|
64
|
+
By directly on String and Integer objects:
|
65
|
+
|
66
|
+
|
29
67
|
> 'MCMXLIV'.from_roman_to_integer
|
68
|
+
|
30
69
|
1944
|
31
70
|
|
71
|
+
|
32
72
|
> 1944.to_roman
|
73
|
+
|
33
74
|
"MCMXLIV"
|
34
75
|
|
76
|
+
|
35
77
|
> 'MMMMMX'.from_roman_to_integer
|
78
|
+
|
36
79
|
RomanNumbers::InvalidInputError
|
37
80
|
|
81
|
+
|
38
82
|
> 999383.to_roman
|
83
|
+
|
39
84
|
RomanNumbers::InvalidInputError
|
40
85
|
|
86
|
+
|
41
87
|
## Contributing
|
42
88
|
|
43
89
|
1. Fork it
|
data/roman_numbers.gemspec
CHANGED
data/spec/helpers.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Helpers
|
2
|
+
def self.valid_inputs
|
3
|
+
[
|
4
|
+
[1, 'I'],
|
5
|
+
[5, 'V'],
|
6
|
+
[10, 'X'],
|
7
|
+
[50, 'L'],
|
8
|
+
[100, 'C'],
|
9
|
+
[500, 'D'],
|
10
|
+
[1000, 'M'],
|
11
|
+
[4, 'IV'],
|
12
|
+
[9, 'IX'],
|
13
|
+
[40, 'XL'],
|
14
|
+
[90, 'XC'],
|
15
|
+
[400, 'CD'],
|
16
|
+
[900, 'CM'],
|
17
|
+
[207, 'CCVII'],
|
18
|
+
[1066, 'MLXVI'],
|
19
|
+
[44, 'XLIV'],
|
20
|
+
[49, 'XLIX'],
|
21
|
+
[904, 'CMIV'],
|
22
|
+
[944, 'CMXLIV'],
|
23
|
+
[1904, 'MCMIV'],
|
24
|
+
[1944, 'MCMXLIV'],
|
25
|
+
[3999, 'MMMCMXCIX'],
|
26
|
+
]
|
27
|
+
end
|
28
|
+
end
|
@@ -2,35 +2,34 @@ require_relative '../spec_helper'
|
|
2
2
|
|
3
3
|
module RomanNumbers
|
4
4
|
describe RomanNumber do # EXAMPLE GROUP
|
5
|
+
|
5
6
|
before(:each) do
|
6
7
|
@roman_number = RomanNumber.new(1)
|
7
8
|
end
|
9
|
+
|
8
10
|
describe '#largest_repeatable_element' do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
expect {
|
32
|
-
@roman_number.largest_repeatable_element(44)
|
33
|
-
}.to raise_error(StartsWithNonRepeatableRomanUnitError)
|
11
|
+
# set 1
|
12
|
+
[
|
13
|
+
[3999, {:reduced_integer => 999, :largest_element => {:unit => :M, :value => 1000}, :times => 3}],
|
14
|
+
[1066, {:reduced_integer => 66, :largest_element => {:unit => :M, :value => 1000}, :times => 1}],
|
15
|
+
[207, {:reduced_integer => 7, :largest_element => {:unit => :C, :value => 100}, :times => 2}],
|
16
|
+
[10, {:reduced_integer => 0, :largest_element => {:unit => :X, :value => 10}, :times => 1}],
|
17
|
+
].each do |exp_input, exp_output|
|
18
|
+
it %Q{works for #{exp_input} as input} do
|
19
|
+
output = @roman_number.largest_repeatable_element(exp_input)
|
20
|
+
output.should == exp_output
|
21
|
+
end
|
22
|
+
end
|
23
|
+
# set 2
|
24
|
+
[
|
25
|
+
[999, StartsWithNonRepeatableRomanUnitError],
|
26
|
+
[44, StartsWithNonRepeatableRomanUnitError],
|
27
|
+
].each do |exp_input, exp_output|
|
28
|
+
it %Q{raises #{exp_output} for #{exp_input} as input} do
|
29
|
+
expect {
|
30
|
+
@roman_number.largest_repeatable_element(exp_input)
|
31
|
+
}.to raise_error(exp_output)
|
32
|
+
end
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
@@ -38,212 +37,57 @@ module RomanNumbers
|
|
38
37
|
before(:each) do
|
39
38
|
@roman_number = RomanNumber.new(1)
|
40
39
|
end
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
output.should == {:reduced_integer => 4, :largest_element => {:unit => :XL, :value => 40}, :times => 1}
|
52
|
-
end
|
53
|
-
it 'works for "49" as input' do
|
54
|
-
output = @roman_number.largest_non_repeatable_element(49)
|
55
|
-
output.should == {:reduced_integer => 9, :largest_element => {:unit => :XL, :value => 40}, :times => 1}
|
40
|
+
[
|
41
|
+
[999, {:reduced_integer => 99, :largest_element => {:unit => :CM, :value => 900}, :times => 1}],
|
42
|
+
[900, {:reduced_integer => 0, :largest_element => {:unit => :CM, :value => 900}, :times => 1}],
|
43
|
+
[44, {:reduced_integer => 4, :largest_element => {:unit => :XL, :value => 40}, :times => 1}],
|
44
|
+
[49, {:reduced_integer => 9, :largest_element => {:unit => :XL, :value => 40}, :times => 1}],
|
45
|
+
].each do |exp_input, exp_output|
|
46
|
+
it %Q{works for #{exp_input} as input} do
|
47
|
+
output = @roman_number.largest_non_repeatable_element(exp_input)
|
48
|
+
output.should == exp_output
|
49
|
+
end
|
56
50
|
end
|
57
51
|
end
|
58
52
|
|
59
53
|
describe '#convert_arabic_to_roman' do
|
60
|
-
context 'For
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
RomanNumber.new(5).convert_decimal_to_roman.should == 'V'
|
66
|
-
end
|
67
|
-
it 'returns X for 10' do
|
68
|
-
RomanNumber.new(10).convert_decimal_to_roman.should == 'X'
|
69
|
-
end
|
70
|
-
it 'returns L for 50' do
|
71
|
-
RomanNumber.new(50).convert_decimal_to_roman.should == 'L'
|
72
|
-
end
|
73
|
-
it 'returns C for 100' do
|
74
|
-
RomanNumber.new(100).convert_decimal_to_roman.should == 'C'
|
75
|
-
end
|
76
|
-
it 'returns D for 500' do
|
77
|
-
RomanNumber.new(500).convert_decimal_to_roman.should == 'D'
|
78
|
-
end
|
79
|
-
it 'returns M for 1000' do
|
80
|
-
RomanNumber.new(1000).convert_decimal_to_roman.should == 'M'
|
81
|
-
end
|
82
|
-
end
|
83
|
-
context 'For Simple Integers' do
|
84
|
-
it 'returns IV for 4' do
|
85
|
-
RomanNumber.new(4).convert_decimal_to_roman.should == 'IV'
|
86
|
-
end
|
87
|
-
it 'returns IX for 9' do
|
88
|
-
RomanNumber.new(9).convert_decimal_to_roman.should == 'IX'
|
89
|
-
end
|
90
|
-
it 'returns XL for 40' do
|
91
|
-
RomanNumber.new(40).convert_decimal_to_roman.should == 'XL'
|
92
|
-
end
|
93
|
-
it 'returns XC for 90' do
|
94
|
-
RomanNumber.new(90).convert_decimal_to_roman.should == 'XC'
|
95
|
-
end
|
96
|
-
it 'returns CD for 400' do
|
97
|
-
RomanNumber.new(400).convert_decimal_to_roman.should == 'CD'
|
98
|
-
end
|
99
|
-
it 'returns CM for 900' do
|
100
|
-
RomanNumber.new(900).convert_decimal_to_roman.should == 'CM'
|
101
|
-
end
|
102
|
-
it 'returns CCVII for 207' do
|
103
|
-
RomanNumber.new(207).convert_decimal_to_roman.should == 'CCVII'
|
104
|
-
end
|
105
|
-
it 'returns MLXVI for 1066' do
|
106
|
-
RomanNumber.new(1066).convert_decimal_to_roman.should == 'MLXVI'
|
107
|
-
end
|
108
|
-
end
|
109
|
-
context 'For Complex Integers' do
|
110
|
-
it 'returns XLIV for 44' do
|
111
|
-
RomanNumber.new(44).convert_decimal_to_roman.should == 'XLIV'
|
112
|
-
end
|
113
|
-
it 'returns XLIX for 49' do
|
114
|
-
RomanNumber.new(49).convert_decimal_to_roman.should == 'XLIX'
|
115
|
-
end
|
116
|
-
it 'returns CMIV for 904' do
|
117
|
-
RomanNumber.new(904).convert_decimal_to_roman.should == 'CMIV'
|
118
|
-
end
|
119
|
-
it 'returns CMXLIV for 944' do
|
120
|
-
RomanNumber.new(944).convert_decimal_to_roman.should == 'CMXLIV'
|
121
|
-
end
|
122
|
-
it 'returns MCMIV for 1904' do
|
123
|
-
RomanNumber.new(1904).convert_decimal_to_roman.should == 'MCMIV'
|
124
|
-
end
|
125
|
-
it 'returns MCMXLIV for 1944' do
|
126
|
-
RomanNumber.new(1944).convert_decimal_to_roman.should == 'MCMXLIV'
|
127
|
-
end
|
128
|
-
end
|
129
|
-
context 'For Very Simple Integers' do
|
130
|
-
it 'returns MMMCMXCIX for 3999' do
|
131
|
-
RomanNumber.new(3999).convert_decimal_to_roman.should == 'MMMCMXCIX'
|
54
|
+
context 'For Valid Input' do
|
55
|
+
Helpers.valid_inputs.each do |exp_input, exp_output|
|
56
|
+
it %Q{returns #{exp_output} for #{exp_input} as input} do
|
57
|
+
RomanNumber.new(exp_input).convert_decimal_to_roman.should == exp_output
|
58
|
+
end
|
132
59
|
end
|
133
60
|
end
|
134
61
|
context 'For Invalid Input' do
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
end
|
145
|
-
it 'raises InvalidInputError for -10 as input' do
|
146
|
-
expect {
|
147
|
-
RomanNumber.new(-10).convert_decimal_to_roman
|
148
|
-
}.to raise_error(InvalidInputError)
|
149
|
-
end
|
150
|
-
it 'raises InvalidInputError for "10" as input' do
|
151
|
-
expect {
|
152
|
-
RomanNumber.new('10').convert_decimal_to_roman
|
153
|
-
}.to raise_error(InvalidInputError)
|
62
|
+
exp_output = InvalidInputError
|
63
|
+
[
|
64
|
+
4000, 0, -10, '10'
|
65
|
+
].each do |exp_input|
|
66
|
+
it %Q{raises #{exp_output} for #{exp_input} as input} do
|
67
|
+
expect {
|
68
|
+
RomanNumber.new(exp_input).convert_decimal_to_roman
|
69
|
+
}.to raise_error(exp_output)
|
70
|
+
end
|
154
71
|
end
|
155
72
|
end
|
156
73
|
end
|
157
74
|
|
158
75
|
describe '#convert_roman_to_arabic' do
|
159
|
-
context 'For
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
RomanNumber.new('V').convert_roman_to_decimal.should == 5
|
165
|
-
end
|
166
|
-
it 'returns 10 for X' do
|
167
|
-
RomanNumber.new('X').convert_roman_to_decimal.should == 10
|
168
|
-
end
|
169
|
-
it 'returns 50 for L' do
|
170
|
-
RomanNumber.new('L').convert_roman_to_decimal.should == 50
|
171
|
-
end
|
172
|
-
it 'returns 100 for C' do
|
173
|
-
RomanNumber.new('C').convert_roman_to_decimal.should == 100
|
174
|
-
end
|
175
|
-
it 'returns 500 for D' do
|
176
|
-
RomanNumber.new('D').convert_roman_to_decimal.should == 500
|
177
|
-
end
|
178
|
-
it 'returns 1000 for M' do
|
179
|
-
RomanNumber.new('M').convert_roman_to_decimal.should == 1000
|
180
|
-
end
|
181
|
-
it 'returns 4 for IV' do
|
182
|
-
RomanNumber.new('IV').convert_roman_to_decimal.should == 4
|
183
|
-
end
|
184
|
-
it 'returns 9 for IX' do
|
185
|
-
RomanNumber.new('IX').convert_roman_to_decimal.should == 9
|
186
|
-
end
|
187
|
-
it 'returns 40 for XL' do
|
188
|
-
RomanNumber.new('XL').convert_roman_to_decimal.should == 40
|
189
|
-
end
|
190
|
-
it 'returns 90 for XC' do
|
191
|
-
RomanNumber.new('XC').convert_roman_to_decimal.should == 90
|
192
|
-
end
|
193
|
-
it 'returns 400 for CD' do
|
194
|
-
RomanNumber.new('CD').convert_roman_to_decimal.should == 400
|
195
|
-
end
|
196
|
-
it 'returns 900 for CM' do
|
197
|
-
RomanNumber.new('CM').convert_roman_to_decimal.should == 900
|
198
|
-
end
|
199
|
-
end
|
200
|
-
context 'For Complex Romans' do
|
201
|
-
it 'returns 207 for CCVII' do
|
202
|
-
RomanNumber.new('CCVII').convert_roman_to_decimal.should == 207
|
203
|
-
end
|
204
|
-
it 'returns 1066 for MLXVI' do
|
205
|
-
RomanNumber.new('MLXVI').convert_roman_to_decimal.should == 1066
|
206
|
-
end
|
207
|
-
it 'returns 44 for XLIV' do
|
208
|
-
RomanNumber.new('XLIV').convert_roman_to_decimal.should == 44
|
209
|
-
end
|
210
|
-
it 'returns 49 for XLIX' do
|
211
|
-
RomanNumber.new('XLIX').convert_roman_to_decimal.should == 49
|
212
|
-
end
|
213
|
-
it 'returns 904 for CMIV' do
|
214
|
-
RomanNumber.new('CMIV').convert_roman_to_decimal.should == 904
|
215
|
-
end
|
216
|
-
it 'returns 944 for CMXLIV' do
|
217
|
-
RomanNumber.new('CMXLIV').convert_roman_to_decimal.should == 944
|
218
|
-
end
|
219
|
-
it 'returns 1904 for MCMIV' do
|
220
|
-
RomanNumber.new('MCMIV').convert_roman_to_decimal.should == 1904
|
221
|
-
end
|
222
|
-
it 'returns 1944 for MCMXLIV' do
|
223
|
-
RomanNumber.new('MCMXLIV').convert_roman_to_decimal.should == 1944
|
224
|
-
end
|
225
|
-
it 'returns 30 for XXX' do
|
226
|
-
RomanNumber.new('XXX').convert_roman_to_decimal.should == 30
|
227
|
-
end
|
228
|
-
it 'returns 3999 for MMMCMXCIX' do
|
229
|
-
RomanNumber.new('MMMCMXCIX').convert_roman_to_decimal.should == 3999
|
76
|
+
context 'For Valid Input' do
|
77
|
+
Helpers.valid_inputs.each do |exp_output, exp_input|
|
78
|
+
it %Q{returns #{exp_output} for #{exp_input} as input} do
|
79
|
+
RomanNumber.new(exp_input).convert_roman_to_decimal.should == exp_output
|
80
|
+
end
|
230
81
|
end
|
231
82
|
end
|
232
83
|
context 'For Invalid Input' do
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
RomanNumber.new('CCCCVII').convert_roman_to_decimal
|
241
|
-
}.to raise_error(InvalidInputError)
|
242
|
-
end
|
243
|
-
it 'raises InvalidInputError for CCVVVVI as input' do
|
244
|
-
expect {
|
245
|
-
RomanNumber.new('CCVVVVI').convert_roman_to_decimal
|
246
|
-
}.to raise_error(InvalidInputError)
|
84
|
+
exp_output = InvalidInputError
|
85
|
+
%w(ABCDE CCCCVII CCVVVVI XXM).each do |exp_input|
|
86
|
+
it %Q{raises #{exp_output} for #{exp_input} as input} do
|
87
|
+
expect {
|
88
|
+
RomanNumber.new(exp_input).convert_roman_to_decimal
|
89
|
+
}.to raise_error(exp_output)
|
90
|
+
end
|
247
91
|
end
|
248
92
|
end
|
249
93
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roman_numbers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Munish Goyal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- lib/roman_numbers/roman_number.rb
|
113
113
|
- lib/roman_numbers/version.rb
|
114
114
|
- roman_numbers.gemspec
|
115
|
+
- spec/helpers.rb
|
115
116
|
- spec/roman_numbers/roman_number_spec.rb
|
116
117
|
- spec/spec_helper.rb
|
117
118
|
homepage: https://github.com/goyalmunish/roman_numbers
|
@@ -126,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
127
|
requirements:
|
127
128
|
- - '>='
|
128
129
|
- !ruby/object:Gem::Version
|
129
|
-
version: '
|
130
|
+
version: '1.9'
|
130
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
132
|
requirements:
|
132
133
|
- - '>='
|
@@ -139,5 +140,6 @@ signing_key:
|
|
139
140
|
specification_version: 4
|
140
141
|
summary: Conversion between Integers and Roman Numbers
|
141
142
|
test_files:
|
143
|
+
- spec/helpers.rb
|
142
144
|
- spec/roman_numbers/roman_number_spec.rb
|
143
145
|
- spec/spec_helper.rb
|