forked from Jules-Boogie/controllingProgramFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.py
More file actions
39 lines (31 loc) · 1.07 KB
/
Copy pathfunc.py
File metadata and controls
39 lines (31 loc) · 1.07 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
"""
A function to find all instances of a substring.
This function is not unlike a 'find-all' option that you might see in a text editor.
Author: Juliet George
Date: 8/5/2020
"""
import introcs
def findall(text,sub):
"""
Returns the tuple of all positions of substring sub in text.
If sub does not appears anywhere in text, this function returns the empty tuple ().
Examples:
findall('how now brown cow','ow') returns (1, 5, 10, 15)
findall('how now brown cow','cat') returns ()
findall('jeeepeeer','ee') returns (1,2,5,6)
Parameter text: The text to search
Precondition: text is a string
Parameter sub: The substring to search for
Precondition: sub is a nonempty string
"""
result = ()
start = 0
l = len(sub)
while start < len(text):
print(str(start) + "first")
if (sub in text) and (text.find(sub,start,start+l+1) != -1):
start = (text.find(sub,start,start+l+1))
result = result + (start,)
start = start + 1
print(str(start) + "end")
return result