-
Notifications
You must be signed in to change notification settings - Fork 0
/
inf_sequence_test.py
66 lines (51 loc) · 1.53 KB
/
inf_sequence_test.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
65
66
import unittest
from inf_sequence import kmp_get_pos
from random import randrange
def bruteforce_get_pos(a):
"""
Generate a sequence of positive integers,
and use brute-force algorithm
to find first substring position in this sequence.
Args:
a (str): target substring A.
Returns:
seq.index(a)+1 (int): first ocurrence of substring A
in sequence S, starting from 1.
"""
found = False
n = 1 # sequence start
seq = ""
while not found:
seq += str(n)
if a in seq:
found = True
return seq.index(a) + 1
n += 1
def gen_substring(n):
# Returns string of n random digits in range 0-9
substring = ""
for i in range(n):
substring += str(randrange(10))
return substring
class PosInSeqTests(unittest.TestCase):
"""
Unit-tests to cover provided example cases data
and check improved algorithm correctness.
"""
def test6789_from_example(self):
s = "6789"
self.assertEqual(kmp_get_pos(s), 6)
def test111_from_example(self):
s = "111"
self.assertEqual(kmp_get_pos(s), 12)
def test_kmp(self):
for i in range(100):
# Generate random substring
n = randrange(1, 5)
substring = gen_substring(n)
# Compare bruteforce vs KMP solution
bruteforce = bruteforce_get_pos(substring)
kmp = kmp_get_pos(substring)
self.assertEqual(bruteforce, kmp)
if __name__ == '__main__':
unittest.main()