From 9aaef5cea0fe4d2a9ecf1b7ad663b8bc27091071 Mon Sep 17 00:00:00 2001 From: ahammed hinas <82311707+hinasahammed@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:03:02 +0530 Subject: [PATCH] Create roman_to_integer.py --- Python-for-beginners/roman_to_integer.py | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Python-for-beginners/roman_to_integer.py diff --git a/Python-for-beginners/roman_to_integer.py b/Python-for-beginners/roman_to_integer.py new file mode 100644 index 0000000..85e89e6 --- /dev/null +++ b/Python-for-beginners/roman_to_integer.py @@ -0,0 +1,26 @@ +class Solution: + def romanToInt(self, s): + roman_dict = { + "I": 1, + "V": 5, + "X": 10, + "L": 50, + "C": 100, + "D": 500, + "M": 1000, + } + total = 0 + for i in range(len(s)): + if i + 1 < len(s) and roman_dict[s[i]] < roman_dict[s[i + 1]]: + total -= roman_dict[s[i]] + else: + total += roman_dict[s[i]] + + return total + + +solution = Solution() + +roman_string = input("Enter a Roman numeral: ") +result = solution.romanToInt(roman_string) +print(f"The integer value of the Roman numeral '{roman_string}' is: {result}")