Basic Language Features
Fundamentals
Control Flow
Roman numerals can be generated with the following letters I, V, X, L, C, D, M
, but the generated numbers must be entered in decimal. Let's see examples:
convert_decimal_roman('46') # 'XLVI'
convert_decimal_roman('999') # 'CMXCIX'
convert_decimal_roman('504') # 'DIV'
Requirements:
- The number per argument must be in string type
- No validation required for floating numbers
- Only numbers up to 999 can be entered
def convert_decimal_roman(numero):
num_romanos_dic = {
1: 'I',
5: 'V',
10: 'X',
50: 'L',
100: 'C',
500: 'D',
1000: 'M'
}
list_num_rom = [{'key': x, 'value': y} for x, y in num_romanos_dic.items()]
size_num = len(numero)
output = []
for i in range(size_num):
num = numero[i:]
num_unit = int(num)
numextre_izq = int(num[0])
pref = []
for i in range(1, len(list_num_rom), 2):
if num_unit >= list_num_rom[i-1]['key'] and num_unit < list_num_rom[i+1]['key']:
pref.append(list_num_rom[i-1]['value'])
pref.append(list_num_rom[i]['value'])
pref.append(list_num_rom[i+1]['value'])
if numextre_izq < 4 and numextre_izq > 0:
output.append(pref[0]*numextre_izq)
if numextre_izq == 4:
output.extend(pref[0:2])
if numextre_izq == 5:
output.append(pref[1])
if numextre_izq > 5 and numextre_izq < 9:
output.append(pref[1] + pref[0]*(numextre_izq-5))
if numextre_izq == 9:
output.extend(pref[::2])
output = ''.join(output)
return output
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(convert_decimal_roman('69'), 'LXIX')
test.assert_equals(convert_decimal_roman('874'), 'DCCCLXXIV')
test.assert_equals(convert_decimal_roman('938'), 'CMXXXVIII')
test.assert_equals(convert_decimal_roman('442'), 'CDXLII')
test.assert_equals(convert_decimal_roman('323'), 'CCCXXIII')
test.assert_equals(convert_decimal_roman('548'), 'DXLVIII')
test.assert_equals(convert_decimal_roman('386'), 'CCCLXXXVI')
test.assert_equals(convert_decimal_roman('962'), 'CMLXII')
test.assert_equals(convert_decimal_roman('217'), 'CCXVII')
test.assert_equals(convert_decimal_roman('771'), 'DCCLXXI')
test.assert_equals(convert_decimal_roman('997'), 'CMXCVII')
test.assert_equals(convert_decimal_roman('175'), 'CLXXV')
test.assert_equals(convert_decimal_roman('873'), 'DCCCLXXIII')
test.assert_equals(convert_decimal_roman('33'), 'XXXIII')
test.assert_equals(convert_decimal_roman('508'), 'DVIII')
test.assert_equals(convert_decimal_roman('743'), 'DCCXLIII')
test.assert_equals(convert_decimal_roman('326'), 'CCCXXVI')
test.assert_equals(convert_decimal_roman('858'), 'DCCCLVIII')
test.assert_equals(convert_decimal_roman('707'), 'DCCVII')
test.assert_equals(convert_decimal_roman('459'), 'CDLIX')
test.assert_equals(convert_decimal_roman('976'), 'CMLXXVI')
test.assert_equals(convert_decimal_roman('855'), 'DCCCLV')
test.assert_equals(convert_decimal_roman('840'), 'DCCCXL')
test.assert_equals(convert_decimal_roman('682'), 'DCLXXXII')
test.assert_equals(convert_decimal_roman('506'), 'DVI')
test.assert_equals(convert_decimal_roman('553'), 'DLIII')
test.assert_equals(convert_decimal_roman('1'), 'I')
test.assert_equals(convert_decimal_roman('48'), 'XLVIII')
test.assert_equals(convert_decimal_roman('125'), 'CXXV')
test.assert_equals(convert_decimal_roman('353'), 'CCCLIII')
test.assert_equals(convert_decimal_roman('137'), 'CXXXVII')
test.assert_equals(convert_decimal_roman('423'), 'CDXXIII')
test.assert_equals(convert_decimal_roman('26'), 'XXVI')
test.assert_equals(convert_decimal_roman('362'), 'CCCLXII')
test.assert_equals(convert_decimal_roman('587'), 'DLXXXVII')
test.assert_equals(convert_decimal_roman('599'), 'DXCIX')
test.assert_equals(convert_decimal_roman('589'), 'DLXXXIX')
test.assert_equals(convert_decimal_roman('619'), 'DCXIX')
test.assert_equals(convert_decimal_roman('261'), 'CCLXI')
test.assert_equals(convert_decimal_roman('480'), 'CDLXXX')
test.assert_equals(convert_decimal_roman('410'), 'CDX')
test.assert_equals(convert_decimal_roman('776'), 'DCCLXXVI')
test.assert_equals(convert_decimal_roman('625'), 'DCXXV')
test.assert_equals(convert_decimal_roman('116'), 'CXVI')
test.assert_equals(convert_decimal_roman('112'), 'CXII')
test.assert_equals(convert_decimal_roman('603'), 'DCIII')
test.assert_equals(convert_decimal_roman('549'), 'DXLIX')
test.assert_equals(convert_decimal_roman('838'), 'DCCCXXXVIII')
test.assert_equals(convert_decimal_roman('846'), 'DCCCXLVI')
test.assert_equals(convert_decimal_roman('756'), 'DCCLVI')
test.assert_equals(convert_decimal_roman('333'), 'CCCXXXIII')
test.assert_equals(convert_decimal_roman('325'), 'CCCXXV')
test.assert_equals(convert_decimal_roman('154'), 'CLIV')
test.assert_equals(convert_decimal_roman('104'), 'CIV')
test.assert_equals(convert_decimal_roman('669'), 'DCLXIX')
test.assert_equals(convert_decimal_roman('822'), 'DCCCXXII')
test.assert_equals(convert_decimal_roman('51'), 'LI')
test.assert_equals(convert_decimal_roman('372'), 'CCCLXXII')
test.assert_equals(convert_decimal_roman('824'), 'DCCCXXIV')
test.assert_equals(convert_decimal_roman('136'), 'CXXXVI')
test.assert_equals(convert_decimal_roman('89'), 'LXXXIX')
test.assert_equals(convert_decimal_roman('693'), 'DCXCIII')
test.assert_equals(convert_decimal_roman('81'), 'LXXXI')
test.assert_equals(convert_decimal_roman('681'), 'DCLXXXI')
test.assert_equals(convert_decimal_roman('306'), 'CCCVI')
test.assert_equals(convert_decimal_roman('398'), 'CCCXCVIII')
test.assert_equals(convert_decimal_roman('554'), 'DLIV')
test.assert_equals(convert_decimal_roman('651'), 'DCLI')
test.assert_equals(convert_decimal_roman('403'), 'CDIII')
test.assert_equals(convert_decimal_roman('485'), 'CDLXXXV')
test.assert_equals(convert_decimal_roman('815'), 'DCCCXV')
test.assert_equals(convert_decimal_roman('88'), 'LXXXVIII')
test.assert_equals(convert_decimal_roman('53'), 'LIII')
test.assert_equals(convert_decimal_roman('610'), 'DCX')
test.assert_equals(convert_decimal_roman('410'), 'CDX')
test.assert_equals(convert_decimal_roman('376'), 'CCCLXXVI')
test.assert_equals(convert_decimal_roman('791'), 'DCCXCI')
test.assert_equals(convert_decimal_roman('594'), 'DXCIV')
test.assert_equals(convert_decimal_roman('486'), 'CDLXXXVI')
test.assert_equals(convert_decimal_roman('165'), 'CLXV')
test.assert_equals(convert_decimal_roman('812'), 'DCCCXII')
test.assert_equals(convert_decimal_roman('224'), 'CCXXIV')
test.assert_equals(convert_decimal_roman('185'), 'CLXXXV')
test.assert_equals(convert_decimal_roman('833'), 'DCCCXXXIII')
test.assert_equals(convert_decimal_roman('994'), 'CMXCIV')
test.assert_equals(convert_decimal_roman('385'), 'CCCLXXXV')
test.assert_equals(convert_decimal_roman('821'), 'DCCCXXI')
test.assert_equals(convert_decimal_roman('572'), 'DLXXII')
test.assert_equals(convert_decimal_roman('428'), 'CDXXVIII')
test.assert_equals(convert_decimal_roman('918'), 'CMXVIII')
test.assert_equals(convert_decimal_roman('86'), 'LXXXVI')
test.assert_equals(convert_decimal_roman('238'), 'CCXXXVIII')
test.assert_equals(convert_decimal_roman('206'), 'CCVI')
test.assert_equals(convert_decimal_roman('56'), 'LVI')
test.assert_equals(convert_decimal_roman('279'), 'CCLXXIX')
test.assert_equals(convert_decimal_roman('72'), 'LXXII')
test.assert_equals(convert_decimal_roman('380'), 'CCCLXXX')
test.assert_equals(convert_decimal_roman('26'), 'XXVI')
test.assert_equals(convert_decimal_roman('715'), 'DCCXV')
test.assert_equals(convert_decimal_roman('168'), 'CLXVIII')