Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 12 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
laylasnii13: Who wanna write or make a song with me???
1 hour ago 0 Replies 0 Medals
kaelynw: art igg
55 minutes ago 9 Replies 1 Medal
XShawtyX: Art
19 hours ago 6 Replies 0 Medals
Nina001: teach me how to draw or just tell me the basics
21 hours ago 2 Replies 1 Medal
XShawtyX: We doing another drawing gimme ideas to add to this
22 hours ago 9 Replies 1 Medal
RAVEN69: What is x 3+y 3+z 3=k
1 day ago 20 Replies 1 Medal
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!