From 334fe3946b18e55b4567139227add7c5081b45f9 Mon Sep 17 00:00:00 2001 From: welli7ngton Date: Tue, 28 May 2024 18:40:46 -0300 Subject: [PATCH] solution to roman_to_integer --- Algoritmos/binary_search/roman_to_integer.py | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Algoritmos/binary_search/roman_to_integer.py diff --git a/Algoritmos/binary_search/roman_to_integer.py b/Algoritmos/binary_search/roman_to_integer.py new file mode 100644 index 0000000..9bdfae7 --- /dev/null +++ b/Algoritmos/binary_search/roman_to_integer.py @@ -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'))