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

how to solve ps3 pr2 by using tuple addition.I have used list but have to make use of tuple.As I can remember tuple was immutable data type .

OpenStudy (anonymous):

here is my code http://dpaste.com/698943/

OpenStudy (anonymous):

Good question - this is a quirky syntactical aspect of Python. So, all you have to do is change your list brackets to parentheses and then - here's the catch - you have to put a comma after index on this line: tuplle += (index,) When adding single elements to tuples, you'll always need this comma after the element you are adding. I think, also, that your code won't work for every case as it stands. It needs a (very) minor tweak... my not-so-cryptic-hint: what happens if the key is a single letter and it appears as the last letter of the target?

OpenStudy (maitre_kaio):

good answer swdalb. But I don't get what you're trying to say in the second part. Try this: print subStringMatchExact('abcd','d')

OpenStudy (anonymous):

Hmm yeah thanks maitre_kaio I didn't check the code before posting - sorry welldone_boy!, your code works - I just have some bad conception of counting positions in lists. For what it's worth (not much), this is pretty enlightening for me. I *thought* it wouldn't go into the while loop (or would kick out too soon) in exactly the kinds of cases that your suggestion is an example of. I thought that, in your example, index=find('abcd', 'd') would make index = -1, but it defaults to 3 instead (which is the same position in this case)... any reason why it defaults to the positive number rather than the negative? My guess is that it first goes through the string from left to right and then comes back from right to left using negative integers... for ex: find('dabcd', 'd', 5) == -1, not -5 or 0 or 4. maitre_kaio, can you confirm this and/or explain better?

OpenStudy (maitre_kaio):

find(target, key) returns the first position (i.e a number >= 0) if key is in target, or -1 if key isn't in target. Here the negative number has the same meaning as None.

OpenStudy (anonymous):

Thank you guys!

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
SophiaL: Someone give me an anime character to draw
15 minutes ago 6 Replies 1 Medal
hellokitty265: a 16 year old and a 18 year old is illegal right?
30 minutes ago 40 Replies 2 Medals
curriful: congrats on getting to green @oliver69 :3
27 minutes ago 13 Replies 2 Medals
Kamiehameha69: I accidentally stabbed my finger with a pencil... Am I gonna be okay?
41 minutes ago 15 Replies 3 Medals
KarmaXD: who's ur celebrity crush?
1 minute ago 83 Replies 10 Medals
Flowerpower54: honestly how do you know if someone really loves you online?
45 minutes ago 37 Replies 2 Medals
chuu: How do I do c? (attached pic)
29 minutes ago 8 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!