Created
March 1, 2017 08:26
-
-
Save inakagawa/2930434a85291609cf883e731bfb7bf0 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
# -*- coding: utf-8 -*- | |
# stack sample | |
textdata = [ | |
{'d':0, 't':'c1'}, | |
{'d':1, 't':'c1-1'}, | |
{'d':1, 't':'c1-2'}, | |
{'d':2, 't':'c1-2-1'}, | |
{'d':0, 't':'c2'}, | |
{'d':1, 't':'c2-1'}, | |
] | |
def pop_and_disp(l): | |
pop_line = l.pop() | |
print(pop_line) | |
stack = [] | |
for line in textdata: | |
# 空なら無条件に追加 | |
if len(stack)==0: | |
stack.append(line) | |
continue | |
# スタックと新しい行を比較 | |
new_d =line['d'] | |
sta_d =stack[-1]['d'] | |
if new_d >= sta_d: | |
# line 開き処理 | |
stack.append(line) | |
else: | |
while new_d < sta_d: | |
pop_and_disp(stack) | |
# line 閉じ処理 | |
sta_d = stack[-1]['d'] | |
if len(stack) > 0: | |
pop_and_disp(stack) | |
while len(stack) > 0: | |
pop_and_disp(stack) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
要素が子要素を内包する関係であることが、はっきりしていれば、実装する意味はあるかも