0

Can somebody explain how this number to roman number code work?

class py_solution: def int_to_Roman(self, num): val = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ] syb = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ] roman_num = '' i = 0 while num > 0: for _ in range(num // val[i]): roman_num += syb[i] num -= val[i] i += 1 return roman_num print(py_solution().int_to_Roman(1)) print(py_solution().int_to_Roman(4000))

20th May 2020, 5:12 AM
Mons Joseph
Mons Joseph - avatar
1 Réponse
+ 1
Syb contain the symbol appropriate to val. The loop uses val to divide a given value to translate it
21st May 2020, 10:20 AM
Da2
Da2 - avatar