I am trying to solve for the below function. I am getting my expected empty tuple when the sub is not found in the given string 'text'. However, I am having a problem incrementing during the for-loop to find the subsequent positions of the same sub in the remainder of the string 'text'.
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
"""
tup = ()
for pos in range(len(text)):
if sub not in text:
tup = ()
else:
pos1 = introcs.find_str(text,sub)
tup = tup + (pos1,)
# increment to the next pos and look for the sub again, not sure how to
# move beyond the first instance of the substring in the text ???
pos1 = pos1 + 1
return tup