This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_debug2.py
79 lines (63 loc) · 2.42 KB
/
test_debug2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
This program includes a function to find and return
the length of a longest common consecutive substring
shared by two strings.
If the strings have nothing in common, the longest
common consecutive substring has length 0.
For example, if string1 = "established", and
string2 = "ballistic", the function returns the
integer 3, as the string "lis" is the longest common
consecutive substring shared by the two strings.
The function tests all possible pairs of starting points
(starting point for string 1, and starting point for string 2),
and measures the match from each. It keeps
track of the length of the best match seen,
and returns that when finished checking all starting points.
The function has bug(s).
There are no tests (yet).
Your job is to
1) include in this program a sufficient
suite of pass/fail tests to thoroughly
test the function and expose all error(s).
2) Generate a screenshot that
demonstrates your use of a debugger
to step through the function. Specifically it should
illustrate the execution point of a test at
which the function makes (or is about to make)
a mistake.
3) fix the code and document your fix(es).
Include additional tests if you feel it
necessary to thoroughly test the function.
You will submit your updated version of this
file (along with a separate document containing
the screenshot and answered questions).
File: test_debug2.py
Author: CS @ RIT
Author: (add your name here)
"""
def match(string1, string2):
"""
Identifies and returns the length of a longest
common consecutive substring shared
by the two input strings.
:param string1: The first string.
:param string2: The second string.
:return: length of a longest shared consecutive string.
"""
best_length = 0
# for all possible string1 start points
for idx1 in range(len(string1)-1):
# for all possible string2 start points
for idx2 in range(len(string2)-1):
# check if these characters match
if string1[idx1] == string2[idx2]:
this_match_count = 1
# see how long the match continues
while string1[idx1 + this_match_count] == \
string2[idx2 + this_match_count]:
this_match_count += 1
# compare to best so far
if this_match_count > best_length:
best_length = this_match_count
# now return the result
return best_length