-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognizePattern.py
More file actions
84 lines (61 loc) · 1.73 KB
/
recognizePattern.py
File metadata and controls
84 lines (61 loc) · 1.73 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
# https://leetcode.com/discuss/interview-question/928806/
def get_count(sin, pattern):
"""
Runs through 1 section of the string to get count of pattern
"""
pattern_len = len(pattern)
s = 0
d = pattern_len
count = 0
# print(sin, pattern)
while d <= len(sin):
cut = sin[s:d]
# print(cut)
if cut == pattern:
count += 1
s += 1
d += 1
else:
s += 1
d += 1
return count
def recognize_pattern(input):
"""
Returns final result, with counts of string and final sum
"""
# print(input.split(";"))
first_split = input.split(";")
pattern = first_split[0]
strs = first_split[1].split("|")
res_list = []
print(pattern, strs)
if len(pattern) == 0:
res_list = [0]*len(strs)
else:
for chars in strs:
res_list.append(get_count(chars, pattern))
addedvals = sum(res_list)
res_list.append(addedvals)
res_str = "|".join([str(i) for i in res_list])
return res_str
if __name__ == '__main__':
testcases = [
"bc;bcdefbcbebc|abcdebcfgsdf|cbdbesfbcy|1bcdef23423bc32",
"aa;aaaakjlhaa|aaadsaaa|easaaad|sa",
"b;bcdefbcbebc|abcdebcfgsdf|cbdbesfbcy|1bcdef23423bc32",
";bcdefbcbebc|abcdebcfgsdf|cbdbesfbcy|1bcdef23423bc32"
]
answers = [
"3|2|1|2|8",
"4|4|2|0|10",
"4|2|3|2|11",
"0|0|0|0|0"
]
res1 = recognize_pattern(testcases[0])
print(res1, answers[0])
res2 = recognize_pattern(testcases[1])
print(res2, answers[1])
res3 = recognize_pattern(testcases[2])
print(res3, answers[2])
res4 = recognize_pattern(testcases[3])
print(res4, answers[3])