Skip to content

213. House Robber II#34

Open
tom4649 wants to merge 2 commits intomainfrom
213.House-Robber-II
Open

213. House Robber II#34
tom4649 wants to merge 2 commits intomainfrom
213.House-Robber-II

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 28, 2026

Comment thread 213/sol2.py
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)))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(0, 0) を初期値とする state の正体を読み解くのと、それが引き回されていく様子をイメージするのに手こずりました。sequence も命名の抽象度が上がり分かりにくく感じました。いずれもタイプヒントを書くと多少は助けになるかもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、採用しました。型ヒントは可読性が向上しますね。

Comment thread 213/sol1.py
if len(nums) < 2:
return max(nums)

def rob_without_circle(nums_without_circle):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

瑣末な好みの問題ですが,rob_from_straight_lineのように「〜でない」よりも「である」を言語化する方が好みです.特に今回は直線のケースの応用として捉えているので.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分は「円の制約を無視する」ということを伝えたかったのだと思います。
が、確かに直線を明示した方が良いかもしれません。
(sol2.pyではご指摘と同じようにrob_linearlyにしていますね)

Comment thread 213/sol2.py
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

面白い書き方で勉強になりました.reduceを使うのが関数型っぽいですね(という認識であっていますかね?).

Copy link
Copy Markdown
Owner Author

@tom4649 tom4649 Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そこです。わざわざ使う必要はなさそうですが、他の人のものを真似て書いてみました。

Comment thread 213/sol2.py
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)))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

色々な書き方を試すというのはとても素晴らしいことだと思います。
ただ、関数型言語由来の関数を使うと、読み手にとって認知負荷が高くなる場合がありそうです。チーム内でどれくらい使われているかを確認してから使うのが良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね、実際にpythonで自分がこのような書き方をすることは少なそうな気がします。勉強にはなりますが。

pythonのGuido氏も好きでなかったらしいですね。

https://jp.quora.com/%E3%81%AA%E3%81%9CGuido%E3%81%AFmap-%E3%82%84filter-reduce-%E3%81%9D%E3%81%97%E3%81%A6lambda%E3%82%92Python-3%E3%81%8B%E3%82%89%E5%8F%96%E3%82%8A%E9%99%A4%E3%81%8D%E3%81%9F%E3%81%8B%E3%81%A3%E3%81%9F%E3%81%AE/answers/173766648?ch=10&share=e8d27008&srid=7Ndeg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants