Skip to content

Conversation

@docto-rin
Copy link
Owner

return nothing, modify nums in-place instead.
"""
def swap(index1, index2):
nums[index1], nums[index2] = nums[index2], nums[index1]

Choose a reason for hiding this comment

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

自分はしませんでしたが、このように swap 処理を関数に切り出す方が for 文の中の見通しがよさそうだと感じました。好みかもしれませんが。

swap_target_index = 0
for i in range(len(nums)):
if nums[i] != 0:
swap(i, swap_target_index)

Choose a reason for hiding this comment

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

ただの感想になりますが、swap_target_index <= i なので swap(swap_target_index, i) と書くのも自然なのかなと少し思いました。ただどちらが自然なのか、それとも意識しなくてもいいのか、わかりませんでした。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

自分もそこで迷いました。iが主役で、swap_target_indexが脇役のように感じたのでこの順序を採用しましたが、自分もどちらがより自然かわからなかったです。

- [コメント集](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.v62rdhwkdymb)
- https://discord.com/channels/1084280443945353267/1235829049511903273/1276579345099526267
- > まとめて 0 fill は、loop unrolling できたりするのでちょっと嬉しいこともあるでしょう。
- https://ja.wikipedia.org/wiki/%E3%83%AB%E3%83%BC%E3%83%97%E5%B1%95%E9%96%8B

Choose a reason for hiding this comment

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

キーワードからいろいろ調べられていて素晴らしいと思いました。自分は loop unrolling を初めて聞いたので調べてみようと思います。

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.

3 participants