# Roman Numeral Converter Mint language script class RomanNumerals { init() { this.lookup = {'M':1000, 'D':500, 'CD':400, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}; this.values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; this.letters = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']; this.itemCount = 13; } def getValueForLetter(letter) { return this.lookup[letter]; } def toDecimal(numeralString) { var result = 0; var length = len(numeralString); for (var i = 0; i < length - 1; i += 1) { var currentLetter = numeralString[i]; var nextLetter = numeralString[i + 1]; var thisValue = this.getValueForLetter(currentLetter); var nextValue = this.getValueForLetter(nextLetter); # if next letter is bigger than current letter, use subtractive notation... if (nextValue > thisValue) { result += nextValue - thisValue; # then jump two chars i += 1; } else { result += thisValue; } } # then add the last letter result += this.getValueForLetter(numeralString[-1]); return result; } def toRoman(decimalValue) { # loop over all of the value/letter pair choices, and add the letters # if our value is >= that choice. var stringResult = ""; for (var i = 0; i < this.itemCount; i += 1) { var choiceValue = this.values[i]; while (decimalValue >= choiceValue) { stringResult += this.letters[i]; decimalValue -= choiceValue; } } return stringResult; } } def printRangeListOfRomanNumerals(start, count) { var converter = RomanNumerals(); for (var i = start; i < start + count; i += 1) { var roman = converter.toRoman(i); print toStr(i) + "\t: " + roman; } } var converter = RomanNumerals(); print converter.toRoman(1666); #print converter.toRoman(5); #print converter.toDecimal("MCMXII"); # 1912 #print converter.toDecimal("MMCDXXI"); # 2421 #print converter.toDecimal("MLXVI"); # 1066 #printRangeListOfRomanNumerals(1, 4000);