Skip to content

Instantly share code, notes, and snippets.

@khan-hasan
Created December 18, 2018 20:45
Show Gist options
  • Save khan-hasan/77c9fcc8099dfbcf7dae0740fb70d6e0 to your computer and use it in GitHub Desktop.
Save khan-hasan/77c9fcc8099dfbcf7dae0740fb70d6e0 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
public class RomanDecode
{
public static int Solution(string roman)
{
// Hashtable representing the numeric value of each roman numeral key
Hashtable romanKeys = new Hashtable() {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
// Array of chars in roman in reverse order
char[] romanChars = roman.ToCharArray();
Array.Reverse(romanChars);
// Numeric value of roman
int total = 0;
// Loop over each numeral in romanChars
for (int i = 0; i < romanChars.Length; i++)
{
// If the numeral at the following index is less than the current numeral, subtract them, and add result to total
if (romanKeys[romanChars[i + 1]] < romanKeys[romanChars[i]])
{
total =+ (romanKeys[romanChars[i]] - romanKeys[romanChars[i + 1]]) * (10^i);
i += 1;
}
else
{
total += (romanKeys[romanChars[i]] + romanKeys[romanChars[i + 1]]) * (10^i);
}
}
return total;
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment