Navigation Menu

Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
invadersmustdie committed Jan 20, 2011
0 parents commit 46a931b
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,20 @@
Copyright (c) 2011 invadersmustdie

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
@@ -0,0 +1,9 @@
## Arabiata - dead simple conversion of roman <> arabian numerals

### Example

>> Arabiata.to_arabian("MC")
=> 1100

>> Arabiata.to_roman(1100)
=> "MC"
22 changes: 22 additions & 0 deletions Rakefile
@@ -0,0 +1,22 @@
require 'rake/testtask'
require 'rspec/core/rake_task'
require 'rake/gempackagetask'

require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "arrabiata"
gem.homepage = "http://github.com/invadersmustdie/arrabiata"
gem.license = "MIT"
gem.summary = %Q{dead simple conversion of roman <> arabian numerals}
gem.description = %Q{dead simple conversion of roman <> arabian numerals}
gem.email = "rugek@dirtyhack.net"
gem.authors = ["invadersmustdie"]
end
Jeweler::RubygemsDotOrgTasks.new

RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rspec_opts = ['--colour', '-f documentation', '--backtrace']
end

task :default => [:spec]
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
0.1.0
35 changes: 35 additions & 0 deletions arabiata.gemspec
@@ -0,0 +1,35 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{arabiata}
s.version = "0.1.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["invadersmustdie"]
s.date = %q{2011-01-20}
s.description = %q{dead simple conversion of roman <> arabian numerals}
s.email = %q{rugek@dirtyhack.net}
s.extra_rdoc_files = [
"LICENSE.txt",
"README.md"
]
s.homepage = %q{http://github.com/invadersmustdie/arabiata}
s.licenses = ["MIT"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{dead simple conversion of roman <> arabian numerals}

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
else
end
else
end
end

46 changes: 46 additions & 0 deletions lib/arrabiata.rb
@@ -0,0 +1,46 @@
class Arrabiata
class ArrabiataError < StandardError; end
class NoZeroInRomanNumbers < ArrabiataError; end
class UnknownRomanNumberError < ArrabiataError; end

CONVERSION = {
"M" => 1000,
"D" => 500,
"C" => 100,
"L" => 50,
"X" => 10,
"V" => 5,
"I" => 1
}

def self.to_roman(n)
raise ArgumentError, "first argument is not a Fixnum" if !n.is_a?(Fixnum)
raise NoZeroInRomanNumbers, "roman numeral system doesn't include 0" if n == 0

result = ""

# NOTE: this version doesn't yet respects the subtractive principle
CONVERSION.sort_by { |d| d.last }.reverse.each do |k|
x = n / k.last
if x != 0
result << k.first * x
n = n - x * k.last
end
end

result
end

def self.to_arabian(str)
raise ArgumentError, "first argument is not a String" if !str.is_a?(String)

result = 0

str.scan(/\w/).each do |char|
raise UnknownRomanNumberError, "'#{char}' is not defined in roman numbers" if !CONVERSION[char]
result = result + CONVERSION[char]
end

result
end
end
35 changes: 35 additions & 0 deletions spec/arabiata_to_arabian_spec.rb
@@ -0,0 +1,35 @@
require "spec_helper"

describe "Arrabiata -> to_arabian" do
it "should fail on characters not defined in roman numerals" do
lambda {
Arrabiata.to_arabian("MVVa")
}.should raise_error(Arrabiata::UnknownRomanNumberError, "'a' is not defined in roman numbers")
end

it "should fail on invalid argument passed" do
lambda {
Arrabiata.to_arabian(4.2)
}.should raise_error(ArgumentError, "first argument is not a String")
end

it "should convert III to 3" do
Arrabiata.to_arabian("III").should == 3
end

it "should convert VIII to 8" do
Arrabiata.to_arabian("VIII").should == 8
end

it "should convert M to 1000" do
Arrabiata.to_arabian("M").should == 1000
end

it "should convert MIII to 1003" do
Arrabiata.to_arabian("MIII").should == 1003
end

it "should convert MMDCCCLV to 2855 " do
Arrabiata.to_arabian("MMDCCCLV").should == 2855
end
end
35 changes: 35 additions & 0 deletions spec/arabiata_to_roman_spec.rb
@@ -0,0 +1,35 @@
require "spec_helper"

describe "Arrabiata -> to_roman" do
it "should fail on 0 because its not included in roman numerals" do
lambda {
Arrabiata.to_roman(0)
}.should raise_error(Arrabiata::NoZeroInRomanNumbers, "roman numeral system doesn't include 0")
end

it "should fail on invalid argument passed" do
lambda {
Arrabiata.to_roman(4.2)
}.should raise_error(ArgumentError, "first argument is not a Fixnum")
end

it "should convert 3 to III" do
Arrabiata.to_roman(3).should == "III"
end

it "should convert 8 to VIII" do
Arrabiata.to_roman(8).should == "VIII"
end

it "should convert 1000 to M" do
Arrabiata.to_roman(1000).should == "M"
end

it "should convert 1003 to M" do
Arrabiata.to_roman(1003).should == "MIII"
end

it "should convert 2855 to MMDCCCLV" do
Arrabiata.to_roman(2855).should == "MMDCCCLV"
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -0,0 +1 @@
require "arrabiata"

0 comments on commit 46a931b

Please sign in to comment.