Skip to content

Create 6. Zigzag Conversion.md#26

Merged
Mike0121 merged 2 commits intomainfrom
6.-Zigzag-Conversion
Nov 1, 2025
Merged

Create 6. Zigzag Conversion.md#26
Mike0121 merged 2 commits intomainfrom
6.-Zigzag-Conversion

Conversation

@Mike0121
Copy link
Copy Markdown
Owner

@Mike0121 Mike0121 commented Jun 8, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

"".join(zigzag) でよいと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら行のリストを表す rows と名付けると思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

step1 の書き方のほうがシンプルに感じました。

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.

そうですね、文字列の追記の再構築の話がなければ、Step1が自分も見やすいです。
ありがとうございます。

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を特別あつかいせずに別の変数を用意したほうがいいように思います。

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.

odaさん、ありがとうございます。nodchipさんの意見も踏まえ、
最後の出力の部分はさまざまな書き方があり、step毎に色々試しましたが、別の変数作ってjoinして結合していくのが一番素直かつ見やすそうですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

リストの内包表記を入れ子にすると、読んでいて認知負荷が高くなるため、避けたほうが良いと思います。 step1 のほうが分かりやすいように思いました。

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.

ありがとうございます。こちら、他参加者のコメントにあったため、試しに書いてみましたが、Step3の意義としては不適切だったかと思います。自分も、書いててかなりややこしく感じたため、よかったです。
基本的に選択肢として、Step1, 文字列の追記における再構築が問題となった場合をStep2を頭に入れておきます。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

読みやすさとはまた別の話ですが、リストを作らなくてもgenerator expressionの形にすれば十分だと思いました。

https://peps.python.org/pep-0289/

For instance, the following summation code will build a full list of squares in memory, iterate over those values, and, when the reference is no longer needed, delete the list:

sum([x*x for x in range(10)])

Memory is conserved by using a generator expression instead:

sum(x*x for x in range(10))

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.

ありがとうございます。みやすさ以外でも視点を広げるためにコメントいただけるのはありがたいです。
たしかに、下記の書き方でも十分ですね。

return "".join(l for row in ordered_letters for l in 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.

問題で与えられた文字列をzigzagにして行ごとに読んだときの文字列を返したいので、ordered_lettersという変数名は読み手を混乱させるかもなと思いました。2次元のリストであることも名前からは分かりにくそうです。
rowsとかzigzag_rowsで良い気がしました

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.

ありがとうございます、参考になります。2次元リストをrowsにして、各行を取り出す際にrowにすると確かに伝わりやすいですね。nodchipさんからのコメントも合わせて、zigzag_rowsにします。

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.

ありがとうございます。意識しておりませんでした。以後 str.join(iterable)にします。
参照: https://docs.python.org/3/library/stdtypes.html

@Mike0121 Mike0121 merged commit 6c4956f into main Nov 1, 2025
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