Skip to content

Instantly share code, notes, and snippets.

@vandersonmota
Created April 29, 2016 13:44
Show Gist options
  • Save vandersonmota/fbb4735a003b2fc676f9796c5eaf4ff1 to your computer and use it in GitHub Desktop.
Save vandersonmota/fbb4735a003b2fc676f9796c5eaf4ff1 to your computer and use it in GitHub Desktop.
"""
Fui num dia do Grupo de Erlang, acabamos fazendo um dojo.
Créditos da solução - Seu Carlos Flores =)
"""
# versão 1
arabicos = [1000, 900,500,400,100,90,50,40,10,9,5,4,1]
romanos = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV', 'I']
def converter(num):
convertido = ''
indice = 0
while num > 0:
qtd = num/arabicos[indice]
convertido += qtd*romanos[indice]
num -= qtd * arabicos[indice]
indice += 1
return convertido
# versão 2
arabicos = [1000, 900,500,400,100,90,50,40,10,9,5,4,1]
romanos = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV', 'I']
def converter(num):
convertido = ''
for indice in range(0, len(arabicos)):
qtd = num/arabicos[indice]
convertido += qtd*romanos[indice]
num -= qtd * arabicos[indice]
return convertido
#versão 3
arabicos = [1000, 900,500,400,100,90,50,40,10,9,5,4,1]
romanos = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV', 'I']
def converter(num):
convertido = ''
for indice, arabico in enumerate(arabicos):
qtd = num/arabico
convertido += qtd*romanos[indice]
num -= qtd * arabico
return convertido
#versão final
arabicos = [1000, 900,500,400,100,90,50,40,10,9,5,4,1]
romanos = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV', 'I']
def converter(num):
convertido = ''
for indice, arabico in enumerate(arabicos):
convertido += (num/arabico)*romanos[indice]
num %= arabico
return convertido
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment