Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ build-iPhoneSimulator/

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

*.sublime-project
*.sublime-workspace
22 changes: 18 additions & 4 deletions lib/roman_numeral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ def initialize( numeral_string )
end

def self.romanize(decimal)
reverse_hashroman = HASHROMAN.invert

roman=""
HASHROMAN.sort_by {|num, val| val }.reverse.each{|num, val|
roman+=num.to_s*(decimal/val).floor # It's a symbol at this point,
decimal-=(decimal/val).floor*val
}
roman
end

def self.decimal_value(numerical_string)
Expand All @@ -53,7 +57,17 @@ def +( other_roman )
end

def -( numeral_string )
"MCMXCVI"
dec_num = decimal_val - numeral_string.decimal_val
dec_num >0 ? RomanNumeral.romanize(dec_num) : "-" + RomanNumeral.romanize(dec_num.abs)
end
end

def *( other_roman )
dec_num = other_roman.decimal_val * decimal_val
RomanNumeral.romanize(dec_num)
end

def /( numeral_string )
dec_num = (decimal_val / numeral_string.decimal_val).floor
dec_num >0 ? RomanNumeral.romanize(dec_num) : "-" + RomanNumeral.romanize(dec_num.abs)
end
end
24 changes: 17 additions & 7 deletions spec/roman_numeral_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
require 'spec_helper'
require 'roman_numeral'


describe RomanNumeral do

it 'should compute simple arithmatic' do
@reports << Benchmark.measure do
# 1996 plus 14 equals 2010
expect( RomanNumeral.new( 'MCMXCVI' ) + RomanNumeral.new( 'XIV' ) ).to eq('MMX')
expect( RomanNumeral.new( 'MMX' ) + RomanNumeral.new( 'XIV' ) ).to eq('MCMXCVI')
end
end

end
# 2010 minus 14 equals 1996
expect( RomanNumeral.new( 'MMX' ) - RomanNumeral.new( 'XIV' ) ).to eq('MCMXCVI')

# 1996 minus 1996 equals -14
expect( RomanNumeral.new( 'MCMXCVI' ) - RomanNumeral.new( 'MMX' ) ).to eq('-XIV')

# 23 times 11 equals 253
expect( RomanNumeral.new( 'XXIII' ) * RomanNumeral.new( 'XI' ) ).to eq('CCLIII')

# 253 divided by 11 equals 23
expect( RomanNumeral.new( 'CCLIII' ) / RomanNumeral.new( 'XI' ) ).to eq('XXIII')

# 100 divided by 33 equals 3. The romans had a complicated fraction system, we're not going there.
expect( RomanNumeral.new( 'C' ) / RomanNumeral.new( 'XXXIII' ) ).to eq('III')
end
end