From f5fe1617ba3839756db50ecd73d5575503641fa7 Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 11 Jun 2025 04:32:28 -0600 Subject: [PATCH] adding algo --- .../round_3/valid_palindrome.py | 18 ++++++++++++++++ .../roman_to_integer.py | 21 +++++++++++++++++++ .../test_roman_to_integer_round_17.py | 19 +++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/my_project/interviews/interview_amazon/round_3/valid_palindrome.py create mode 100644 src/my_project/interviews/top_150_questions_round_17/roman_to_integer.py create mode 100644 tests/test_150_questions_round_17/test_roman_to_integer_round_17.py diff --git a/src/my_project/interviews/interview_amazon/round_3/valid_palindrome.py b/src/my_project/interviews/interview_amazon/round_3/valid_palindrome.py new file mode 100644 index 00000000..c9c88118 --- /dev/null +++ b/src/my_project/interviews/interview_amazon/round_3/valid_palindrome.py @@ -0,0 +1,18 @@ +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # to lowercase + s = s.lower() + + # remove non-alphanumerical characters + s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) + + len_s = len(s) + + for i in range(len_s//2): + if s[i] != s[len_s - 1 -i]: + return False + + return True \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_17/roman_to_integer.py b/src/my_project/interviews/top_150_questions_round_17/roman_to_integer.py new file mode 100644 index 00000000..9c96b427 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_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 diff --git a/tests/test_150_questions_round_17/test_roman_to_integer_round_17.py b/tests/test_150_questions_round_17/test_roman_to_integer_round_17.py new file mode 100644 index 00000000..7438488a --- /dev/null +++ b/tests/test_150_questions_round_17/test_roman_to_integer_round_17.py @@ -0,0 +1,19 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_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) + +