Skip to content

solve: 6.Zigzag Conversion#60

Open
t9a-dev wants to merge 1 commit intomainfrom
6.Zigzag-Conversion
Open

solve: 6.Zigzag Conversion#60
t9a-dev wants to merge 1 commit intomainfrom
6.Zigzag-Conversion

Conversation

@t9a-dev
Copy link
Copy Markdown
Owner

@t9a-dev t9a-dev commented Mar 19, 2026

問題: 6. Zigzag Conversion
次にやること: コーディング練習会に参加した感想を書く
ファイルの構成: ./src/bin/<各ステップ>.rs

Comment thread src/bin/step2a.rs
println!("zip(s.chars()); (row_index,c) -> ({},{})", row_index, c)
})
.collect::<Vec<_>>();
zigzags.sort_by_key(|(row_index, _)| *row_index);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort_by_key

This sort is stable
を利用していますね。

この解法きれいですね。

Comment thread src/bin/step2.rs

let mut rows = vec![String::new(); num_rows];
let mut i = 0;
let mut is_down_direction = true;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

英語としてこなれた命名としては is_downward, is_descending, is_going_down あたりが思いつきました。現状でも意図は通じるので問題ありません。

Comment thread src/bin/step2.rs
- rowをi32として扱えば少し良くなりそう。

所感
- step1.rsと比べて少し行数は増えたものの読み手の認知負荷が下がったように感じる。
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に比べて読みやすかったです。

Comment thread src/bin/step2a.rs
Comment on lines +57 to +65
let mut zigzags = (0..num_rows)
.chain((1..num_rows - 1).rev())
.cycle()
.zip(s.chars())
// 複数のイテレータを連結しているコードのデバッグ手法の例として意図的にinspect()のコードを残しています
.inspect(|(row_index, c)| {
println!("zip(s.chars()); (row_index,c) -> ({},{})", row_index, c)
})
.collect::<Vec<_>>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

メソッドチェーンでまとめて書けると美しい一方で、Rustに不慣れな私としてはインデックス作成を分けていただけると読みやすかったです。

Suggested change
let mut zigzags = (0..num_rows)
.chain((1..num_rows - 1).rev())
.cycle()
.zip(s.chars())
// 複数のイテレータを連結しているコードのデバッグ手法の例として意図的にinspect()のコードを残しています
.inspect(|(row_index, c)| {
println!("zip(s.chars()); (row_index,c) -> ({},{})", row_index, c)
})
.collect::<Vec<_>>();
// Zig-zag order indices. e.g. [0,1,2,1,0,1, ...] for num_rows = 3
let row_indices = (0..num_rows)
.chain((1..num_rows - 1).rev())
.cycle();
let mut zigzags: Vec<(i32, char)> = row_indices
.zip(s.chars())
.collect();

Comment thread src/bin/step3.rs
Comment on lines +35 to +37
if num_rows == 0 {
return "".to_string();
}
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.

確かにnum_rowsは 1 <= num_rows <= 1000 となっているので、誤ったパラメーター0を渡してきていることを知らせるという観点は良いなと思いました。
Leet Codeの採点システムを通らなくなってしまいますが、型で表すことも可能なのでそもそも符号付き32ビット整数i32ではなく、非ゼロの32ビット整数NonZeroU32などを活用できるなと思いました。

pub fn convert(s: String, num_rows: NonZeroU32) -> String

https://doc.rust-lang.org/std/num/type.NonZeroU32.html

Comment thread src/bin/step1.rs

for c in s.chars() {
rows[row].push(c);
row = ((row as i32) + direction) as usize;
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 を s のインデックスから計算式で求める方法もあります。興味があれば探してみてください。
ただ、ぱっと見で理解しやすい計算式ではないため、もし書いたとしても、コメントで内容を説明したほうが良いと思います。

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