Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit dfc6c4b

Browse files
committed
adds python valid palindrome
1 parent 9256e6f commit dfc6c4b

File tree

6 files changed

+104
-71
lines changed

6 files changed

+104
-71
lines changed

python/count_primes/count_primes.py

-71
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
.venv
3+
questions.md
4+
__pycache__
5+
.vscode
6+
glue.sh
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
FROM python:3.8-slim-buster
3+
4+
WORKDIR /valid_palindrome
5+
6+
COPY requirements.txt requirements.txt
7+
8+
RUN pip3 install -r requirements.txt
9+
10+
COPY . .
11+
12+
CMD ["pytest"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==6.2.3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
import pytest
3+
from valid_palindrome import Solution
4+
5+
@pytest.fixture
6+
def sol() -> Solution:
7+
return Solution()
8+
9+
def test_one_char_true(
10+
sol: Solution,
11+
):
12+
assert sol.is_palindrome("Q") == True
13+
14+
def test_hannah_true(
15+
sol: Solution,
16+
):
17+
assert sol.is_palindrome("Hannah") == True
18+
19+
def test_panama_canal_string_true(
20+
sol: Solution,
21+
):
22+
s = "A man, a plan, a canal: Panama"
23+
assert sol.is_palindrome(s) == True
24+
25+
def test_matrix_false(
26+
sol: Solution,
27+
):
28+
assert sol.is_palindrome("matrix") == False
29+
30+
def test_90509_true(
31+
sol: Solution,
32+
):
33+
assert sol.is_palindrome("90509") == True
34+
35+
def test_madam_im_adam_true(
36+
sol: Solution,
37+
):
38+
assert sol.is_palindrome("Madam I'm Adam") == True
39+
40+
def test_greek_thing_true(
41+
sol: Solution,
42+
):
43+
assert sol.is_palindrome("ΝΙΨΟΝ ΑΝΟΜΗΜΑΤΑ ΜΗ ΜΟΝΑΝ ΟΨΙΝ") == True
44+
45+
def test_mr_owl_true(
46+
sol: Solution,
47+
):
48+
assert sol.is_palindrome("Mr. Owl ate my metal worm") == True
49+
50+
def test_mr_bowl_false(
51+
sol: Solution,
52+
):
53+
assert sol.is_palindrome("Mr. Bowl ate my metal worm") == False
54+
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
"""
3+
Given a string s, determine if it is a palindrome, considering only
4+
alphanumeric characters and ignoring cases.
5+
6+
Example 1:
7+
Input: s = "A man, a plan, a canal: Panama"
8+
Output: true
9+
Explanation: "amanaplanacanalpanama" is a palindrome.
10+
11+
Example 2:
12+
Input: s = "race a car"
13+
Output: false
14+
Explanation: "raceacar" is not a palindrome.
15+
16+
Constraints:
17+
18+
1 <= s.length <= 2 * 105
19+
s consists only of printable ASCII characters.
20+
"""
21+
22+
class Solution:
23+
24+
def is_palindrome(
25+
self,
26+
s: str,
27+
) -> bool:
28+
simplify = lambda char: char.isalnum()
29+
new_str = "".join(tuple(filter(simplify, s.lower())))
30+
return new_str[::-1] == new_str

0 commit comments

Comments
 (0)