Skip to content

Instantly share code, notes, and snippets.

@skkut
Last active March 6, 2019 10:29
Show Gist options
  • Select an option

  • Save skkut/59a7b45f59b6f5d7e2ec to your computer and use it in GitHub Desktop.

Select an option

Save skkut/59a7b45f59b6f5d7e2ec to your computer and use it in GitHub Desktop.

Revisions

  1. skkut revised this gist Mar 6, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ConvertRomanToNumerals.cs
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,6 @@ public static void ProcessFile(string path)

    string output = Regex.Replace(text, pattern, delegate(Match match)
    {
    string v = match.ToString();
    return match.Groups[1].Value + match.Groups[2].Value + ConvertRomanNumeralToInt(match.Groups[3].Value) + match.Groups[4].Value;
    });

  2. skkut revised this gist Nov 7, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ConvertRomanToNumerals.cs
    Original file line number Diff line number Diff line change
    @@ -63,7 +63,7 @@ public static void ProcessFile(string path)
    });

    if(Regex.IsMatch(text, pattern))
    System.IO.File.WriteAllText(path, output);
    System.IO.File.WriteAllText(path, output);

    Console.WriteLine("Processed file '{0}'.", path);
    }
  3. skkut revised this gist Sep 9, 2015. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions ConvertRomanToNumerals.cs
    Original file line number Diff line number Diff line change
    @@ -68,8 +68,6 @@ public static void ProcessFile(string path)
    Console.WriteLine("Processed file '{0}'.", path);
    }



    public static string ConvertRomanNumeralToInt(string romanNumeralString)
    {
    if (romanNumeralString == null) return int.MinValue.ToString();
  4. skkut created this gist Sep 9, 2015.
    105 changes: 105 additions & 0 deletions ConvertRomanToNumerals.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConvertRomanToNumeral
    {
    public class RomanNumber
    {
    public string Numeral { get; set; }
    public int Value { get; set; }
    public int Hierarchy { get; set; }
    }

    class Program
    {
    public static List<RomanNumber> RomanNumbers = new List<RomanNumber>
    {
    new RomanNumber {Numeral = "M", Value = 1000, Hierarchy = 4},
    new RomanNumber {Numeral = "D", Value = 500, Hierarchy = 4},
    new RomanNumber {Numeral = "C", Value = 100, Hierarchy = 3},
    new RomanNumber {Numeral = "L", Value = 50, Hierarchy = 3},
    new RomanNumber {Numeral = "X", Value = 10, Hierarchy = 2},
    new RomanNumber {Numeral = "V", Value = 5, Hierarchy = 2},
    new RomanNumber {Numeral = "I", Value = 1, Hierarchy = 1}
    };

    static void Main(string[] args)
    {
    if (args.Length == 0)
    return;
    string path = args[0];

    if (Directory.Exists(path))
    {
    ProcessDirectory(path);
    }
    }

    public static void ProcessDirectory(string targetDirectory)
    {
    string[] fileEntries = Directory.GetFiles(targetDirectory);
    foreach (string fileName in fileEntries)
    ProcessFile(fileName);

    string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
    foreach (string subdirectory in subdirectoryEntries)
    ProcessDirectory(subdirectory);
    }

    public static void ProcessFile(string path)
    {
    string text = System.IO.File.ReadAllText(path);
    string pattern = @"(<h.>)(Chapter )([IVCLXDM]+?)(</h.>)";

    string output = Regex.Replace(text, pattern, delegate(Match match)
    {
    string v = match.ToString();
    return match.Groups[1].Value + match.Groups[2].Value + ConvertRomanNumeralToInt(match.Groups[3].Value) + match.Groups[4].Value;
    });

    if(Regex.IsMatch(text, pattern))
    System.IO.File.WriteAllText(path, output);

    Console.WriteLine("Processed file '{0}'.", path);
    }



    public static string ConvertRomanNumeralToInt(string romanNumeralString)
    {
    if (romanNumeralString == null) return int.MinValue.ToString();

    var total = 0;
    for (var i = 0; i < romanNumeralString.Length; i++)
    {
    var current = romanNumeralString[i].ToString();
    var curRomanNum = RomanNumbers.First(rn => rn.Numeral.ToUpper() == current.ToUpper());

    if (i + 1 == romanNumeralString.Length)
    {
    total += curRomanNum.Value;
    break;
    }

    var next = romanNumeralString[i + 1].ToString();
    var nextRomanNum = RomanNumbers.First(rn => rn.Numeral.ToUpper() == next.ToUpper());

    if (curRomanNum.Hierarchy == (nextRomanNum.Hierarchy - 1))
    {
    total += nextRomanNum.Value - curRomanNum.Value;
    i++;
    }
    else
    {
    total += curRomanNum.Value;
    }
    }
    return total.ToString();
    }
    }
    }