-
Notifications
You must be signed in to change notification settings - Fork 966
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from faisalr/Roman
Roman Numerals
- Loading branch information
Showing
5 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Xunit; | ||
using Xunit.Extensions; | ||
using Humanizer; | ||
|
||
namespace Humanizer.Tests | ||
{ | ||
public class RomanNumeralTests | ||
{ | ||
[Theory] | ||
[InlineData(1, "I")] | ||
[InlineData(2, "II")] | ||
[InlineData(3, "III")] | ||
[InlineData(4, "IV")] | ||
[InlineData(5, "V")] | ||
[InlineData(6, "VI")] | ||
[InlineData(7, "VII")] | ||
[InlineData(8, "VIII")] | ||
[InlineData(9, "IX")] | ||
[InlineData(10, "X")] | ||
[InlineData(11, "XI")] | ||
[InlineData(12, "XII")] | ||
[InlineData(100, "C")] | ||
[InlineData(3999, "MMMCMXCIX")] | ||
public void CanRomanize(int input, string expected) | ||
{ | ||
Assert.Equal(expected, input.ToRoman()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "I")] | ||
[InlineData(2, "II")] | ||
[InlineData(3, "III")] | ||
[InlineData(4, "IV")] | ||
[InlineData(5, "V")] | ||
[InlineData(6, "VI")] | ||
[InlineData(7, "VII")] | ||
[InlineData(8, "VIII")] | ||
[InlineData(9, "IX")] | ||
[InlineData(10, "X")] | ||
[InlineData(11, "XI")] | ||
[InlineData(12, "XII")] | ||
[InlineData(100, "C")] | ||
[InlineData(3999, "MMMCMXCIX")] | ||
public void CanUnromanize(int expected, string input) | ||
{ | ||
Assert.Equal(expected, input.FromRoman()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Humanizer | ||
{ | ||
public static class RomanNumeralExtensions | ||
{ | ||
private const int NumberOfRomanNumeralMaps = 13; | ||
|
||
private static readonly Dictionary<string, int> romanNumerals = new Dictionary<string, int>(NumberOfRomanNumeralMaps) | ||
{ | ||
{ "M", 1000 }, | ||
{ "CM", 900 }, | ||
{ "D", 500 }, | ||
{ "CD", 400 }, | ||
{ "C", 100 }, | ||
{ "XC", 90 }, | ||
{ "L", 50 }, | ||
{ "XL", 40 }, | ||
{ "X", 10 }, | ||
{ "IX", 9 }, | ||
{ "V", 5 }, | ||
{ "IV", 4 }, | ||
{ "I", 1 } | ||
}; | ||
|
||
private static readonly Regex validRomanNumeral = new Regex("^(?i:(?=[MDCLXVI])((M{0,3})((C[DM])|(D?C{0,3}))" + "?((X[LC])|(L?XX{0,2})|L)?((I[VX])|(V?(II{0,2}))|V)?))$", RegexOptions.None); | ||
|
||
/// <summary> | ||
/// Converts Roman numbers into integer | ||
/// </summary> | ||
/// <param name="value">Roman number</param> | ||
/// <returns>Human-readable number</returns> | ||
public static int FromRoman(this string value) | ||
{ | ||
if (value == null) | ||
throw new ArgumentNullException("value"); | ||
|
||
value = value.ToUpper().Trim(); | ||
var length = value.Length; | ||
|
||
if (length == 0 || IsInvalidRomanNumeral(value)) | ||
throw new ArgumentException("Empty or invalid Roman numeral string.", "value"); | ||
|
||
var total = 0; | ||
var i = length; | ||
|
||
while (i > 0) | ||
{ | ||
var digit = romanNumerals[value[--i].ToString()]; | ||
|
||
if (i > 0) | ||
{ | ||
var previousDigit = romanNumerals[value[i - 1].ToString()]; | ||
|
||
if (previousDigit < digit) | ||
{ | ||
digit -= previousDigit; | ||
i--; | ||
} | ||
} | ||
|
||
total += digit; | ||
} | ||
|
||
return total; | ||
} | ||
|
||
/// <summary> | ||
/// Converts the input to Roman number | ||
/// </summary> | ||
/// <param name="value">Human-readable number</param> | ||
/// <returns>Roman number</returns> | ||
public static string ToRoman(this int value) | ||
{ | ||
const int MinValue = 1; | ||
const int MaxValue = 3999; | ||
const int MaxRomanNumeralLength = 15; | ||
|
||
if ((value < MinValue) || (value > MaxValue)) | ||
throw new ArgumentOutOfRangeException(); | ||
|
||
var sb = new StringBuilder(MaxRomanNumeralLength); | ||
|
||
foreach (var pair in romanNumerals) | ||
{ | ||
while (value / pair.Value > 0) | ||
{ | ||
sb.Append(pair.Key); | ||
value -= pair.Value; | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
private static bool IsInvalidRomanNumeral(string value) | ||
{ | ||
return !validRomanNumeral.IsMatch(value); | ||
} | ||
} | ||
} |