-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_palindrome.py
More file actions
executable file
·164 lines (126 loc) · 4.88 KB
/
test_palindrome.py
File metadata and controls
executable file
·164 lines (126 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import unittest
from random import choice, randint
from string import ascii_letters
from typing import Union
from parameterized import parameterized
from algorithms.two_pointers.palindrome import (
is_palindrome,
smallest_palindrome,
largest_palindrome,
is_palindrome_number,
is_palindrome_number_2,
is_valid_palindrome_with_one_char_removal,
)
from algorithms.two_pointers.palindrome.longest_palindrome import longest_palindrome
class LongestPalindromeTests(unittest.TestCase):
def single_letter(self):
self.assertEqual(longest_palindrome("a"), 1)
def test_double_letter(self):
self.assertEqual(longest_palindrome("aa"), 2)
def test_3(self):
self.assertEqual(longest_palindrome("baa"), 2)
def test_4(self):
self.assertEqual(longest_palindrome("aab"), 2)
def test_5(self):
self.assertEqual(longest_palindrome("abcdefghba"), 1)
def test_6(self):
self.assertEqual(longest_palindrome("baablkj12345432133d"), 9)
class IsPalindromeTests(unittest.TestCase):
def test_7(self):
self.assertEqual(is_palindrome("anna"), True)
def test_8(self):
self.assertEqual(is_palindrome("walter"), False)
def test_9(self):
self.assertEqual(is_palindrome("12321"), True)
def test_10(self):
self.assertEqual(is_palindrome("123456"), False)
def test_11(self):
word = "Was it a cat I saw?"
actual = is_palindrome(word)
self.assertTrue(actual)
def test_12(self):
word = "Never odd or even"
actual = is_palindrome(word)
self.assertTrue(actual)
def test_13(self):
word = "radar"
actual = is_palindrome(word)
self.assertTrue(actual)
def test_14(self):
word = "Live on time, emit no evil"
actual = is_palindrome(word)
self.assertTrue(actual)
@staticmethod
def generate_test_case() -> Union[str, int]:
if randint(0, 1000):
test_case = randint(0, 10000000)
else:
test_case = "".join(choice(ascii_letters) for _ in range(randint(1, 1000)))
if randint(0, 1000):
test_case = "%s%s" % (test_case, test_case[::-1])
return test_case
@staticmethod
def reference(s):
return str(s) == str(s)[::-1]
def test_15(self):
for _ in range(100):
test_case = self.generate_test_case()
self.assertEqual(is_palindrome(str(test_case)), self.reference(test_case))
IS_PALINDROME_TEST_CASES = [
(353, True),
(-353, False),
(90, False),
(12321, True),
(10101, True),
(1000021, False),
(0, True),
(2147447412, True),
]
class IsPalindromeNumber(unittest.TestCase):
@parameterized.expand(IS_PALINDROME_TEST_CASES)
def test_is_palindrome_number(self, x: int, expected: bool):
actual = is_palindrome_number(x)
self.assertEqual(actual, expected)
@parameterized.expand(IS_PALINDROME_TEST_CASES)
def test_is_palindrome_number_2(self, x: int, expected: bool):
actual = is_palindrome_number_2(x)
self.assertEqual(actual, expected)
class SmallestPalindromeTests(unittest.TestCase):
def test_smallest_palindrome_from_double_digit_factors(self):
value, factors = smallest_palindrome(max_factor=99, min_factor=10)
self.assertEqual(121, value)
self.assertEqual({11}, set(factors))
def test_smallest_palindrome_from_triple_digit_factors(self):
value, factors = smallest_palindrome(max_factor=999, min_factor=100)
self.assertEqual(10201, value)
self.assertEqual({101, 101}, set(factors))
class LargestPalindromeTests(unittest.TestCase):
def test_largest_palindrome_from_single_digit_factors(self):
value, factors = largest_palindrome(max_factor=9)
self.assertEqual(9, value)
self.assertIn(set(factors), [{1, 9}, {3, 3}])
def test_largest_palindrome_from_double_digit_factors(self):
value, factors = largest_palindrome(max_factor=99, min_factor=10)
self.assertEqual(9009, value)
self.assertEqual({91, 99}, set(factors))
def test_largest_palindrome_from_triple_digit_factors(self):
value, factors = largest_palindrome(max_factor=999, min_factor=100)
self.assertEqual(906609, value)
self.assertEqual({913, 993}, set(factors))
IS_PALINDROME_WITH_ONE_CHAR_REMOVAL_TEST_CASES = [
("aba", True),
("abca", True),
("abc", False),
("abccbxa", True),
("madame", True),
("dead", True),
("tebbem", False),
("eeccccbebaeeabebccceea", False),
]
class IsPalindromeWithOneCharRemoval(unittest.TestCase):
@parameterized.expand(IS_PALINDROME_WITH_ONE_CHAR_REMOVAL_TEST_CASES)
def test_is_palindrome_with_one_char_removal(self, s: str, expected: bool):
actual = is_valid_palindrome_with_one_char_removal(s)
self.assertEqual(expected, actual)
if __name__ == "__main__":
unittest.main()