Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 13 Online
OpenStudy (anonymous):

Hello! I am working on problem 2, where I need to make a function that checks strings and returns a tuple of the indexes that match the "key". Here's my code: def subStringMatchExact(target, key): match = [] index = 0 for i in target: if i == key: match.append(index) index += 1 else: index += 1 tuple(match) #convert list to tuple return match This code works fine so long as the key is only a single character. How can I modify this so that the function will return matches for longer strings? Thanks!

OpenStudy (anonymous):

Check out the find() function

OpenStudy (rsmith6559):

for i in target: if i == key: Just for fun, put "print i" in between these lines and run it with a not too long target.

OpenStudy (anonymous):

As suggested, you can use a built-in function or you can also consider taking every possible bit of the target string where the bit's length equals the length of the key and testing for equality. One such bit would be: target[ i + len(key) ]

OpenStudy (lopus):

just work with tuple: tuple=() tuple= tuple[:count]+(i,) for example cont= 1 i=4 ("",4,)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Arriyanalol: bro how
3 hours ago 0 Replies 0 Medals
Arriyanalol: cant wait for the new bluey movie in 2027
6 hours ago 11 Replies 1 Medal
MrsTooTac: naa shorty blew me askin me if i eat ahh wdf
1 day ago 0 Replies 0 Medals
Arriyanalol: NOAH STINKS
1 day ago 10 Replies 1 Medal
Arriyanalol: this app is broken
1 day ago 1 Reply 0 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!