I've read a lot of the Python manual, but can't figure this one out.
Two local variables in a function that calls itself, and only one of them is behaving "static" like.
Here's the code snippet:
def sort_bubble(local_itera, difficulty):
#local_itera = itera[:]
sorted_count = 0
nrecursions = 0
for i in range(difficulty - 1):
val1 = local_itera[i]
val2 = local_itera[i+1]
if local_itera[i] == min(val1, val2):
sorted_count += 1
continue # skip sorted pairs
else: # swap
local_itera[i] = min(val1, val2)
local_itera[i+1] = max(val1, val2)
if not sorted_count == difficulty - 1: # recurse if not sorted
nrecursions += 1
sort_bubble(local_itera, difficulty)
While sorted_count
gets incremented, nrecursions
does not, which I would like to use to count the number of recursion calls.
Please notice that the purpose of this is to be used as a self-contained function (this is just prototyping):
- global variables defeat the purpose
- class syntax overhead defeats the purpose
Addendum
I am thinking in the following direction.
(taken from Python manual)
def whats_on_the_telly(penguin=None):
if penguin is None:
penguin = []
penguin.append("property of the zoo")
return penguin
But this is also overkill.