Last active
March 6, 2019 10:29
-
-
Save skkut/59a7b45f59b6f5d7e2ec to your computer and use it in GitHub Desktop.
Revisions
-
skkut revised this gist
Mar 6, 2019 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) { return match.Groups[1].Value + match.Groups[2].Value + ConvertRomanNumeralToInt(match.Groups[3].Value) + match.Groups[4].Value; }); -
skkut revised this gist
Nov 7, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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); Console.WriteLine("Processed file '{0}'.", path); } -
skkut revised this gist
Sep 9, 2015 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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(); -
skkut created this gist
Sep 9, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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(); } } }