Skip to content

02_Valid Parentheses.md#5

Open
Kitaken0107 wants to merge 1 commit intomainfrom
Kitaken0107-patch-6
Open

02_Valid Parentheses.md#5
Kitaken0107 wants to merge 1 commit intomainfrom
Kitaken0107-patch-6

Conversation

@Kitaken0107
Copy link
Owner

Copy link

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

辞書を使っても良いので命名などはもう少しこだわると良いと思います。

#対応表を作る
#.pop
dict_kakko = {')':'(','}':'{',']':'['}
open = ('(','{','[')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openはbuiltin functionなので変数名として使わないほうが良いでしょう。
https://docs.python.org/3/library/functions.html

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

open_brackets, left_bracketsとかでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数の命名参考になります...!

#openだったらstackに積む、closeだったら、stackの末尾のopenとcloseが対応しているかを確認。対応→〇、非対応→×
#対応表を作る
#.pop
dict_kakko = {')':'(','}':'{',']':'['}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数名がないよりはマシですがローマ字読みの命名は好ましくないです。parentheses, bracketsあたりでしょうか。

stack.append(kakko)
elif stack.pop() != dict_kakko[kakko]:
return False
break

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このbreakは不要です。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

01と同じですね
すみません、ありがとうございます

elif stack.pop() != dict_kakko[kakko]:
return False
break
elif stack.pop() == dict_kakko[kakko]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if-elif-elifでelseの場合がどうなるか読んでいて気になりました。実際にはこのelifはelif stack.pop() != dict_kakko[kakko]:の余事象になるので、elseで良いと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

たしかに余事象になるので、わざわざelifで条件記載すると冗長になりますね
ありがとうございます

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants