Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

shupler Group Title

ok this is from problem set three question one. from string import* def countSubStringMatchRecursive(target, key): index = 0 count = 0 index = find(target, key, index) if index != -1: count += 1 index += len(key) print "index %d" % (index) print "target[index:]: %s" % (target[index:]) count += countSubStringMatchRecursive(target[index:], key) return count this code works, I took it from http://www.kevinladenheim.com/2011/07/mit-600-problem-set-3-problem-1.html because i didn't quite understand what was meant by re

  • one year ago
  • one year ago

  • This Question is Open
  1. shupler Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    recursive ( I guess its like iteration but with a smaller sample size each time?). my real question comes from "count += countSubStringMatchRecursive(target[index:], key)" I am a bit puzzled how this code allows it to add one to the count value for each inter iteration. Is it because the return within the definition is assigned to count, so it adds what the count value becomes each time? mad props if you understand my question lol ty

    • one year ago
  2. sasogeek Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    the return statement is the last statement and isn't part of the iteration, and the last count assignment is a recursive statement done if index is not found, index=find(...) if index!=-1: so count really is assigned a value at count+=1, the second one only allows the recursion. idk if i answered u cos i'm not clear about your question, but this is my general understanding of the code in ur question.

    • one year ago
  3. sasogeek Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    i haven't seen the problem set though so my knowledge to your question is limited. sorry if i was of no help here lol

    • one year ago
  4. bwCA Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    x += something is the same as x = x + something. the code you posted builds up a string of count + somethings for each recursion. this link is a good explanation - but the language being used is scheme/lisp - http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2

    • one year ago
  5. bwCA Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    same thing but different http://dpaste.com/842357/

    • one year ago
  6. rsmith6559 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Recursion is used when a problem can be solved by aggregating the results of solving a section of the problem. Recursion is the best way to traverse trees and graphs. A recursive function, say one to reverse a string, will take one part of the problem, and call itself with the rest of the problem (string). Each time a function is called, it gets a frame, it's execution environment (mostly the memory where it's variables are stored). The calling code is pushed to the side, a reference to it is stored on a stack (a LIFO, Last In First Out structure). One of the most important parts of a recursive function is the "base case", when to stop calling itself. The function can work on the problem before and/or after it calls itself, getting the results of the sub-problem. When the function is in the returning phase, it's referred to as unwinding. A simple Python program: def reverse( string ): # the base case if( len( string ) == 0 ): return char = string[ 0 ] reverse( string[ 1: ] ) print char if( __name__ == "__main__" ): reverse( "Hello World" ) You can play around with this and get comfortable with what's going on in recursion.

    • one year ago
    • Attachments:

See more questions >>>

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.