Conversation
| else: | ||
| raise ValueError("Invalid input: only parentheses are allowed") | ||
|
|
||
| return not open_parentheses_stack |
There was a problem hiding this comment.
[IMO]
return not open_parentheses_stackは認知負荷が高いかなと私は思いました。
この行だけ見たときに、open_parenthese_stackはどんな変数なのか、その否定とは?みたいな疑問が湧くためです。
私は return len(open_parentheses_stack) == 0 というような形で書いてまして、
これはopen_parentheses_stackの長さが0であるかどうかを検証している関数なんだなというのがわかりやすいだろうという意図です。
文章を書くときと同様に結論はなにをしたいのかというのはわかりやすくなるように心がけてます。
There was a problem hiding this comment.
ご指摘ありがとうございます。
私も感覚としてlen(open_parentheses_stack) == 0の方が若干読みやすいのは賛成なのですが、下記のような指摘があり、変数名にstackとあることもありより簡潔に書ける方を採用しました。
https://github.com/komdoroid/arai60/pull/12/files#r2630417643
もう一度他の方のコードを見返してみるとlenで書かれてる方が多いようでしたので次回以降はlen~で書くようにしようと思います。
| for char in s: | ||
| if char in open_parentheses: | ||
| open_parentheses_stack.append(char) | ||
| elif char in close_parentheses: |
There was a problem hiding this comment.
読み手としては、条件分岐を頭に入れながら読み進める必要があるので、さっさとearly returnして新しいif文があるほうが読みやすいです。さらにif文が続く場合はなおさらです。
問題
https://leetcode.com/problems/valid-parentheses/description/
次に解く問題
https://leetcode.com/problems/reverse-linked-list/description/