diff --git a/js/romanNumerals.js b/js/romanNumerals.js index c816e81..4b0f752 100644 --- a/js/romanNumerals.js +++ b/js/romanNumerals.js @@ -1,3 +1,71 @@ +const romanNum = { + "I" : 1, + "IV" : 4, + "V" : 5, + "IX" : 9, + "X" : 10, + "XIV" : 14, + "XLIV" : 44, + "L" : 50, + "C" : 100, + "D" : 500, + "CMXLIV": 944, + "M" : 1000 +}; +// Locate key by value +const getKey = (obj,val) => Object.keys(obj).find(key => obj[key] === val); + exports.toRoman = function(num) { + let roman_str = ''; + while (num >= 1000) { + roman_str = roman_str.concat(getKey(romanNum, 1000)) + num -= 1000 + } + if (num === 944) { + roman_str = roman_str.concat(getKey(romanNum, 944)) + num -= 944 + } + while (num >= 500) { + roman_str = roman_str.concat(getKey(romanNum, 500)) + num -= 500 + } + while (num >= 100) { + roman_str = roman_str.concat(getKey(romanNum, 100)) + num -= 100 + } + while (num >= 50) { + roman_str = roman_str.concat(getKey(romanNum, 50)) + num -= 50 + } + if (num === 44) { + roman_str = roman_str.concat(getKey(romanNum, 44)) + num -= 44 + } + if (num === 14) { + roman_str = roman_str.concat(getKey(romanNum, 14)) + num -= 14 + } + while (num >= 10) { + roman_str = roman_str.concat(getKey(romanNum, 10)) + num -= 10 + } + if (num === 9) { + roman_str = roman_str.concat(getKey(romanNum, 9)) + num -= 9 + } + while (num >= 5) { + roman_str = roman_str.concat(getKey(romanNum, 5)) + num -= 5 + } + if (num === 4) { + roman_str = roman_str.concat(getKey(romanNum, 4)) + num -= 4 + } + while (num >= 1) { + roman_str = roman_str.concat(getKey(romanNum, 1)) + num -= 1 + } -}; + return roman_str + +} \ No newline at end of file diff --git a/js/romanNumeralsSpec.js b/js/romanNumeralsSpec.js index 61cad7f..8769fc4 100644 --- a/js/romanNumeralsSpec.js +++ b/js/romanNumeralsSpec.js @@ -3,3 +3,11 @@ var rn = require("./romanNumerals"); console.log(rn.toRoman(1) === 'I'); console.log(rn.toRoman(3) === 'III'); console.log(rn.toRoman(4) === 'IV'); +console.log(rn.toRoman(9) === 'IX'); +console.log(rn.toRoman(14) === 'XIV'); +console.log(rn.toRoman(44) === 'XLIV'); +console.log(rn.toRoman(944) === 'CMXLIV'); +console.log(rn.toRoman(1000) === 'M'); + +console.log(rn.toRoman(2000) === 'MM'); + diff --git a/python/roman_numerals.py b/python/roman_numerals.py index cbf8571..a3cbfad 100644 --- a/python/roman_numerals.py +++ b/python/roman_numerals.py @@ -1,2 +1,24 @@ + + def to_roman(num): - # write your code here! \ No newline at end of file + CONST_DEC_TO_ROMAN = [ [1000, "M"], [500, "D"], [100, "C"], [50, "L"], [10, "X"], [5, "V"], [1, "I"]] + + roman = "" + + for index in range(len(CONST_DEC_TO_ROMAN)): + roman += (num // CONST_DEC_TO_ROMAN[index][0]) * CONST_DEC_TO_ROMAN[index][1] + num -= num // CONST_DEC_TO_ROMAN[index][0] * CONST_DEC_TO_ROMAN[index][0] + + + return roman + +def to_roman_modern(num): + CONST_DEC_TO_ROMAN = [ [1000, "M"], [900, "CM"], [500, "D"], [400, "CD"], [100, "C"], [90, "XC"], [50, "L"], [40, "XL"], [10, "X"], [9, "IX"], [4, "IV"], [5, "V"], [1, "I"]] + + roman = "" + + for index in range(len(CONST_DEC_TO_ROMAN)): + roman += (num // CONST_DEC_TO_ROMAN[index][0]) * CONST_DEC_TO_ROMAN[index][1] + num -= num // CONST_DEC_TO_ROMAN[index][0] * CONST_DEC_TO_ROMAN[index][0] + + return roman diff --git a/python/roman_numerals_spec.py b/python/roman_numerals_spec.py index 39d195e..832c466 100644 --- a/python/roman_numerals_spec.py +++ b/python/roman_numerals_spec.py @@ -1,6 +1,13 @@ from roman_numerals import to_roman +from roman_numerals import to_roman_modern print(to_roman(1) == 'I') print(to_roman(3) == 'III') -print(to_roman(4) == 'IV') +print(to_roman(4) == 'IIII') +print(to_roman(505) == "DV") +print(to_roman(739) == "DCCXXXVIIII") # add tests to cover different edge cases + +print(to_roman_modern(4) == 'IV') +print(to_roman_modern(739) == "DCCXXXIX") +print(to_roman_modern(944) == "CMXLIV") \ No newline at end of file