Skip to content

20. Valid Parentheses#12

Open
Yuto729 wants to merge 1 commit intomainfrom
valid-parentheses
Open

20. Valid Parentheses#12
Yuto729 wants to merge 1 commit intomainfrom
valid-parentheses

Conversation

@Yuto729
Copy link
Copy Markdown
Owner

@Yuto729 Yuto729 commented Nov 28, 2025

解く問題

Valid Parentheses

次に解く問題

Reverse Linked List

Repository owner deleted a comment from github-actions bot Nov 28, 2025
@Yuto729 Yuto729 changed the title Valid Parentheses 20. Valid Parentheses Nov 28, 2025
Comment on lines +13 to +22
def are_branckets_pair(open, close):
pair_dict = {
'}': '{',
')': '(',
']': '['
}
if pair_dict[close] == open:
return True

return False
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この場合だと、同じ二つの括弧を入れていたとしても、引数の順番によって異なる返り値が返ってくるため、順番に依存しないような関数にしたいと思いました。

たとえばこんな感じ?動作までは確認できていないので、もしかしたら動かないかもしれないです。

def are_branckets_pair(bracket1, bracket2):
    valid_pairs = [
        {'(', ')'},
        {'[', ']'},
        {'{', '}'}
    ]

    # 集合は順序を持たないため { '(', ')' } も { ')', '(' } も同じ扱いのはず
    return {bracket1, bracket2} in valid_pairs

Comment on lines +67 to +70
# 以下はまとめて `return not open_branckets`とテクニカルに書けるがちょっとわかりにくい.
if len(open_branckets) != 0:
return False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には return not open_branckets の方がわかりやすく感じます。趣味の範囲なのだろうなーと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私も条件式で返せるのであればreturn <条件式>のようにすると思いました。

Comment on lines +27 to +28
for s in s:
if is_close_brancket(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.

次のステップでは改善されていたためお気付きだと思いますが、s in s で同じ変数名を使うのが怖いなと感じました。これ以降の char in s の方が良さそうです。

def is_close_brancket(s):
return s in ['}', ']', ')']

def are_branckets_pair(open, close):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

メソッドの命名について、boolを返すメソッドで are_* から始まるものを始めて見たので違和感を感じました。個人的にはis_pair_brackets,is_pairなどにすると思いました。

Copy link
Copy Markdown
Owner Author

@Yuto729 Yuto729 Nov 30, 2025

Choose a reason for hiding this comment

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

自分も今まで見たことなかったのですが、英語的にはこうかなと考えて書きましたが、複数形に対してもisを使うのが普通のようですね

Comment on lines +134 to +135
if not open_branckets or open_to_close[open_branckets.pop()] != char:
return False
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には2つに条件式を分けたほうが読みやすいと感じました。not or notのように続くよりは一度に読む量を減らせるので認知負荷が下がるという感覚です。

Suggested change
if not open_branckets or open_to_close[open_branckets.pop()] != char:
return False
if not open_branckets:
return False
if open_to_close[open_branckets.pop()] != char:
return False```

Copy link
Copy Markdown

@t9a-dev t9a-dev left a comment

Choose a reason for hiding this comment

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

細かい点ですが全体的に bracketsbrancketsになっている点が気になりました。

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.

3 participants