Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 76 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
Rileyy: I need help
3 minutes ago 5 Replies 0 Medals
YoungBlood: pfp wonu2019t let me switch
41 minutes ago 3 Replies 0 Medals
Austinsbabygirl4life: Do you think guys would survive on periods?
9 hours ago 22 Replies 2 Medals
alyviahwillis: if anyone has any questions feel free to DM me... I don't mind at all.
11 hours ago 0 Replies 0 Medals
Rosee5656: how do I level up!??!? >,
10 hours ago 14 Replies 4 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!