I want to convert my code such that I don’t have to use an extra variable (i.e, s in the example below). I know that there is a way to use iterator but I don’t know how. Can somebody help? Thanks a bunch.
from numbers import Number
a = [[[1,2],[3,4]],[[5,6],[7,8]]]
def trav(root,s):
if isinstance(root,Number):
print(str(root) + " -> " + s)
else:
s = s + "0"
trav(root[0],s)
s = s[:-1]
s = s + "1"
trav(root[1],s)
s = s[:-1]
s = ""
trav(a,s)
Why not just…
and then you can omit your
s = ""line and call it astrav(a)? Also if you were willing to just inline a thing or two, you could do less slicing:Note that none of this has anything to do with iterators; I’m not sure what you were thinking of there.
shere acts as an accumulator argument; perhaps that’s the term you were thinking of?