Skip to content

Instantly share code, notes, and snippets.

@bforrest
Created April 21, 2012 00:53
Show Gist options
  • Select an option

  • Save bforrest/2432956 to your computer and use it in GitHub Desktop.

Select an option

Save bforrest/2432956 to your computer and use it in GitHub Desktop.
Roman Numeral Parser
//
// Copyright 2012 barry forrest
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Linq;
namespace RomanNumeralKata
{
public static class NumeralParser
{
public static int? FromRomanNumeral(this string source)
{
int? parsedValue;
int sum = 0;
for(int i = 0; i < source.Length; i++)
{
RomanNumeral current = (RomanNumeral)Enum.Parse(typeof(RomanNumeral), source[i].ToString(), true );
RomanNumeral next = (i + 1 < source.Length)
? (RomanNumeral)Enum.Parse(typeof(RomanNumeral), source[i + 1].ToString(), true )
: RomanNumeral.Default;
if( current < next )
{
sum += next - current;
++i;
}
else
{
sum += current;
}
}
if( sum > 0 )
parsedValue = sum;
return parsedValue;
}
}
}
//
// Copyright 2012 barry forrest
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using NUnit.Framework;
namespace RomanNumeralKata
{
[TestFixture()]
public class NumeralParserTests
{
[Test()]
public void I_Translates_To_One ()
{
Assert.That("i".FromRomanNumeral() == 1);
}
[Test()]
public void II_Translates_To_Two()
{
Assert.That("ii".FromRomanNumeral() == 2);
}
[Test]
public void IV_Translates_To_Four()
{
Assert.That("iv".FromRomanNumeral() == 4);
}
[Test()]
public void V_Translates_To_Five()
{
Assert.That("v".FromRomanNumeral() == 5);
}
[Test]
public void VIII_Translates_To_8()
{
Assert.That("viii".FromRomanNumeral() == 8);
}
[Test]
public void XIV_Translates_To_Fourteen()
{
Assert.That("XIV".FromRomanNumeral() == 14);
}
[Test]
public void M_Translates_To_1000()
{
Assert.That("m".FromRomanNumeral() == 1000);
}
[Test]
public void CDXLVIII_Translates_To_448()
{
Assert.That("CDXLVIII".FromRomanNumeral() == 448);
}
[Test]
public void MCMXCVIII_Translates_To_1998()
{
Assert.That("MCMXCVIII".FromRomanNumeral() == 1998);
}
}
}
// http://en.wikipedia.org/wiki/Roman_numerals#Reading_Roman_numerals
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000
These Roman Numbers are formed by combining symbols together and adding the values. For example, MMVI is 1000 + 1000 + 5 + 1 = 2006. Generally, symbols are placed in order of value, starting with the largest values. When smaller values precede larger values, the smaller values are subtracted from the larger values, and the result is added to the total. For example MCMXLIV = 1000 + (1000 − 100) + (50 − 10) + (5 − 1) = 1944.
//
// Copyright 2012 barry forrest
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
namespace RomanNumeralKata
{
public enum RomanNumeral
{
Default = 0,
I = 1,
V = 5,
X = 10,
L = 50,
C = 100,
D = 500,
M = 1000
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment