from __future__ import generators
def fr(args):
if not args:
return [""]
r = []
for i in args[0]:
for tmp in fr(args[1:]):
r.append(i + tmp)
return r
def fg(args):
if not args:
yield ""
return
for i in args[0]:
for tmp in fg(args[1:]):
yield i + tmp
return
def f0(args):
counter = [ 0 for i in args ]
r = []
while 1:
r.append("".join([ arg1[i] for arg1,i in zip(args, counter) ]))
carry = 1
x = range(len(args))
x.reverse()
for i in x:
counter[i] += 1
if counter[i] < len(args[i]):
carry = 0
break
counter[i] = 0
else:
break
return r
class fi:
def __init__(self, args):
self.args = args
self.counter = [ 0 for i in args ]
self.carry = 0
return
def __iter__(self):
return self
def next(self):
if self.carry:
raise StopIteration
r = "".join([ arg1[i] for arg1,i in zip(self.args, self.counter) ])
self.carry = 1
x = range(len(self.args))
x.reverse()
for i in x:
self.counter[i] += 1
if self.counter[i] < len(self.args[i]):
self.carry = 0
break
self.counter[i] = 0
return r
def fl(args, i=0, tmp="", parent_sibling=None):
if not args:
return (tmp, parent_sibling)
if i < len(args[0]):
sibling = fl(args, i+1, tmp, parent_sibling)
return lambda: fl(args[1:], 0, tmp+args[0][i], sibling)
else:
return parent_sibling
def traverse(n):
while n:
if callable(n):
n = n()
else:
(result, n) = n
print result,
print
def display(x):
for i in x:
print i,
print
return
def gmap(f, x):
for i in x:
yield f(i)
return
f = fr
f = fg
print f
display(f([]))
display(f(['abc']))
display(f(['abc', 'xyz']))
display(f(['abc', 'xyz', '12']))