Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversione numero romano in numero arabo - Angelo Spadea #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,25 @@ def convert_roman_to_arabic(roman: str) -> str:
:param roman: string to be converted
:return: converted string
"""
# inserisci qui il tuo codice
pass

#creo un dizionario contenente i valori associati ai numeri romani
dict={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
#controllo che l'ultimo valore sia un valore valido se lo è lo uso come valore di partenza
if dict.get(roman[len(roman)-1],-1)==-1:
raise ValueError
else:
c=dict.get(roman[len(roman)-1])

for i in range(len(roman)-1):
#controllo che ogni elemento della stringa sia valido
b=dict.get(roman[i],-1)
h=dict.get(roman[i+1],-1)
if b==-1 or h==-1:
raise ValueError
else:
#se il primo valore (b) è maggiore del secondo (h) allora li sommo altrimenti lo sottraggo
if b>=h:
c+=b
else:
c-=b
return str(c)