roman_numerals_converter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in roman_numerals_converter.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Adam Dratwinski
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,39 @@
1
+ # RomanNumeralsConverter
2
+
3
+ RomanNumeralsConverter is a simple tool that provides API for converting arabic numerals to roman and vice versa.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'roman_numerals_converter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install roman_numerals_converter
18
+
19
+ ## Usage
20
+
21
+ API is simple, it provides following method:
22
+
23
+ ```ruby
24
+ 10.to_arabic # => "X"
25
+ RomanNumeralsConverter::is_arabic? 10 # => true
26
+ RomanNumeralsConverter::is_roman? "X" # => true
27
+ RomanNumeralsConverter::to_roman? 10 # => "X"
28
+ RomanNumeralsConverter::to_arabic? "X" # => 10
29
+ ```
30
+
31
+ Note that the maximum Roman number supported is 3999 (MMMCMXCIX).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,108 @@
1
+ module RomanNumeralsConverter
2
+ class Base
3
+ attr_accessor :roman_number, :arabic_number, :number
4
+
5
+ MAPPINGS = [
6
+ { :roman => "M", :arabic => 1000 },
7
+ { :roman => "CM", :arabic => 900 },
8
+ { :roman => "D", :arabic => 500 },
9
+ { :roman => "CD", :arabic => 400 },
10
+ { :roman => "C", :arabic => 100 },
11
+ { :roman => "XC", :arabic => 90 },
12
+ { :roman => "L", :arabic => 50 },
13
+ { :roman => "XL", :arabic => 40 },
14
+ { :roman => "X", :arabic => 10 },
15
+ { :roman => "IX", :arabic => 9 },
16
+ { :roman => "V", :arabic => 5 },
17
+ { :roman => "IV", :arabic => 4 },
18
+ { :roman => "I", :arabic => 1}
19
+ ]
20
+
21
+ def initialize number
22
+ self.number = number
23
+ if self.is_roman?
24
+ self.roman_number = number
25
+ self.arabic_number = self.convert_to_arabic
26
+ elsif self.is_arabic?
27
+ self.arabic_number = number
28
+ self.roman_number = self.convert_to_roman
29
+ else
30
+ raise ArgumentError, "Can`t convert number"
31
+ end
32
+ end
33
+
34
+ def is_roman?
35
+ ! "#{self.number}".match(/[IVXLCDM]+/).nil?
36
+ end
37
+
38
+ def is_arabic?
39
+ self.number.is_a? Fixnum
40
+ end
41
+
42
+ def to_roman
43
+ self.roman_number
44
+ end
45
+
46
+ def to_arabic
47
+ self.arabic_number
48
+ end
49
+
50
+ def convert_to_roman
51
+ temp_number = self.number
52
+ roman_number = ""
53
+ MAPPINGS.each_with_index do |mapping, i|
54
+ q = temp_number/mapping[:arabic]
55
+
56
+ if q <= 3 and q > 0
57
+ roman_number += mapping[:roman]*q
58
+ temp_number -= mapping[:arabic]*q
59
+ end
60
+ end
61
+
62
+ if temp_number > 0
63
+ raise ArgumentError, "Number #{self.arabic_number} can`t be converted to a roman number"
64
+ end
65
+ roman_number
66
+ end
67
+
68
+ def convert_to_arabic
69
+ temp_number = self.number
70
+ arabic_number = 0
71
+
72
+ MAPPINGS.each_with_index do |mapping, i|
73
+ q = number.scan(mapping[:roman]).count
74
+
75
+ if i%2 == 1
76
+ if q == 1
77
+ arabic_number += q*mapping[:arabic]
78
+ temp_number = temp_number.split(mapping[:roman]).join
79
+ elsif q > 1
80
+ raise ArgumentError, "Number #{self.roman_number} can`t be converted to an arabic number"
81
+ end
82
+ end
83
+ end
84
+
85
+ MAPPINGS.each_with_index do |mapping, i|
86
+ q = temp_number.scan(mapping[:roman]).count
87
+
88
+ if q <= 3 and i%2 == 0
89
+ arabic_number += q*mapping[:arabic]
90
+ temp_number = temp_number.split(mapping[:roman]).join
91
+ elsif q > 3
92
+ raise ArgumentError, "Number #{self.roman_number} can`t be converted to an arabic number"
93
+ end
94
+ end
95
+ arabic_number
96
+ end
97
+
98
+ private
99
+
100
+ def number_from_mappings number
101
+ RomanNumeralsConverter::MAPPINGS.each_pair do |key, val|
102
+ if number == val
103
+ return key
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,7 @@
1
+ module RomanNumeralsConverter
2
+ module FixnumAdditions
3
+ def to_roman
4
+ Base.new(self).to_roman
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module RomanNumeralsConverter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ require "roman_numerals_converter/version"
2
+ require "roman_numerals_converter/base"
3
+ require "roman_numerals_converter/fixnum_additions"
4
+
5
+ module RomanNumeralsConverter
6
+ def self.is_arabic? number
7
+ Base.new(number).is_arabic?
8
+ end
9
+
10
+ def self.is_roman? number
11
+ Base.new(number).is_roman?
12
+ end
13
+
14
+ def self.to_arabic number
15
+ Base.new(number).to_arabic
16
+ end
17
+
18
+ def self.to_roman number
19
+ Base.new(number).to_roman
20
+ end
21
+ end
22
+
23
+ class Fixnum
24
+ include RomanNumeralsConverter::FixnumAdditions
25
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/roman_numerals_converter/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Adam Dratwinski"]
6
+ gem.email = ["adam.dratwinski@gmail.com"]
7
+ gem.description = %q{Simple tool that provides API for converting Roman Numerals to Arabic and vice versa}
8
+ gem.summary = %q{Simple tool that provides API for converting Roman Numerals to Arabic and vice versa}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "roman_numerals_converter"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RomanNumeralsConverter::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ gem.add_development_dependency "rake"
20
+
21
+ end
@@ -0,0 +1,194 @@
1
+ require "spec_helper"
2
+
3
+ describe RomanNumeralsConverter do
4
+ def test_arabic_to_roman a,b
5
+ converter = RomanNumeralsConverter::Base.new a
6
+ converter.to_roman.should == b
7
+ end
8
+
9
+ def test_roman_to_arabic a,b
10
+ converter = RomanNumeralsConverter::Base.new a
11
+ converter.to_arabic.should == b
12
+ end
13
+
14
+ describe "converts arabic numerals to roman" do
15
+ it "with ones" do
16
+ test_arabic_to_roman 1, "I"
17
+ end
18
+
19
+ it "with twos" do
20
+ test_arabic_to_roman 2, "II"
21
+ end
22
+
23
+ it "with threes" do
24
+ test_arabic_to_roman 3, "III"
25
+ end
26
+
27
+ it "with tens" do
28
+ test_arabic_to_roman 10, "X"
29
+ end
30
+
31
+ it "with twenties" do
32
+ test_arabic_to_roman 20, "XX"
33
+ end
34
+
35
+ it "with twenties" do
36
+ test_arabic_to_roman 21, "XXI"
37
+ end
38
+
39
+ it "with fours" do
40
+ test_arabic_to_roman 4, "IV"
41
+ end
42
+
43
+ it "with fourteens" do
44
+ test_arabic_to_roman 14, "XIV"
45
+ end
46
+
47
+ it "with fourties" do
48
+ test_arabic_to_roman 40, "XL"
49
+ end
50
+
51
+ it "with fives" do
52
+ test_arabic_to_roman 5, "V"
53
+ end
54
+
55
+ it "with sixs" do
56
+ test_arabic_to_roman 6, "VI"
57
+ end
58
+
59
+ it "with sevens" do
60
+ test_arabic_to_roman 7, "VII"
61
+ end
62
+
63
+ it "with eights" do
64
+ test_arabic_to_roman 8, "VIII"
65
+ end
66
+
67
+ it "with nines" do
68
+ test_arabic_to_roman 9, "IX"
69
+ end
70
+
71
+ it "with nineties" do
72
+ test_arabic_to_roman 90, "XC"
73
+ end
74
+
75
+ it "with 50's" do
76
+ test_arabic_to_roman 50, "L"
77
+ end
78
+
79
+ it "with 100's" do
80
+ test_arabic_to_roman 100, "C"
81
+ end
82
+
83
+ it "with 400's" do
84
+ test_arabic_to_roman 400, "CD"
85
+ end
86
+
87
+ it "with 500's" do
88
+ test_arabic_to_roman 500, "D"
89
+ end
90
+
91
+ it "with 900's" do
92
+ test_arabic_to_roman 900, "CM"
93
+ end
94
+
95
+ it "with 1901's" do
96
+ test_arabic_to_roman 1941, "MCMXLI"
97
+ end
98
+
99
+ it "with 1999's" do
100
+ test_arabic_to_roman 1999, "MCMXCIX"
101
+ end
102
+
103
+ it "with 3999's" do
104
+ test_arabic_to_roman 3999, "MMMCMXCIX"
105
+ end
106
+
107
+ it "fails with 4000's" do
108
+ lambda do
109
+ converter = RomanNumeralsConverter.new 4000
110
+ converter.to_roman
111
+ end.should raise_error
112
+ end
113
+ end
114
+
115
+ describe "converts roman numerals to arabic" do
116
+ it "with I" do
117
+ test_roman_to_arabic "I", 1
118
+ end
119
+
120
+ it "with II" do
121
+ test_roman_to_arabic "II", 2
122
+ end
123
+
124
+ it "with III" do
125
+ test_roman_to_arabic "III", 3
126
+ end
127
+
128
+ it "with IV" do
129
+ test_roman_to_arabic "IV", 4
130
+ end
131
+
132
+ it "with V" do
133
+ test_roman_to_arabic "V", 5
134
+ end
135
+
136
+ it "with VI" do
137
+ test_roman_to_arabic "VI", 6
138
+ end
139
+
140
+ it "with X" do
141
+ test_roman_to_arabic "X",10
142
+ end
143
+
144
+ it "with XIV" do
145
+ test_roman_to_arabic "XIV",14
146
+ end
147
+
148
+ it "with XL" do
149
+ test_roman_to_arabic "XL",40
150
+ end
151
+
152
+ it "with L" do
153
+ test_roman_to_arabic "L",50
154
+ end
155
+
156
+ it "with XC" do
157
+ test_roman_to_arabic "XC",90
158
+ end
159
+
160
+ it "with C" do
161
+ test_roman_to_arabic "C",100
162
+ end
163
+
164
+ it "with CD" do
165
+ test_roman_to_arabic "CD",400
166
+ end
167
+
168
+ it "with D" do
169
+ test_roman_to_arabic "D",500
170
+ end
171
+
172
+ it "with CM" do
173
+ test_roman_to_arabic "CM",900
174
+ end
175
+
176
+ it "with M" do
177
+ test_roman_to_arabic "M",1000
178
+ end
179
+
180
+ it "fails with IIII" do
181
+ lambda do
182
+ converter = RomanNumeralsConverter.new "IIII"
183
+ converter.to_arabic
184
+ end.should raise_error
185
+ end
186
+
187
+ it "fails with IVIV" do
188
+ lambda do
189
+ converter = RomanNumeralsConverter.new "IVIV"
190
+ converter.to_arabic
191
+ end.should raise_error
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe RomanNumeralsConverter do
4
+ describe RomanNumeralsConverter::FixnumAdditions do
5
+ it "method to_roman should works" do
6
+ 10.to_roman == "X"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe RomanNumeralsConverter do
4
+ it "should have working method is_arabic?" do
5
+ RomanNumeralsConverter::is_arabic?("X").should == false
6
+ RomanNumeralsConverter::is_arabic?(10).should == true
7
+ end
8
+
9
+ it "should have working method to_arabic" do
10
+ RomanNumeralsConverter::to_arabic("X").should == 10
11
+ end
12
+
13
+ it "should have working method is_roman?" do
14
+ RomanNumeralsConverter::is_roman?("X").should == true
15
+ RomanNumeralsConverter::is_roman?(10).should == false
16
+ end
17
+
18
+ it "should have working method to_roman" do
19
+ RomanNumeralsConverter::to_roman(10).should == "X"
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ require 'roman_numerals_converter'
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roman_numerals_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Dratwinski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &78703050 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *78703050
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &78702830 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *78702830
36
+ description: Simple tool that provides API for converting Roman Numerals to Arabic
37
+ and vice versa
38
+ email:
39
+ - adam.dratwinski@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - lib/roman_numerals_converter.rb
50
+ - lib/roman_numerals_converter/base.rb
51
+ - lib/roman_numerals_converter/fixnum_additions.rb
52
+ - lib/roman_numerals_converter/version.rb
53
+ - roman_numerals_converter.gemspec
54
+ - spec/roman_numerals_converter/base_spec.rb
55
+ - spec/roman_numerals_converter/fixnum_additions_spec.rb
56
+ - spec/roman_numerals_converter_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: ''
59
+ licenses: []
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 1.8.15
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Simple tool that provides API for converting Roman Numerals to Arabic and
82
+ vice versa
83
+ test_files:
84
+ - spec/roman_numerals_converter/base_spec.rb
85
+ - spec/roman_numerals_converter/fixnum_additions_spec.rb
86
+ - spec/roman_numerals_converter_spec.rb
87
+ - spec/spec_helper.rb