arrabiata 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 invadersmustdie
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ ## Arabiata - dead simple conversion of roman <> arabian numerals
2
+
3
+ ### Example
4
+
5
+ >> Arabiata.to_arabian("MC")
6
+ => 1100
7
+
8
+ >> Arabiata.to_roman(1100)
9
+ => "MC"
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake/testtask'
2
+ require 'rspec/core/rake_task'
3
+ require 'rake/gempackagetask'
4
+
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "arrabiata"
8
+ gem.homepage = "http://github.com/invadersmustdie/arrabiata"
9
+ gem.license = "MIT"
10
+ gem.summary = %Q{dead simple conversion of roman <> arabian numerals}
11
+ gem.description = %Q{dead simple conversion of roman <> arabian numerals}
12
+ gem.email = "rugek@dirtyhack.net"
13
+ gem.authors = ["invadersmustdie"]
14
+ end
15
+ Jeweler::RubygemsDotOrgTasks.new
16
+
17
+ RSpec::Core::RakeTask.new(:spec) do |spec|
18
+ spec.pattern = 'spec/**/*_spec.rb'
19
+ spec.rspec_opts = ['--colour', '-f documentation', '--backtrace']
20
+ end
21
+
22
+ task :default => [:spec]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/arrabiata.gemspec ADDED
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{arrabiata}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["invadersmustdie"]
12
+ s.date = %q{2011-01-20}
13
+ s.description = %q{dead simple conversion of roman <> arabian numerals}
14
+ s.email = %q{rugek@dirtyhack.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "LICENSE.txt",
21
+ "README.md",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "arrabiata.gemspec",
25
+ "lib/arrabiata.rb",
26
+ "spec/arrabiata_to_arabian_spec.rb",
27
+ "spec/arrabiata_to_roman_spec.rb",
28
+ "spec/spec_helper.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/invadersmustdie/arrabiata}
31
+ s.licenses = ["MIT"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.7}
34
+ s.summary = %q{dead simple conversion of roman <> arabian numerals}
35
+ s.test_files = [
36
+ "spec/arrabiata_to_arabian_spec.rb",
37
+ "spec/arrabiata_to_roman_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
51
+
data/lib/arrabiata.rb ADDED
@@ -0,0 +1,46 @@
1
+ class Arrabiata
2
+ class ArrabiataError < StandardError; end
3
+ class NoZeroInRomanNumbers < ArrabiataError; end
4
+ class UnknownRomanNumberError < ArrabiataError; end
5
+
6
+ CONVERSION = {
7
+ "M" => 1000,
8
+ "D" => 500,
9
+ "C" => 100,
10
+ "L" => 50,
11
+ "X" => 10,
12
+ "V" => 5,
13
+ "I" => 1
14
+ }
15
+
16
+ def self.to_roman(n)
17
+ raise ArgumentError, "first argument is not a Fixnum" if !n.is_a?(Fixnum)
18
+ raise NoZeroInRomanNumbers, "roman numeral system doesn't include 0" if n == 0
19
+
20
+ result = ""
21
+
22
+ # NOTE: this version doesn't yet respects the subtractive principle
23
+ CONVERSION.sort_by { |d| d.last }.reverse.each do |k|
24
+ x = n / k.last
25
+ if x != 0
26
+ result << k.first * x
27
+ n = n - x * k.last
28
+ end
29
+ end
30
+
31
+ result
32
+ end
33
+
34
+ def self.to_arabian(str)
35
+ raise ArgumentError, "first argument is not a String" if !str.is_a?(String)
36
+
37
+ result = 0
38
+
39
+ str.scan(/\w/).each do |char|
40
+ raise UnknownRomanNumberError, "'#{char}' is not defined in roman numbers" if !CONVERSION[char]
41
+ result = result + CONVERSION[char]
42
+ end
43
+
44
+ result
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe "Arrabiata -> to_arabian" do
4
+ it "should fail on characters not defined in roman numerals" do
5
+ lambda {
6
+ Arrabiata.to_arabian("MVVa")
7
+ }.should raise_error(Arrabiata::UnknownRomanNumberError, "'a' is not defined in roman numbers")
8
+ end
9
+
10
+ it "should fail on invalid argument passed" do
11
+ lambda {
12
+ Arrabiata.to_arabian(4.2)
13
+ }.should raise_error(ArgumentError, "first argument is not a String")
14
+ end
15
+
16
+ it "should convert III to 3" do
17
+ Arrabiata.to_arabian("III").should == 3
18
+ end
19
+
20
+ it "should convert VIII to 8" do
21
+ Arrabiata.to_arabian("VIII").should == 8
22
+ end
23
+
24
+ it "should convert M to 1000" do
25
+ Arrabiata.to_arabian("M").should == 1000
26
+ end
27
+
28
+ it "should convert MIII to 1003" do
29
+ Arrabiata.to_arabian("MIII").should == 1003
30
+ end
31
+
32
+ it "should convert MMDCCCLV to 2855 " do
33
+ Arrabiata.to_arabian("MMDCCCLV").should == 2855
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe "Arrabiata -> to_roman" do
4
+ it "should fail on 0 because its not included in roman numerals" do
5
+ lambda {
6
+ Arrabiata.to_roman(0)
7
+ }.should raise_error(Arrabiata::NoZeroInRomanNumbers, "roman numeral system doesn't include 0")
8
+ end
9
+
10
+ it "should fail on invalid argument passed" do
11
+ lambda {
12
+ Arrabiata.to_roman(4.2)
13
+ }.should raise_error(ArgumentError, "first argument is not a Fixnum")
14
+ end
15
+
16
+ it "should convert 3 to III" do
17
+ Arrabiata.to_roman(3).should == "III"
18
+ end
19
+
20
+ it "should convert 8 to VIII" do
21
+ Arrabiata.to_roman(8).should == "VIII"
22
+ end
23
+
24
+ it "should convert 1000 to M" do
25
+ Arrabiata.to_roman(1000).should == "M"
26
+ end
27
+
28
+ it "should convert 1003 to M" do
29
+ Arrabiata.to_roman(1003).should == "MIII"
30
+ end
31
+
32
+ it "should convert 2855 to MMDCCCLV" do
33
+ Arrabiata.to_roman(2855).should == "MMDCCCLV"
34
+ end
35
+ end
@@ -0,0 +1 @@
1
+ require "arrabiata"
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arrabiata
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - invadersmustdie
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-20 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: dead simple conversion of roman <> arabian numerals
23
+ email: rugek@dirtyhack.net
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE.txt
30
+ - README.md
31
+ files:
32
+ - LICENSE.txt
33
+ - README.md
34
+ - Rakefile
35
+ - VERSION
36
+ - arrabiata.gemspec
37
+ - lib/arrabiata.rb
38
+ - spec/arrabiata_to_arabian_spec.rb
39
+ - spec/arrabiata_to_roman_spec.rb
40
+ - spec/spec_helper.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/invadersmustdie/arrabiata
43
+ licenses:
44
+ - MIT
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: dead simple conversion of roman <> arabian numerals
75
+ test_files:
76
+ - spec/arrabiata_to_arabian_spec.rb
77
+ - spec/arrabiata_to_roman_spec.rb
78
+ - spec/spec_helper.rb