can anyone share their PS3 part 1 results? I created a working function fine but don't know the difference between recursive and iterative functioning. If you have an example of both it would be great.
An iterative function will repeat itself through a list, like mylist = [milk, bread, eggs] so any defined function will first test on "milk", then test on "bread", then test on "eggs." Then it will be done. Conversely, the process of a function calling itself is recursion. Here's an example: def nLines(n): ...if n > 0: ...print ...nLines(n-1) So this function calls itself and modifies the variable "n" each time, rather than running through a preset list as an iterative function would. I'm still on ps1, but hopefully that helps you.
Yuk- that's ugly. Sorry, look here for the little code I rendered illegible above: http://dpaste.com/606899/
def countSubStringMatch(target, key): counted = 0 i = 0 nextindex = 0 while i != -1: nextindex = i + len(key) i = target.find(key, nextindex) counted += 1 return counted def countSubStringMatchRecursive(target, key): if target.find(key) == -1: return 0 else: nextindex = target.find(key) + len(key) return 1 + countSubStringMatchRecursive(target[nextindex:], key)
countSubStringMatch(target, key) is an iterative function; using a loop construct to repeat a detailed process a described number of times characterizes an iterative function. This is the main style used in the imperative/procedural programming paradigm. countSubStringMatchRecursive(target, key) is a recursive function; a function that breaks down a larger problem into a smaller problem by calling itself is an important characteristic of recursive functions. This is the main style used by the functional/declarative programming paradigm.
Join our real-time social learning platform and learn together with your friends!