Created
November 11, 2012 06:45
-
-
Save RandomEtc/4053983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ftrain(list): | |
out = [ list[0] ] | |
stack = [ out ] | |
for i in list[1:]: | |
last = stack[-1][-1] | |
if i > last: | |
# start a new list | |
new = [] | |
stack[-1].append(new) | |
stack.append(new) | |
while i < last: | |
# otherwise go back | |
stack.pop() | |
last = stack[-1][0] | |
# add to the end... | |
stack[-1].append(i) | |
return out | |
a = [ 1, 2, 3, 3, 4, 4, 2, 1 ] | |
b = ftrain(a) | |
print b | |
# should be = [ 1, [ 2, [ 3, 3, [ 4, 4 ] ], 2 ], 1 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment