-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromanos.py
48 lines (36 loc) · 1.35 KB
/
romanos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
valores = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
valores5 = {'V':5, 'L':50, 'D':500}
simbolosOrdenados = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
def romano_a_arabigo(numRomano):
numArabigo = 0
numRepes = 1
ultimoCaracter = ''
for letra in numRomano:
if letra in valores:
numArabigo += valores[letra]
if ultimoCaracter == '':
pass
elif valores[ultimoCaracter] > valores[letra]:
numRepes = 1
elif valores[ultimoCaracter] == valores[letra]:
numRepes += 1
if letra in valores5:
return 0
if numRepes > 3:
return 0
elif valores[ultimoCaracter] < valores[letra]:
if numRepes > 1:
return 0
if ultimoCaracter in valores5:
return 0
distancia = simbolosOrdenados.index(letra) - simbolosOrdenados.index(ultimoCaracter)
if distancia > 2:
return 0
numArabigo -= valores[ultimoCaracter]*2
numRepes = 1
elif ultimoCaracter == ')':
numArabigo = numArabigo * 1000
else:
return 0
ultimoCaracter = letra
return numArabigo