-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q18_pattern_matching.py
64 lines (47 loc) · 1.88 KB
/
Q18_pattern_matching.py
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
import unittest
def pattern_matching(pattern, value):
if len(value) == 0:
return True
if len(pattern) == 0:
return False
len_value = len(value)
pattern = normalize_pattern(pattern)
count_a = pattern.count("a")
count_b = pattern.count("b")
for i in range(len_value + 1):
substring_1 = value[:i]
len_all_b = len_value - (count_a * i)
if count_b and len_all_b % count_b == 0:
len_b = int(len_all_b / count_b)
first_b_idx = i * pattern.index("b")
substring_2 = value[first_b_idx : first_b_idx + len_b]
if build_pattern(substring_1, substring_2, pattern) == value:
return True
elif not count_b:
if build_pattern(substring_1, "", pattern) == value:
return True
return False
def get_string(a, b, char):
return a if char == "a" else b
def build_pattern(a, b, pattern):
return "".join([get_string(a, b, char) for char in pattern])
def flip(c):
return "a" if c == "b" else "b"
def normalize_pattern(pattern):
if pattern[0] == "a":
return pattern
return "".join([flip(c) for c in pattern])
class Test(unittest.TestCase):
def test_normalize_pattern(self):
self.assertEqual("aba", normalize_pattern("bab"))
self.assertEqual("abab", normalize_pattern("baba"))
def test_pattern_matching(self):
s = "catcatgocatgo"
self.assertEqual(True, pattern_matching("aabab", s))
self.assertEqual(True, pattern_matching("ab", s))
self.assertEqual(True, pattern_matching("ba", s))
self.assertEqual(False, pattern_matching("abab", s))
self.assertEqual(True, pattern_matching("a", s))
self.assertEqual(False, pattern_matching("aaa", s))
self.assertEqual(True, pattern_matching("a", ""))
self.assertEqual(False, pattern_matching("", "cat"))