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)
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
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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
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,
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.
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
Not the answer you are looking for? Search for more explanations.
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 -
same thing but different http://dpaste.com/842357/
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 ):
char = string[ 0 ]
reverse( string[ 1: ] )
if( __name__ == "__main__" ):
reverse( "Hello World" )
You can play around with this and get comfortable with what's going on in recursion.