Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 37 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
Nina001: I need tips on how to let go and not hurt yourself
9 seconds ago 24 Replies 0 Medals
Taku12: Any other tips for my art project?
6 minutes ago 5 Replies 0 Medals
mintyy: where and how can i get sub?
42 minutes ago 15 Replies 2 Medals
EdwinJsHispanic: THIS IS FOR EXOTIC
2 hours ago 20 Replies 6 Medals
breiadabrattzz: I need help to make a theme -
18 hours ago 14 Replies 3 Medals
curriful: Black QuestionCove Theme (WIP)
2 hours ago 38 Replies 3 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!