Skip to content

779 kth symbol in grammar#47

Open
kitano-kazuki wants to merge 3 commits into
mainfrom
779-kth-symbol-in-grammar
Open

779 kth symbol in grammar#47
kitano-kazuki wants to merge 3 commits into
mainfrom
779-kth-symbol-in-grammar

Conversation

@kitano-kazuki
Copy link
Copy Markdown
Owner

Comment thread code1-1.py
Comment on lines +3 to +7
if n <= 0:
raise ValueError("n must be > 0")

if k > 2 ** (n - 1):
raise ValueError("k must be less than the number of symbols in the row")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

例外処理が書かれているのいいですね

Comment thread code1-1.py

is_reverse = False
row = n
col = k
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rowに対してcolということだと思いますが、問題文にcolの定義が出てきているわけではないので、ここは単純にkの方がわかりやすい気がします

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.

引数で渡されるkと今回注目するもの(変動する値)を区別したかったんですよね.
colだと確かに問題の定義にないのでわかりにくそうというのは同意です

Comment thread code1-1.py
Comment on lines +12 to +21
while row > 1:
if col % 2 == 0:
is_reverse = not is_reverse
row = row - 1
col = (col + 1) // 2

if is_reverse:
return 1
else:
return 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.

ループ内で累積した結果をそのまま返すというやり方もありそうです。少しトリッキーになりますね

result = 0
row = n
col = k
while row > 1:
    if col % 2 == 0:
        # 反転する
        result = 1 - result
    row = row - 1
    col = (col + 1) // 2

return result

@5103246
Copy link
Copy Markdown

5103246 commented May 19, 2026

LGTM

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