Very new to Python here, and struggling. Any help is appreciated! Confession: this is obviously a request for help with homework, but my course ends tomorrow and the instructor takes too long to return a message, so I'm afraid if I wait I won't get this finished in time.
I'm using a learning module from Cornell University called introcs. It's documented here: http://cs1110.cs.cornell.edu/docs/index.html
I am trying to write a function that returns a tuple of all indexes of a substring within a string. I feel like I'm pretty close, but just not quite getting it. Here's my code:
import introcs
def findall(text,sub):
result = ()
x = 0
pos = introcs.find_str(text,sub,x)
for i in range(len(text)):
if introcs.find_str(text,sub,x) != -1:
result = result + (introcs.find_str(text,sub,x), )
x = x + 1 + introcs.find_str(text,sub,x)
return result
On the call findall('how now brown cow', 'ow') I want it to return (1, 5, 10, 15) but instead it lops off the last result and returns (1, 5, 10) instead.
Any pointers would be really appreciated!
find_str()from a module calledintrocs, which isn't a common module and you didn't provide the code. What doesfind_str()do exactly? Why even use it?introcs.find_str()is the same as the built-instr.find()method.