All pastes #1649364 Raw Edit

Miscellany

public python v1 · immutable
#1649364 ·published 2009-10-30 15:49 UTC
rendered paste body
def flatten(input, ltypes=(list,tuple)):    ltype = type(input)    output = []    stack = []    stack.extend(reversed(input))    while stack:        top = stack.pop()        if isinstance(top, ltypes):            stack.extend(reversed(top))        else:            output.append(top)    return ltype(output)def flatten2(l, ltypes=(list, tuple)):    ltype = type(l)    l = list(l)    i = 0    while i < len(l):        while isinstance(l[i], ltypes):            if not l[i]:                l.pop(i)                i -= 1                break            else:                l[i:i + 1] = l[i]        i += 1    return ltype(l)#print flatten([1,[2,3,[4,5,[6],7],8,[9,[10,11],[12],[[13]],[[[14,15]]],16]]])import sysfunc = sys.argv[1]total = int(sys.argv[2])a = []for i in xrange(total):      a = [a, i]if func == "flatten":    print len(flatten(a))elif func == "flatten2":    print len(flatten2(a))