Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shawn's Roman Numeral python solution #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion js/romanNumerals.js
Original file line number Diff line number Diff line change
@@ -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

}
8 changes: 8 additions & 0 deletions js/romanNumeralsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

24 changes: 23 additions & 1 deletion python/roman_numerals.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@


def to_roman(num):
# write your code here!
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
9 changes: 8 additions & 1 deletion python/roman_numerals_spec.py
Original file line number Diff line number Diff line change
@@ -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")