RosettaCodeData/Task/Roman-numerals-Decode/Elena/roman-numerals-decode.elena
2026-02-01 16:33:20 -08:00

46 lines
1.2 KiB
Text

import extensions;
import system'collections;
import system'routines;
import system'culture;
static RomanDictionary = Dictionary.new()
.setAt("I".toChar(), 1)
.setAt("V".toChar(), 5)
.setAt("X".toChar(), 10)
.setAt("L".toChar(), 50)
.setAt("C".toChar(), 100)
.setAt("D".toChar(), 500)
.setAt("M".toChar(), 1000);
extension op : String
{
toRomanInt()
{
var minus := 0;
var s := self.toUpper();
var total := 0;
for(int i := 0; i < s.Length; i += 1)
{
var thisNumeral := RomanDictionary[s[i]] - minus;
if (i >= s.Length - 1 || thisNumeral + minus >= RomanDictionary[s[i + 1]])
{
total += thisNumeral;
minus := 0
}
else
{
minus := thisNumeral
}
};
^ total
}
}
public Program()
{
Console.printLine("MCMXC: ", "MCMXC".toRomanInt());
Console.printLine("MMVIII: ", "MMVIII".toRomanInt());
Console.printLine("MDCLXVI:", "MDCLXVI".toRomanInt())
}