Conversation
|
|
||
| stack = [(root, root.val)] | ||
| while stack: | ||
| node, sum_so_far = stack.pop() |
There was a problem hiding this comment.
(細かいのと、いろいろな方言があるのですが、予約語は keyword、builtin は組み込みと対応させることが多く、sum は builtin のほうです。)
https://docs.python.org/ja/3/reference/lexical_analysis.html#keywords
| if ( | ||
| node.left is None | ||
| and node.right is None | ||
| and sum_so_far == targetSum |
There was a problem hiding this comment.
return sum_so_far == targetSumという手もございます🙇
There was a problem hiding this comment.
付け加えると、このコードでそれをやってしまうと適当な葉ノードにたどり着いた段階で探索が終わってしまいます。
やるならしたみたいな感じになるかとおもいます。
if node.left is None and node.right is None:
if sum_so_far == targetSum:
return True
continue| return False | ||
| ``` | ||
|
|
||
| 雑な感想)成長かはわからないが、プログラムの中で空行を入れる箇所が減った気がする。 No newline at end of file |
There was a problem hiding this comment.
成長かと思います。
それはそうと、複数行の文の改行の入れ方、フォーマッターに一度かけてみるといいかもしれません。
別に、絶対的な標準があるわけではなくいろいろな流儀があり、どれが正しいというわけではないですが、参考になるものもあるでしょう。
There was a problem hiding this comment.
フォーマッターというとblackとかですかね。使って変化を見てみます。
あと、少し話がそれますが、leetcodeだとtype annotationが全体的に古いことが気になります。leetcodeでのpython3のバージョンは3.11みたいなので(https://support.leetcode.com/hc/en-us/articles/360011833974-What-are-the-environments-for-the-programming-languages)、[List](https://docs.python.org/3/library/typing.html#typing.List)とかdeprecatedなように思います。
こういう状況だと、既存のものもゴリゴリ書き換えていくのがいいのか、周りの雰囲気に合わせて自分もその様式に倣うのか、自分の新たに書くものだけはとりあえず新しいものを使うのか(今回だったらinner functionのアノテーションは新しくするとか)って、状況次第なかんじなんでしょうか?動くコードが正義だから書き換えないみたいな主張もあると思いますが、実務でどのようなバランスを取るのか気になりました。
| return False | ||
| ``` | ||
|
|
||
| 雑な感想)成長かはわからないが、プログラムの中で空行を入れる箇所が減った気がする。 No newline at end of file |
There was a problem hiding this comment.
これわかる気がします。
最初の慣れていないときは空行で機能の違うものはとりあえず分離して、安心したい感情がありました。
There was a problem hiding this comment.
私もなぜか空行が減りました。以前はコードの可読性を高める手段として変数名の工夫と空行を挟むことくらいしか持ち合わせていなかったのでそうしていたような気がします。
There was a problem hiding this comment.
コードを読む機会が増えて空行がないコードを読むことが苦にならなくなったのもあると思います。
| - https://github.com/hayashi-ay/leetcode/pull/30/files | ||
| - targetSumから引いていくようにすれば、helperなしでも再帰でかける | ||
|
|
||
| targetSumを引いていきながら、そのまま再帰 |
There was a problem hiding this comment.
好みですが、私は targetSum は名前的に固定値のままにしたい派です。
olsen-blue/Arai60#25 (comment)
| if ( | ||
| node.left is None | ||
| and node.right is None | ||
| and sum_so_far == targetSum | ||
| ): |
There was a problem hiding this comment.
個人的な好みですが下記のような書き方のほうが読みやすいと思いました、条件部分がカッコ含めてひとかたまりになっていてわかりやすいためです。
if (node.left is None
and node.right is None
and sum_so_far == targetSum):There was a problem hiding this comment.
そうしていない理由としては、最後の条件(今回で言うとand sum_so_far == targetSum):)と条件の下につくstatementが共に4つのスペースが入るため、縦に並べたくないみたいな気持ちがあります。
好みかとは思いますが、こう書こうと思ったのもこの練習を始めてからあまり日が経ってない時なので、提案いただいた書き方も試してみます。
There was a problem hiding this comment.
if内のインデントにはいくつかパターンがあるようです。
https://pep8-ja.readthedocs.io/ja/latest/#id5
# 追加のインデントをしない
if (this_is_one_thing and
that_is_another_thing):
do_something()
# シンタックスのハイライトをサポートするエディタで区別するため
# コメントを追加する
if (this_is_one_thing and
that_is_another_thing):
# 両方の条件がtrueなので、処理を調整可能
do_something()
# 継続された行の条件をインデントする
if (this_is_one_thing
and that_is_another_thing):
do_something()There was a problem hiding this comment.
英語版を参照すると、これらには限られないそうです。
Acceptable options in this situation include, but are not limited to:
ただ、自分のコードは他の方と比べると全体的に縦に長くなりがちな印象があるので縮めてもいい気がしています。
| node.right, | ||
| sum_so_far + node.right.val | ||
| ) | ||
| return left_path or right_path |
There was a problem hiding this comment.
if node.right is not None:
のブロックの中でhas_path_sum_helperの返り値によってreturnしてしまうこともできそうです。
そうするとこのreturnとleft_path,right_path変数を消去でき、コードをより単純化できると思いました。
| def has_path_sum_helper( | ||
| node: Optional[TreeNode], | ||
| sum_so_far: int | ||
| ) -> bool: |
There was a problem hiding this comment.
好みの問題かもしれませんが、カッコ内の改行が多いと読みにくく感じます。2引数程度であれば1行で書いてしまっていいと思います。
| if node.left is None and node.right is None: | ||
| if sum_so_far == targetSum: | ||
| return True | ||
| continue |
There was a problem hiding this comment.
スタックに空ノードが入ることを許容する方法のメリットはループ内の条件分岐が減ることにあると思うので、私は単に
if node.left is None and node.right is None and sum_so_far == targetSum:
return Trueとします。
|
あとは val >= 0 であるとわかっていたらtargetSumを超えた時点で捨てるみたいなこともできそうです。 |
https://leetcode.com/problems/path-sum/description/