-
Notifications
You must be signed in to change notification settings - Fork 0
6. Zigzag Conversion #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| if numRows == 1 or numRows >= len(s): | ||
| return s | ||
|
|
||
| row_i = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
個人的にはrow_iと省略せずにrow_indexとしたほうがわかりやすい気がします。
より名前の長いdirectionと同じぐらい(というより、組になって)重要な変数だからだと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
単に row としてもよいと思います。
|
|
||
| for i in range(numRows): | ||
| zigzag_rows[i] = ''.join(zigzag_rows[i]) | ||
| flattened_rows = ''.join(zigzag_rows) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
itertools.chain.from_iterable(zigzag_rows)
としたり、わかりにくいですが
[s for row in zigzag_rows for s in row]
という書き方もあるみたいです。
https://docs.python.org/3/library/itertools.html#itertools.chain
https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうですね。Generator comprehension は nest することができます。二重に内包表記を書けるということです。
[] はリストが作られますが、() にしておくと、List comprehension ではなくてジェネレーターになります。
"".join(c for row_chars in rows for c in row_chars)
https://discord.com/channels/1084280443945353267/1322513618217996338/1361352231668158564
| * 解答時間 | ||
| - 1回目: 3:47 | ||
| - 2回目: 3:55 | ||
| - 3回目: 5:07 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
余談ですが、恐らく直近で問題問わずレビュー依頼を出していた5人にdiscordでメンションをしている気がするのですが、直近で同じ問題(今回であれば6. zigzag conversion)を解いた方にメンションを送っているのが多数派だと思います。(やったことない問題のレビューも面白いので、自分は大丈夫ですが)
自分はdiscordの検索欄で「in: レビュー依頼 6. zigzag」などと検索して探しています。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。いつも誰に出そうか迷っていたので、そういった情報はとても助かります。
| row_i += direction | ||
|
|
||
| for i in range(numRows): | ||
| zigzag_rows[i] = ''.join(zigzag_rows[i]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zigzag_rowsがlist[list[str]]からlist[str]になるのは驚きました。避けたほうが無難かと思います
6. Zigzag Conversion
次回予告: 8. String to Integer (atoi)