Skip to content

Instantly share code, notes, and snippets.

@Koronen
Created January 27, 2012 20:03
Show Gist options
  • Save Koronen/1690643 to your computer and use it in GitHub Desktop.
Save Koronen/1690643 to your computer and use it in GitHub Desktop.
Roman Numerals
*.gem
.bundle
Gemfile.lock
pkg/*

Roman Numerals

Logic for dealing with Roman numerals.

License

The MIT License

Copyright (C) 2012 Victor Koronen

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.

source :rubygems
# Specify your gem's dependencies in roman_numerals.gemspec
gemspec
guard 'bundler' do
watch('Gemfile')
watch(/^.+\.gemspec/)
end
guard 'minitest' do
watch(%r|^spec/(.*)_spec\.rb|)
watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r|^spec/spec_helper\.rb|) { "spec" }
end
require 'bundler/gem_tasks'
require 'rake/testtask'
Rake::TestTask.new(:spec) do |t|
t.libs << 'spec'
t.test_files = FileList['spec/**/*_spec.rb']
end
task default: :spec
class RomanNumeral
NUMERALS = {
"I" => 1,
"V" => 5,
"X" => 10,
"L" => 50,
"C" => 100,
"D" => 500,
"M" => 1000
}
def initialize(number)
if number.is_a? String
number = parse_string number
else
number = number.to_i
end
if number < 0
throw ArgumentError
end
@number = number
end
def to_i
@number
end
def to_s
return "" if @number == 0
NUMERALS.each do |numeral, value|
return numeral if @number == value
end
str = ""
sum = 0
NUMERALS.to_a.reverse.each do |numeral, value|
while (@number - sum) >= value
str += numeral
sum += value
end
end
str.gsub!("DCCCC", "CM")
str.gsub!("LXXXX", "XC")
str.gsub!("VIIII", "IX")
str.gsub!("IIII", "IV")
str
end
private
def valid_string?(str)
/^[#{NUMERALS.keys.join}]*$/.match str
end
def parse_string(str)
throw ArgumentError unless valid_string? str
return 0 if str.empty?
tokens = str.split(//).map{|t| NUMERALS[t] }
number = 0
buffer = last = tokens.shift
tokens.each do |n|
if n == last
buffer += n
elsif n < last
number += buffer
buffer = n
elsif n > last
number += n - buffer
buffer = 0
end
last = n
end
if buffer > 0
number += buffer
end
number
end
end
require 'spec_helper'
require 'roman_numeral'
describe RomanNumeral do
describe "constructor" do
it "accepts non-negative integers" do
RomanNumeral.new(1).wont_be_nil
end
it "rejects negative integers" do
lambda { RomanNumeral.new(-1) }.must_throw ArgumentError
end
it "accepts valid non-negative roman numerals as strings" do
RomanNumeral.new("I").wont_be_nil
end
it "rejects invalid roman numerals as strings" do
lambda { RomanNumeral.new("foobar") }.must_throw ArgumentError
end
end
describe "#to_i" do
it "handles valid integers" do
RomanNumeral.new(0).to_i.must_equal 0
RomanNumeral::NUMERALS.each do |_, value|
RomanNumeral.new(value).to_i.must_equal value
end
RomanNumeral.new(17).to_i.must_equal 17
RomanNumeral.new(4711).to_i.must_equal 4711
end
it "handles valid roman numerals" do
RomanNumeral.new("").to_i.must_equal 0
RomanNumeral::NUMERALS.each do |numeral, value|
RomanNumeral.new(numeral).to_i.must_equal value
end
RomanNumeral.new("IV").to_i.must_equal 4
RomanNumeral.new("IX").to_i.must_equal 9
RomanNumeral.new("XVII").to_i.must_equal 17
RomanNumeral.new("MCMXCIX").to_i.must_equal 1999
RomanNumeral.new("MMMMDCCXI").to_i.must_equal 4711
end
end
describe "#to_s" do
it "handles valid integers" do
RomanNumeral.new(0).to_s.must_equal ""
RomanNumeral::NUMERALS.each do |numeral, value|
RomanNumeral.new(value).to_s.must_equal numeral
end
RomanNumeral.new(4).to_s.must_equal "IV"
RomanNumeral.new(9).to_s.must_equal "IX"
RomanNumeral.new(17).to_s.must_equal "XVII"
RomanNumeral.new(1999).to_s.must_equal "MCMXCIX"
RomanNumeral.new(4711).to_s.must_equal "MMMMDCCXI"
end
it "handles valid roman numerals" do
RomanNumeral.new("").to_s.must_equal ""
RomanNumeral::NUMERALS.each do |numeral, _|
RomanNumeral.new(numeral).to_s.must_equal numeral
end
RomanNumeral.new("IV").to_s.must_equal "IV"
RomanNumeral.new("IX").to_s.must_equal "IX"
RomanNumeral.new("XVII").to_s.must_equal "XVII"
RomanNumeral.new("MCMXCIX").to_s.must_equal "MCMXCIX"
RomanNumeral.new("MMMMDCCXI").to_s.must_equal "MMMMDCCXI"
end
end
end
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = "roman_numerals"
s.version = "0.0.1"
s.authors = ["Victor Koronen"]
s.email = ["[email protected]"]
s.homepage = ""
s.summary = %q{Roman numerals}
s.description = %q{Roman numerals}
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.add_development_dependency "minitest"
s.add_development_dependency "guard"
s.add_development_dependency "guard-bundler"
s.add_development_dependency "guard-minitest"
end
require 'minitest/autorun'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment