Skip to content

Instantly share code, notes, and snippets.

@inakagawa
Created March 1, 2017 08:26
Show Gist options
  • Save inakagawa/2930434a85291609cf883e731bfb7bf0 to your computer and use it in GitHub Desktop.
Save inakagawa/2930434a85291609cf883e731bfb7bf0 to your computer and use it in GitHub Desktop.
# -*- 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)
@inakagawa
Copy link
Author

スタック構造のテスト。あんまりうまくいってない

@inakagawa
Copy link
Author

要素が子要素を内包する関係であることが、はっきりしていれば、実装する意味はあるかも

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment