Skip to content

127. Word Ladder#22

Open
xbam326 wants to merge 3 commits into
mainfrom
127
Open

127. Word Ladder#22
xbam326 wants to merge 3 commits into
mainfrom
127

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Jan 26, 2026

import collections


class NeighberWord:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[nits]
タイポですね。エディタでスペルチェックができると良いと思います。

Suggested change
class NeighberWord:
class NeighborWord:

self.pattern_to_neighbers[pattern].add(word)

@staticmethod
def word_to_pattern_iter(word):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら名前よりタイプヒントを書くかなと思いました。mypyなどの静的解析ツールも使えますし。

Suggested change
def word_to_pattern_iter(word):
def word_to_pattern(word) -> Iterable[tuple[str, str]]:


class NeighberWord:
def __init__(self):
self.pattern_to_neighbers = collections.defaultdict(set)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

どういう構造になっているのか気になる(し、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] を独自の型で置き直したい気もします。


visited_words = {beginWord}
neighbers = {beginWord}
transformation_word_count = 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

問題文や既存のコードに含まれる単語を使う方が誤解が少ないと思うので、自分なら以下にします。

Suggested change
transformation_word_count = 1
ladder_length = 1

現状の方向性でいくなら num_transformation あるいは word_count あたりがしっくりきます。

word_to_patterns[word].add(pattern)

transformation_word_count = 1
neighbers = set([beginWord])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: neighbors でしょうか。

word_to_patterns[word].add(pattern)

transformation_word_count = 1
neighbers = set([beginWord])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

次に調べる予定の要素の集合に対して neighbors と名付けるのは、やや違和感があります。今調べている対象の要素に隣接し、次に調べる予定の要素に対して neighbor とつけるのは自然に感じます。

個人的には frontier または frontiers あたりが良いと思います。

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