Conversation
mamo3gr
reviewed
Jan 27, 2026
| import collections | ||
|
|
||
|
|
||
| class NeighberWord: |
There was a problem hiding this comment.
[nits]
タイポですね。エディタでスペルチェックができると良いと思います。
Suggested change
| class NeighberWord: | |
| class NeighborWord: |
mamo3gr
reviewed
Jan 27, 2026
| self.pattern_to_neighbers[pattern].add(word) | ||
|
|
||
| @staticmethod | ||
| def word_to_pattern_iter(word): |
There was a problem hiding this comment.
自分なら名前よりタイプヒントを書くかなと思いました。mypyなどの静的解析ツールも使えますし。
Suggested change
| def word_to_pattern_iter(word): | |
| def word_to_pattern(word) -> Iterable[tuple[str, str]]: |
mamo3gr
reviewed
Jan 27, 2026
|
|
||
| class NeighberWord: | ||
| def __init__(self): | ||
| self.pattern_to_neighbers = collections.defaultdict(set) |
There was a problem hiding this comment.
どういう構造になっているのか気になる(し、add_word を読むまで分からない)ので、型ヒントで補うとよいのではないでしょうか。
Suggested change
| self.pattern_to_neighbers = collections.defaultdict(set) | |
| self.pattern_to_neighbers: dict[tuple[str, str], list[str]] = collections.defaultdict(set) |
ちょっと複雑なので、tuple[str, str] を独自の型で置き直したい気もします。
mamo3gr
reviewed
Jan 27, 2026
|
|
||
| visited_words = {beginWord} | ||
| neighbers = {beginWord} | ||
| transformation_word_count = 1 |
There was a problem hiding this comment.
問題文や既存のコードに含まれる単語を使う方が誤解が少ないと思うので、自分なら以下にします。
Suggested change
| transformation_word_count = 1 | |
| ladder_length = 1 |
現状の方向性でいくなら num_transformation あるいは word_count あたりがしっくりきます。
nodchip
reviewed
Jan 27, 2026
| word_to_patterns[word].add(pattern) | ||
|
|
||
| transformation_word_count = 1 | ||
| neighbers = set([beginWord]) |
| word_to_patterns[word].add(pattern) | ||
|
|
||
| transformation_word_count = 1 | ||
| neighbers = set([beginWord]) |
There was a problem hiding this comment.
次に調べる予定の要素の集合に対して neighbors と名付けるのは、やや違和感があります。今調べている対象の要素に隣接し、次に調べる予定の要素に対して neighbor とつけるのは自然に感じます。
個人的には frontier または frontiers あたりが良いと思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
この問題:https://leetcode.com/problems/word-ladder/
次に解く問題:https://leetcode.com/problems/maximum-depth-of-binary-tree/