diff --git a/js/romanNumerals.js b/js/romanNumerals.js index c816e81..5bfcded 100644 --- a/js/romanNumerals.js +++ b/js/romanNumerals.js @@ -1,3 +1,49 @@ -exports.toRoman = function(num) { +//pt 1* +//pt 2 +exports.toRoman = function(num) { + let ansStr = ''; + do { + if(num >= 1000){ + num -= 1000; + ansStr += 'M' + } + else duh (num >= 500){ + num -= 500; + ansStr += 'D' + } + else if (num >= 100){ + num -= 100; + ansStr += 'C' + } + else if (num >= 50){ + num -= 50; + ansStr += 'L'; + } + else if (num >= 10){ + num -= 10; + ansStr += 'X'; + } + else if (num >= 5){ + num -= 5; + ansStr += 'V'; + } + else if (num === 4){ + num -= 4; + ansStr += 'IV'; + } + else { + num -= 1; + ansStr += 'I' + } + } while (num > 0); + return ansStr; }; + +/* +if (num >= 900 && num < 1000) { + num -= 900 + ansStr += "CM"} // for times sake -- conditions would reflect this + +/* + diff --git a/python/.gitignore b/python/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/python/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/python/__pycache__/roman_numerals.cpython-39.pyc b/python/__pycache__/roman_numerals.cpython-39.pyc new file mode 100644 index 0000000..917f694 Binary files /dev/null and b/python/__pycache__/roman_numerals.cpython-39.pyc differ diff --git a/python/roman_numerals.py b/python/roman_numerals.py index cbf8571..aa81b64 100644 --- a/python/roman_numerals.py +++ b/python/roman_numerals.py @@ -1,2 +1,53 @@ +# 1. Write a method TO_ROMAN, TO_ROMAN takes in INPUT_NUMBER (an arabic number) +# 2. Create a OUTPUT string, set to '' +# 3. Create a ROMAN_NUMERAL_TO_ARABIC_MAP that includes roman numerals as keys, arabic numbers as values +# 4. Iterate over ROMAN_NUMERAL_TO_ARABIC_MAP, keep track of ROMAN_NUMERAL and ARABIC_NUMBER +# 5. Set EVENLY_DIVISIBLE_TIMES = INPUT_NUMBER / ARABIC_NUMBER: +# 6. If EVENLY_DIVISIBLE_TIMES >= 1 + # 6a. Append ROMAN_NUMERAL to OUTPUT EVENLY_DIVISIBLE_TIMES + # 6b. Subtract ARABIC_NUMBER from INPUT_NUMBER EVENLY_DIVISIBLE_TIMES +# 7. Return OUTPUT + +#python +import math def to_roman(num): - # write your code here! \ No newline at end of file + + dict_roman_num = { + 'M' : 1000, + 'CM': 900, + 'D' : 500, + 'CD': 400, + 'C' : 100, + 'XC': 90, + 'L' : 50, + 'XL': 40, + 'X' : 10, + 'IX': 9, + 'V' : 5, + 'IV': 4, + 'I' : 1 + } + + roman_char_list = [] # target list for appended roman chars from dict + roman_value_list = [] # target list for appended values from dict + + roman_str = '' + + for roman_char, roman_value in dict_roman_num.items(): # + roman_char_list.append(roman_char) + roman_value_list.append(roman_value) + # Create iterable lists from dict items of key, values + + + index = 0 + while num > 0: + for x in range(math.floor(num /roman_value_list[index])): #Iterate through the range of number / index at value list + roman_str += roman_char_list[index] # roman string adds I + # print(roman_str, roman_char_list[index]) + num -= roman_value_list[index] # num param decrements to 0 and breaks out + # print(roman_value_list[index]) + index += 1 # add 1 to index value to iterate through lists + + return roman_str + + diff --git a/python/roman_numerals_spec.py b/python/roman_numerals_spec.py index 39d195e..abf7019 100644 --- a/python/roman_numerals_spec.py +++ b/python/roman_numerals_spec.py @@ -1,6 +1,41 @@ +import unittest from roman_numerals import to_roman -print(to_roman(1) == 'I') -print(to_roman(3) == 'III') -print(to_roman(4) == 'IV') -# add tests to cover different edge cases +class RomanNumeralsTestCase(unittest.TestCase): + + + # add tests to cover different edge cases + def test_output(self): + self.assertEqual(to_roman(1), 'I') + self.assertEqual(to_roman(3), 'III') + self.assertEqual(to_roman(4), 'IV') + self.assertEqual(to_roman(944), 'CMXLIV') + self.assertEqual(to_roman(150), 'CL') + self.assertEqual(to_roman(2400), 'MMCD') + + + # Output correct roman numerals + + + def test_str(self): + self.assertNotEqual(to_roman(1), str) + + # output returns a string type + + + def test_returns_modern_numerals(self): + self.assertNotEqual(to_roman(4), 'IIII') + + # output not in lazy roman numerals + + # IV -> 4 + # IX -> 9 + # XIV -> 14 + # XLIV -> 44 + # CMXLIV -> 944 + + # to_roman(4) # 'IV' + # to_roman(944) # 'CMXLIV' + # to_roman(150) # CL +if __name__ == '__main__': + unittest.main() \ No newline at end of file