From e022cc56c2ef89a635a913bfc9cc3d97113985c6 Mon Sep 17 00:00:00 2001 From: Mushfiqur719 Date: Tue, 1 Mar 2022 00:29:40 +0600 Subject: [PATCH] Added python solution for 13. Roman to Integer.(#101) --- Algorithms/Easy/13_RomanToInteger/Solution.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Algorithms/Easy/13_RomanToInteger/Solution.py diff --git a/Algorithms/Easy/13_RomanToInteger/Solution.py b/Algorithms/Easy/13_RomanToInteger/Solution.py new file mode 100644 index 0000000..4630011 --- /dev/null +++ b/Algorithms/Easy/13_RomanToInteger/Solution.py @@ -0,0 +1,21 @@ +class Solution: + def romanToInt(self, s: str) -> int: + # Integer values of the roman numbers + values = { "I": 1 , "V": 5 , "X": 10 , "L": 50 , "C": 100 , "D": 500 , "M": 1000 } + + num = 0 + + left = 0 + + size = len(s) + + for i in range(size-1, -1, -1): + current = values[s[i]] + + if (current >= left): + num += current + else: + num -= current + left = current + + return num