From the chapter, accumulation of a recursive data structure by use of a Queue.
Q = [[2,1,9],3,[[6,7],0]] |
def loopsum(List): |
Accum, Queue = 0, List[:] |
# Queue starts out with all items |
while len(Queue)>0: |
head=Queue.pop(0) |
if type(head)==int: |
Accum += head |
else: # head is a list |
# move nested items to end |
Queue.extend(head) |
# Magically, when loop ends, done! |
return Accum |
print loopsum(Q) |