Skip to content

78. Subsets#48

Open
tom4649 wants to merge 2 commits intomainfrom
78.Subsets
Open

78. Subsets#48
tom4649 wants to merge 2 commits intomainfrom
78.Subsets

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Apr 18, 2026

Comment thread 78.Subsets/sol1.py
subsets = []
mask = (1 << len(nums)) - 1
while mask >= 0:
subsets.append([num for i, num in enumerate(nums) if (mask >> i) & 1])
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 かどうかを判定する際は、 implicit false を使用しないほうが良いかもしれません。

subsets.append([num for i, num in enumerate(nums) if (mask >> i) & 1 != 0])

参考までにスタイルガイドへのリンクを共有いたします。

https://google.github.io/styleguide/pyguide.html#2144-decision

When handling integers, implicit false may involve more risk than benefit (i.e., accidentally handling None as 0). You may compare a value which is known to be an integer (and is not the result of len()) against the integer 0.

なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。

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.

ビット演算でも implicit false を使用するべきではないのか分からないのですが、一応書き直しておきます。
頭に留めておきます。

Comment thread 78.Subsets/sol2.py
frontier = [([], -1)]

while frontier:
fixed, determined_idx = frontier.pop()
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 を入れ、 index という名前で受けると思いますが、趣味の範囲だと思います。

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.

他の方の回答にはそのパターンもありましたね。今回はこのままにしておきます。

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.

2 participants