Skip to content

Commit

Permalink
solution to roman_to_integer
Browse files Browse the repository at this point in the history
  • Loading branch information
welli7ngton committed May 28, 2024
1 parent 98b63ff commit 334fe39
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Algoritmos/binary_search/roman_to_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def romanToInt(s: str) -> int:
hashNums = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}

prev = 0
total = 0

for i in range(len(s) - 1, -1, -1):
actual = hashNums.get(s[i])
if actual >= prev:
total += actual
prev = actual
else:
total -= actual
return total


if __name__ == '__main__':
print(romanToInt('XIV'))
print(romanToInt('XX'))
print(romanToInt('XIX'))

0 comments on commit 334fe39

Please sign in to comment.