-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
趣味の範囲でしょう。大規模開発などでは周りを見て合わせましょう。 | ||
- https://github.com/BumbuShoji/Leetcode/pull/7/files#r1814477339 | ||
- なるべくimplicitにする。 | ||
- とはいえ、戻り値の型がboolの関数で`return List`とするのがなんとなく好きじゃない。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、型が変わるのは良くないですね。bool にキャストするか、頭に !! をつけます。
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this 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 ['(', '{', '[']: |
There was a problem hiding this comment.
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 = { |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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なんですね
https://leetcode.com/problems/valid-parentheses/description/