A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

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 .

  • This Question is Closed
  1. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  2. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  3. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

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

  4. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  5. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  6. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you guys!

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.