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 .
here is my code http://dpaste.com/698943/
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?
good answer swdalb. But I don't get what you're trying to say in the second part. Try this: print subStringMatchExact('abcd','d')
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?
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.
Thank you guys!
Join our real-time social learning platform and learn together with your friends!