From f807d7ac0283a4422aa67fc79a41b8889035b442 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 2 Mar 2025 04:29:04 -0600 Subject: [PATCH] adding valid palindrome --- .../valid_palindrome.py | 22 +++++++++++++++++++ .../test_find_valid_palindrome_round_14.py | 15 +++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_14/valid_palindrome.py create mode 100644 tests/test_150_questions_round_14/test_find_valid_palindrome_round_14.py diff --git a/src/my_project/interviews/top_150_questions_round_14/valid_palindrome.py b/src/my_project/interviews/top_150_questions_round_14/valid_palindrome.py new file mode 100644 index 00000000..8e84d73d --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_14/valid_palindrome.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # transform to lower case + s = s.lower() + + # remove non-alphanumerical value in the string + 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 diff --git a/tests/test_150_questions_round_14/test_find_valid_palindrome_round_14.py b/tests/test_150_questions_round_14/test_find_valid_palindrome_round_14.py new file mode 100644 index 00000000..5fc3f199 --- /dev/null +++ b/tests/test_150_questions_round_14/test_find_valid_palindrome_round_14.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_14\ +.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