Conversation
| max_with_last = max(max_with_last, max_without_last + money) | ||
| return max_with_last, next_max_without_last | ||
|
|
||
| return max(functools.reduce(visit_next, sequence, (0, 0))) |
There was a problem hiding this comment.
(0, 0) を初期値とする state の正体を読み解くのと、それが引き回されていく様子をイメージするのに手こずりました。sequence も命名の抽象度が上がり分かりにくく感じました。いずれもタイプヒントを書くと多少は助けになるかもしれません。
There was a problem hiding this comment.
なるほど、採用しました。型ヒントは可読性が向上しますね。
| if len(nums) < 2: | ||
| return max(nums) | ||
|
|
||
| def rob_without_circle(nums_without_circle): |
There was a problem hiding this comment.
瑣末な好みの問題ですが,rob_from_straight_lineのように「〜でない」よりも「である」を言語化する方が好みです.特に今回は直線のケースの応用として捉えているので.
There was a problem hiding this comment.
自分は「円の制約を無視する」ということを伝えたかったのだと思います。
が、確かに直線を明示した方が良いかもしれません。
(sol2.pyではご指摘と同じようにrob_linearlyにしていますね)
There was a problem hiding this comment.
面白い書き方で勉強になりました.reduceを使うのが関数型っぽいですね(という認識であっていますかね?).
There was a problem hiding this comment.
そこです。わざわざ使う必要はなさそうですが、他の人のものを真似て書いてみました。
| max_with_last = max(max_with_last, max_without_last + money) | ||
| return max_with_last, next_max_without_last | ||
|
|
||
| return max(functools.reduce(visit_next, sequence, (0, 0))) |
There was a problem hiding this comment.
色々な書き方を試すというのはとても素晴らしいことだと思います。
ただ、関数型言語由来の関数を使うと、読み手にとって認知負荷が高くなる場合がありそうです。チーム内でどれくらい使われているかを確認してから使うのが良いと思います。
There was a problem hiding this comment.
そうですね、実際にpythonで自分がこのような書き方をすることは少なそうな気がします。勉強にはなりますが。
pythonのGuido氏も好きでなかったらしいですね。
https://leetcode.com/problems/house-robber-ii/submissions/1962278794/