A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Problem with a recursive approach to problem set 2 question 2 (ps3b).

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

    I always end up with the function yielding nested tuples, plus I could not do it without passing an additional arg. to the function to keep the length of the sliced string. http://dpaste.com/531825/ Any ideas?

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

    Sorry, this question is in regards to problem set 3, not 2

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

    I don't understand recursive functions enough to explain them but I can give you mine if you want it and you can try to figure it out?

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

    that'll be great, thanks

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

    http://dpaste.com/hold/531832/

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

    to solve the nested tuple problem, instead of returning loc, subStringMatchRecursive(target,key,(loc+1)) you return things like loc + subStringMatchRecursive(target,key,(loc+1)) when you use a comma, even without parens it assumes your trying to make a tuple, or at least it does sometimes, its sketchy at best. when you use the "+", tried to create one whole thing. sometimes the tuple doesn't like to add with a non tuple, when that happens i find that you can help it along by putting the non tuple variable (say x) tuple + (x,) hopefully that made sense and i understood what was going wrong

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

    Like Nessman said, you can fix your code by returning: (loc,) + subStringMatchRecursive(target,key,(loc+1)) You'll also have to seed an empty tuple instead of None, which is what you're implicitly seeding (by not having a return outside the while loop). You can do that by adding "return tuple()" outside your while loop. I've attached my ps3b.py for reference. I solved it three different ways, but my "subStringMatchExact2" is closest to your code.

    1 Attachment
  8. 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.