Ad
Basic Language Features
Fundamentals

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