Skip to content

Conversation

@TORUS0818
Copy link
Owner

if k == 1:
return 0

if k % 2:
Copy link

Choose a reason for hiding this comment

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

if k % 2 == 1 と書いた方がわかりやすいように思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

反転する処理をメインにした方が良いという意図でしょうか?

- https://github.com/olsen-blue/Arai60/pull/47
- 2分木を考えてkの偶奇で親の値と同じになるかを判定
- https://github.com/olsen-blue/Arai60/pull/47/files#r2002307405
- これは思いつかないが知っておこう
Copy link

Choose a reason for hiding this comment

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

私が想像する出題者の感覚として、なんらかの方法で書いてもらって、そこから誘導をかけながらビットの数であることに気が付いてもらい、連想する知識を見る、くらいかなと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

思いつかないようなものでも、考え方を聞いて、(あまり時間をかけずに咀嚼して)選択肢の一つとして引き出しにしまっておくのは大事だと思いました。
色々な選択肢を持って選ぶ感覚を持つ、ということですね。

- 上記を踏まえると、len(row(t+1)) = 2*len(row(t))に注意して
- k <= len(row(t))なら、row(t+1)のkは、row(t)のkと同じ
- k > len(row(t))なら、row(t+1)のkは、row(t)のk-len(row(t))を反転させたものと同じ
- ところでこの関係性ってtrivialなものなのか??
Copy link

Choose a reason for hiding this comment

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

観察すれば思いつきやすいですが、意外と自明ではないですよね

- https://github.com/yukik8/leetcode/commit/a0e14f3fbe34720d13fcb61916c99d2a424b7bc6
- https://github.com/nittoco/leetcode/pull/29
- bitcount
- 初手コレの人もいるのか
Copy link

Choose a reason for hiding this comment

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

思いつきやすかったのは、昔中学受験の算数で、似たような問題があったのかもしれません

Copy link
Owner Author

Choose a reason for hiding this comment

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

こんなことやるんですね。
私も中学受験しましたが、遊びで参加していたのでガチ勢がどういうことをやるのかあまりよく分かっておらず。

子供がその気になったら面白そうなので自分もやってみようかなと思いました。

Comment on lines +75 to +82
def kthGrammar(self, n: int, k: int) -> int:
if k == 1:
return 0

if k % 2:
return self.kthGrammar(n - 1, (k + 1) // 2)
else:
return 1 - self.kthGrammar(n - 1, (k + 1) // 2)
Copy link

Choose a reason for hiding this comment

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

kに1引いて0-indexedから考えて行った方が個人的にわかりやすく感じます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

nittocoさんの解法の感じですよね。
私もその方が自然だと思いまいた

そもそもkを弄る必要がある時点であまりこの解法は想定されていないのかもしれませんね。

return 0

if k <= row_len // 2:
return self.kthGrammar(n-1, k)
Copy link

Choose a reason for hiding this comment

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

- の両側にスペースを空けることをお勧めします。

参考までにスタイルガイドへのリンクを貼ります。

https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements

Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <=, >=, in, not in, is, is not), Booleans (and, or, not).

https://google.github.io/styleguide/pyguide.html#s3.6-whitespace

Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).

ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。

Copy link
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.

6 participants