From 699518478c92425b5570bb71efd9059be26bab5b Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 7 Dec 2025 04:06:03 -0600 Subject: [PATCH] adding algo --- .../ex_17_roman_to_integer.py | 21 +++++++++++++++ .../ex_18_integer_to_roman.py | 26 +++++++++++++++++++ .../test_17_roman_to_integer_round_22.py | 17 ++++++++++++ .../test_18_integer_to_roman_round_22.py | 17 ++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_17_roman_to_integer.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_18_integer_to_roman.py create mode 100644 tests/test_150_questions_round_22/test_17_roman_to_integer_round_22.py create mode 100644 tests/test_150_questions_round_22/test_18_integer_to_roman_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_17_roman_to_integer.py b/src/my_project/interviews/top_150_questions_round_22/ex_17_roman_to_integer.py new file mode 100644 index 00000000..34c055b1 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_17_roman_to_integer.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def romanToInt(self, s: str) -> int: + + roman_to_dict = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} + + len_s = len(s) + + answer = roman_to_dict[s[len_s - 1]] + + for i in range(len_s - 1): + + if roman_to_dict[s[len_s - 2 - i]] \ + < roman_to_dict[s[len_s - 1 - i]]: + answer -= roman_to_dict[s[len_s - 2 - i]] + else: + answer += roman_to_dict[s[len_s - 2 - i]] + + return answer \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_18_integer_to_roman.py b/src/my_project/interviews/top_150_questions_round_22/ex_18_integer_to_roman.py new file mode 100644 index 00000000..5173c4d0 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_18_integer_to_roman.py @@ -0,0 +1,26 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def intToRoman(self, num: int) -> str: + # Creating Dictionary for Lookup + num_map = { + 1: "I", + 5: "V", 4: "IV", + 10: "X", 9: "IX", + 50: "L", 40: "XL", + 100: "C", 90: "XC", + 500: "D", 400: "CD", + 1000: "M", 900: "CM", + } + + # Result Variable + r = '' + + + for n in [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]: + # If n in list then add the roman value to result variable + while n <= num: + r += num_map[n] + num-=n + return r \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_17_roman_to_integer_round_22.py b/tests/test_150_questions_round_22/test_17_roman_to_integer_round_22.py new file mode 100644 index 00000000..bf3c8278 --- /dev/null +++ b/tests/test_150_questions_round_22/test_17_roman_to_integer_round_22.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_17_roman_to_integer import Solution + +class RomanToIntegerTestCase(unittest.TestCase): + + def test_patter_one(self): + solution = Solution() + output = solution.romanToInt(s='VII') + target = 7 + self.assertEqual(output, target) + + def test_patter_two(self): + solution = Solution() + output = solution.romanToInt(s='IX') + target = 9 + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_18_integer_to_roman_round_22.py b/tests/test_150_questions_round_22/test_18_integer_to_roman_round_22.py new file mode 100644 index 00000000..1d96734e --- /dev/null +++ b/tests/test_150_questions_round_22/test_18_integer_to_roman_round_22.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_18_integer_to_roman import Solution + +class IntegerToRomanTestCase(unittest.TestCase): + + def test_patter_one(self): + solution = Solution() + output = solution.intToRoman(num = 3749) + target = "MMMDCCXLIX" + self.assertEqual(output, target) + + def test_patter_two(self): + solution = Solution() + output = solution.intToRoman(num = 58) + target = "LVIII" + self.assertEqual(output, target) \ No newline at end of file