Skip to content

20. Valid Parentheses - #6

Open
kazizi55 wants to merge 3 commits into
mainfrom
20-valid-parentheses
Open

20. Valid Parentheses#6
kazizi55 wants to merge 3 commits into
mainfrom
20-valid-parentheses

Conversation

@kazizi55

@kazizi55 kazizi55 commented Nov 1, 2025

Copy link
Copy Markdown
Owner

@kazizi55
kazizi55 marked this pull request as ready for review November 1, 2025 10:17
Comment on lines +30 to +45
def isValid(self, s: str) -> bool:
stack = []
bracket_pairs = {"(": ")", "{": "}", "[": "]"}

for char in s:
if char in bracket_pairs:
stack.append(char)
continue
if stack:
if char != bracket_pairs[stack[-1]]:
return False
else:
stack.pop()
else:
return False
return not stack

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にはfor文内の条件分岐部分についてネストを浅くしたいなと思いました。

Suggested change
def isValid(self, s: str) -> bool:
stack = []
bracket_pairs = {"(": ")", "{": "}", "[": "]"}
for char in s:
if char in bracket_pairs:
stack.append(char)
continue
if stack:
if char != bracket_pairs[stack[-1]]:
return False
else:
stack.pop()
else:
return False
return not stack
def isValid(self, s: str) -> bool:
stack = []
bracket_pairs = {"(": ")", "{": "}", "[": "]"}
for char in s:
if char in bracket_pairs:
stack.append(char)
continue
if not stack:
return False
expected_bracket = bracket_pairs[stack[-1]]
if char != expected_bracket:
return False
stack.pop()
return not stack

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!
else 節がなくなることで見通しがよくなった印象です!
Step 4 として追加しました!
https://github.com/kazizi55/coding-challenges/pull/6/files#diff-dee0cfbbf6f296838839ded17654fe00273d500efa9a5fdb004e516117cecc86R18-R35

if char in bracket_pairs:
stack.append(char)
continue
if char != bracket_pairs[stack[-1]]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にbracket_pairs[stack[-1]]が少し認知負荷が高いと感じました。
変数の命名により明示的にすると緩和できると思いました。

Suggested change
if char != bracket_pairs[stack[-1]]:
expected_bracket = bracket_pairs[stack[-1]]
if char != expected_bracket:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
確かに改めて見ると認知負荷が高いですね、、「stack の一番後ろにあるということは opening bracket を期待しているのだな」という推察をしてもらわないといけないですもんね。
以下で説明変数を追加しました!
https://github.com/kazizi55/coding-challenges/pull/6/files#diff-dee0cfbbf6f296838839ded17654fe00273d500efa9a5fdb004e516117cecc86R1-R16

@@ -0,0 +1,45 @@
class SolutionWithBracketPairsAndSentinel:
def isValid(self, s: str) -> bool:
stack = ["*"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この名前は読み手に伝わる情報が特にない気がします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

通常の list を stack として扱うということを明示する意図でした。
https://github.com/kazizi55/coding-challenges/pull/6/files#diff-2fbd326bf14883ff067365f9862ec32c829032750dd33fcf9caef79989b432d6R23

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

中にどのような要素が含まれるかを想起できるような英単語、または英語句を付けたほうが、読み手にとってコードを理解するうえで有用な情報になると思います。


## Comments

### Step 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Step1で何を考えて解いていたかもう少し書くとコメントがしやすいと思いました!

どういう思考・試行を経て回答に至っているかのほうが気になるので

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!
確かに Step 1についても思考過程を詳しく書いた方が議論が活発になりそうですね。
次回から追加してみます。

return False
return len(stack) == 1

# bracket pairs を使って実装する version (expected_closing_bracket の命名を追加し、else を使わない)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

番兵使わないほうが、この場合は素直かもしれませんね。あると少し説明がいる感じがします。その割に減るのが条件文ひとつなので。
底に "*" をいれているのが、特別な意味がありそうに見えているというのもあると思っています。
"" や None のほうが特別な意味がない感じがするので、読み手には少し楽です。

@@ -0,0 +1,45 @@
class SolutionWithBracketPairsAndSentinel:
def isValid(self, s: str) -> bool:
stack = ["*"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

中にどのような要素が含まれるかを想起できるような英単語、または英語句を付けたほうが、読み手にとってコードを理解するうえで有用な情報になると思います。

class SolutionWithBracketPairsAndSentinel:
def isValid(self, s: str) -> bool:
stack = ["*"]
bracket_pairs = {"(": ")", "{": "}", "[": "]", "*": ""}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には開きカッコと閉じカッコの対応であることを読み手に伝えるため、 open_to_close と名付けると思います。好みの問題かもしれません。

def isValid(self, s: str) -> bool:
stack_list = list()

for char in s:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

char という変数名は、 C/C++ で予約語となっているため、避けたほうが無難かもしれません。 c や ch といった変数名を自分はよく使います。

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.

6 participants