From 38490799e76ac807466c7f78af7adbaeada971a9 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 11 Dec 2025 04:07:22 -0600 Subject: [PATCH] adding algo --- .../ex_25_valid_palindrome.py | 21 +++++++++++++++++++ .../ex_26_is_subsequence.py | 18 ++++++++++++++++ .../test_25_valid_palindrome_round_22.py | 15 +++++++++++++ .../test_26_is_subsequence_round_22.py | 15 +++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_25_valid_palindrome.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_26_is_subsequence.py create mode 100644 tests/test_150_questions_round_22/test_25_valid_palindrome_round_22.py create mode 100644 tests/test_150_questions_round_22/test_26_is_subsequence_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_25_valid_palindrome.py b/src/my_project/interviews/top_150_questions_round_22/ex_25_valid_palindrome.py new file mode 100644 index 00000000..75bda153 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_25_valid_palindrome.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # To lowercase + s = s.lower() + + # Remove non-alphanumeric characters + s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) + + # Determine if it is palindrome or not + 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_22/ex_26_is_subsequence.py b/src/my_project/interviews/top_150_questions_round_22/ex_26_is_subsequence.py new file mode 100644 index 00000000..d7dc7a3b --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_26_is_subsequence.py @@ -0,0 +1,18 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + + l1, l2 = 0, 0 + + len_s, len_t = len(s), len(t) + + while l1 < len_s and l2 < len_t: + + if s[l1] == t[l2]: + l1 += 1 + + l2 += 1 + + return l1 == len_s diff --git a/tests/test_150_questions_round_22/test_25_valid_palindrome_round_22.py b/tests/test_150_questions_round_22/test_25_valid_palindrome_round_22.py new file mode 100644 index 00000000..9329f1b5 --- /dev/null +++ b/tests/test_150_questions_round_22/test_25_valid_palindrome_round_22.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_25_valid_palindrome import Solution + +class ValidPalindromeTestCase(unittest.TestCase): + + def test_is_valid_palindrome(self): + solution = Solution() + output = solution.isPalindrome(s="A man, a plan, a canal: Panama") + self.assertTrue(output) + + def test_is_no_valid_palindrome(self): + solution = Solution() + output = solution.isPalindrome(s="race a car") + self.assertFalse(output) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_26_is_subsequence_round_22.py b/tests/test_150_questions_round_22/test_26_is_subsequence_round_22.py new file mode 100644 index 00000000..c3025751 --- /dev/null +++ b/tests/test_150_questions_round_22/test_26_is_subsequence_round_22.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_26_is_subsequence import Solution + +class IsSubsequenceTestCase(unittest.TestCase): + + def test_is_subsequence(self): + solution = Solution() + output = solution.isSubsequence(s="abc", t="ahbgdc") + self.assertTrue(output) + + def test_is_no_subsequence(self): + solution = Solution() + output = solution.isSubsequence(s="axc", t="ahbgdc") + self.assertFalse(output)