Skip to content

Instantly share code, notes, and snippets.

@benneuman
Created February 18, 2014 20:31
Show Gist options
  • Save benneuman/9079409 to your computer and use it in GitHub Desktop.
Save benneuman/9079409 to your computer and use it in GitHub Desktop.
require 'rspec'
def from_roman(roman)
if roman.include? "X"
roman[0] == "X" ? 10 * roman.count("X") + ones(roman) : 9
else
ones(roman)
end
end
def ones(roman)
if roman.include? "V"
roman[0] == "V" ? 5 + roman.count("I") : 4
else
roman.count('I')
end
end
describe "Roman Numeral Converter" do
it "converts I" do
expect(from_roman("I")).to eq(1)
end
it "converts III" do
expect(from_roman("III")).to eq(3)
end
it "converts V" do
expect(from_roman("V")).to eq(5)
end
it "converts VI" do
expect(from_roman("VI")).to eq(6)
end
it "converts IV" do
expect(from_roman("IV")).to eq(4)
end
it "converts X" do
expect(from_roman("X")).to eq(10)
end
it "converts XI" do
expect(from_roman("XI")).to eq(11)
end
it "converts IX" do
expect(from_roman("IX")).to eq(9)
end
it "converts XIV" do
expect(from_roman("XIV")).to eq(14)
end
it "converts XX" do
expect(from_roman("XX")).to eq(20)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment