Skip to content

Instantly share code, notes, and snippets.

@joaopaulovieira
Created July 20, 2018 17:34
Show Gist options
  • Select an option

  • Save joaopaulovieira/8e3391affd827f1fbb46c1601bde5ad3 to your computer and use it in GitHub Desktop.

Select an option

Save joaopaulovieira/8e3391affd827f1fbb46c1601bde5ad3 to your computer and use it in GitHub Desktop.
Convert decimals to roman algarism's.
def int_to_roman(num):
_values = [
1000000, 900000, 500000, 400000, 100000, 90000, 50000, 40000, 10000, 9000, 5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
_strings = [
'M', 'C', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
result = ""
decimal = num
while decimal > 0:
for algarism in range(len(_values)):
if decimal >= _values[algarism]:
if _values[algarism] > 1000:
result += u'\u0304'.join(list(_strings[algarism])) + u'\u0304'
else:
result += _strings[algarism]
decimal -= _values[algarism]
break
return result
print(int_to_roman(2000017))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment