diff --git a/src/my_project/interviews/top_150_questions_round_23/ex_17_roman_to_integer.py b/src/my_project/interviews/top_150_questions_round_23/ex_17_roman_to_integer.py new file mode 100644 index 00000000..34c055b1 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_23/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_typescript/top_150_questions_round_23/ex_17_roman_to_integer.ts b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_17_roman_to_integer.ts new file mode 100644 index 00000000..ed971786 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_17_roman_to_integer.ts @@ -0,0 +1,19 @@ +function romanToInt(s: string): number { + + let roman_to_int_obj: {[key:string]:number} = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} + + let lenS: number = s.length + let solution: number = roman_to_int_obj[s[lenS-1]] + + for (let i = 0; i < lenS-1;i++){ + if (roman_to_int_obj[s[lenS-2-i]] < roman_to_int_obj[s[lenS-1-i]]) { + solution -= roman_to_int_obj[s[lenS-2-i]] + } + else{ + solution += roman_to_int_obj[s[lenS-2-i]] + } + } + + return solution; + +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_23/test_17_roman_to_integer_round_23.py b/tests/test_150_questions_round_23/test_17_roman_to_integer_round_23.py new file mode 100644 index 00000000..f58bddf4 --- /dev/null +++ b/tests/test_150_questions_round_23/test_17_roman_to_integer_round_23.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_23\ +.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