Skip to content

20. Valid Parentheses.md #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

20. Valid Parentheses.md #6

wants to merge 1 commit into from

Conversation

katataku
Copy link
Owner

趣味の範囲でしょう。大規模開発などでは周りを見て合わせましょう。
- https://github.com/BumbuShoji/Leetcode/pull/7/files#r1814477339
- なるべくimplicitにする。
- とはいえ、戻り値の型がboolの関数で`return List`とするのがなんとなく好きじゃない。
Copy link

Choose a reason for hiding this comment

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

あ、型が変わるのは良くないですね。bool にキャストするか、頭に !! をつけます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。前提を間違えて理解してました。
型が変わるのは良くないというのはとてもしっくりくるので、安心しました。

if c == ']' and opening_parentheses[-1] != '[':
return False
opening_parentheses.pop()
return len(opening_parentheses) == 0
Copy link

Choose a reason for hiding this comment

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

implicit false を利用してシンプルに書くことをおすすめいたします。
return not opening_parentheses

https://google.github.io/styleguide/pyguide.html#214-truefalse-evaluations

For sequences (strings, lists, tuples), use the fact that empty sequences are false, so if seq: and if not seq: are preferable to if len(seq): and if not len(seq): respectively.

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.

valid parentheses は、チョムスキー階層、タイプ-2、文脈自由文法だから、プッシュダウンオートマトンで書ける、を多分連想します

https://discord.com/channels/1084280443945353267/1201211204547383386/1202541275115425822

def isValid(self, s: str) -> bool:
opening_parentheses = []
for c in s:
if c in ['(', '{', '[']:

Choose a reason for hiding this comment

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

if c in '({[':

これでも良いですね。Pythonだとstringもsequenceです。
https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str

Strings are immutable sequences of Unicode code points.

```python
class Solution:
def isValid(self, s: str) -> bool:
open_to_close = {

Choose a reason for hiding this comment

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

open, closeの代わりにleft, rightも選択肢ですかね。

Brackets are typically deployed in symmetric pairs, and an individual bracket may be identified as a 'left' or 'right' bracket or, alternatively, an "opening bracket" or "closing bracket"

https://en.wikipedia.org/wiki/Bracket

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!そういう言い方できるの知りませんでした!
wikipedia 的にはleft-rightが標準で、open-closeがalternativeなんですね

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.

4 participants