Conversation
| class Solution: | ||
| def __init__(self): | ||
| self.parenthesis_cache: Dict[int, str] = {} | ||
|
|
There was a problem hiding this comment.
クラスメソッド間の空行は 1 行とすることをお勧めいたします。
https://google.github.io/styleguide/pyguide.html#35-blank-lines
Method definitions inside a class are surrounded by a single blank line.
https://google.github.io/styleguide/pyguide.html
One blank line between method definitions and between the docstring of a class and the first method.
| (((B))) + "" -> ["((()))"] | ||
| ((C)(A)) + "" -> ["(()())"] | ||
| """ | ||
| if (n == 0): |
There was a problem hiding this comment.
条件が複数行に渡る場合を除き条件文を () で囲わないことをお勧めいたします。
https://google.github.io/styleguide/pyguide.html#33-parentheses
Do not use them in return statements or conditional statements unless using parentheses for implied line continuation or to indicate a tuple.
There was a problem hiding this comment.
ありがとうございます。そこら辺、まだJSの癖が抜けず申し訳ないです。JSはないと動かないので。合わせます。
|
|
||
| results: List[str] = [] | ||
|
|
||
| for left_count in range(n): |
There was a problem hiding this comment.
left_count は自分なら num_left_parentheses と付けると思います。ただ、強く推奨するわけではありません。
|
|
||
| for left_count in range(n): | ||
| for left_hand in self.generateParenthesis(left_count): | ||
|
|
There was a problem hiding this comment.
この空行は不自然に感じました。処理の切れ目等に入れるのであれば自然に感じられます。 for 文の直後は処理の切れ目ではないと思いますので、空行を入れないほうが良いと思います。
https://peps.python.org/pep-0008/#blank-lines
Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations).
|
|
||
| right_count = n - 1 - left_count | ||
| for right_hand in self.generateParenthesis(right_count): | ||
| results.append(f"({left_hand})" + right_hand) |
There was a problem hiding this comment.
f 文字列と + による文字列結合を同時に使う点に違和感を感じました。計 4 つの文字列を結合していますので、 f 文字列で統一するとよいと思います。
results.append(f"({left_hand}){right_hand}")
ちなみに、
results.append(f"{left_hand}({right_hand})")
が必要ない理由はなぜでしょうか…?第一感分からなかったため質問しました。
There was a problem hiding this comment.
ありがとうございます。
そこはちょっと悩みましたが、f""の中に全部入れたとき、なんとなく可読性が悪いと感じたから分けました。
私としては、何の部分文字列が組み合わさっているのか分かりやすくするのが重要だと思いますが、どうですか?
例えば、もう少し冗長的に書くなら、こうです。
results.append("".join([
f"({left_hand})",
right_hand
])実際、SQLを生で生成しなければならないとき、こういう風にすることがあります
sql = f"select id, name, score from {table_name} where score >= {lower_bounds} and score < {upper_bounds}"よりは、こちらの方が可読性が良いと私としては思います
sql = " ".join([
"select",
"id, name, score",
f"from {table_name}",
"where",
f"score >= {lower_bounds} and",
f"score < {upper_bounds}"
])Python は"""が使えるので、敢えてこんなことはしませんが、意図としては、「アルゴリズムの意図として、left_handに対して()を付けたものにright_handを結合するということを明示するため」です。
趣味嗜好の範囲なので、多くの人がf""に全部入れたほうが良い、というのであれば、合わせます
There was a problem hiding this comment.
そこはちょっと悩みましたが、f""の中に全部入れたとき、なんとなく可読性が悪いと感じたから分けました。
私としては、何の部分文字列が組み合わさっているのか分かりやすくするのが重要だと思いますが、どうですか?
あくまで自分の感覚となりますが、
results.append(f"({left_hand})" + right_hand)
も
results.append("".join([
f"({left_hand})",
right_hand
])
も、第一感何をしようとしているのか分かりにくく感じました。自分の場合は、 + 一つまでで済むのであれば + で、そうでなければ f 文字列で、文字列の結合を行います。
余談となりますが、
実際、SQLを生で生成しなければならないとき、こういう風にすることがあります
sql = f"select id, name, score from {table_name} where score >= {lower_bounds} and score < {upper_bounds}"
SQL インジェクション回避のため、 SQL 文を生成する際は、プレースホルダーを使用するのがよいと思います。
|
|
||
| class Solution: | ||
| def __init__(self): | ||
| self.parenthesis_cache: Dict[int, str] = {} |
22. Generate Parentheses
level: Medium
https://leetcode.com/problems/generate-parentheses/