A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

I'm just getting back into this course after being busy for a while, and I'm on pset3. For the first part, I can't even think of a way to do it iteratively, but I've got some code for a recursive function. http://pastie.org/1874924 Right now that's returning None. I'm not even sure how that could happen right now in my code. Any help?

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

    from string import * def countSubStringMatchRecursive(target,key): count = 0 if find(target,key) != -1: count=1+countSubStringMatchRecursive(target[find(target,key) + 1:],key) return count this is my recursive. Yours looks closer to an iterative version

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

    here is an iterative version: from string import * def countSubStringMatch(target,key): limit=len(target) index=-1 count=0 while index <(limit): count=count+1 index=find(target,key,index+1) if index==-1: print 'End' break print index,count countSubStringMatch('gsca777777tsg77gcatsg77gcatgg77','7')

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

    When you recurse you don't preserve the value of count

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

    Or ... when you 'return from a recursion' you don't do anything with count. Not sure which is the correct way to say that

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

    Remember all Python functions must return something. They will return whatever you've told them to return if you use a return statement, but they will return None if that is not the case. Whenever a function is called the execution of the main code freezes and will not continue until the function has returned a value, no matter how long that takes. If it has nothing to return, it just returns the value None, rather than nothing at all.

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

    bwCA is right, you're not really incrementing `count`, you just creating a bunch of instances of the same function, each with its own independent, local variable by that name. I'm not sure what jpkita meant when they said that your code was looking more like an iterative function, it looks pretty obvious to me that it is a recursive function, just a bit of a buggy one. I think they might have meant that you're incrementing count as though it's in an iterative loop. That's a serious problem with your function you'll have to think on a bit.

  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.