Skip to content

127. Word Ladder#25

Open
Yuto729 wants to merge 2 commits intomainfrom
word-ladder
Open

127. Word Ladder#25
Yuto729 wants to merge 2 commits intomainfrom
word-ladder

Conversation

@Yuto729
Copy link
Copy Markdown
Owner

@Yuto729 Yuto729 commented Dec 30, 2025

解く問題

Word Ladder

次に解く問題

Maximum Depth Of Binary Tree

@Yuto729 Yuto729 changed the title Word Ladder 127. Word Ladder Dec 30, 2025
Repository owner deleted a comment from github-actions bot Dec 30, 2025
Repository owner deleted a comment from github-actions bot Dec 30, 2025

for i in range(len(word)):
for c in lowercase_letters:
candidate = word[:i] + c + word[i+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.

PEP8では:の両隣には同じだけ空白を入れることになっているので、[i+1:]か[i+1 : ]をおすすめします。

http://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements

また、i+1は個人的にはi + 1と空白を入れて書きたいと感じました。

Suggested change
candidate = word[:i] + c + word[i+1: ]
candidate = word[:i] + c + word[i + 1:]

return 0

word_set = set(wordList)
lowercase_letters = string.ascii_lowercase
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

Choose a reason for hiding this comment

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

したいならば
import foo.bar.bazaar as baz
from foo.bar import bazaar as baz
みたいにするといきなり名前をつけてインポートできますね。


neighbors = WordNeighbors()
for word in wordList:
neighbors.add(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.

add_wordですね。

Comment on lines +151 to +152
def __init__(self):
self._pattern_to_words = defaultdict(list)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

趣味の範囲ですが、wordListをforループでadd_wordしていく作業をコンストラクタに含めてもいいですね。

    def __init__(self, words=None):
        self._pattern_to_words = defaultdict(list)

        if words is not None:
            for word in words:
                self.add_word(word)

Comment on lines +83 to +86
for c in lowercase_letters:
candidate = word[:i] + c + word[i+1: ]
if candidate not in word_set or candidate in visited:
continue
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この辺りを自分ならジェネレータとして切り出す気がします。

def get_neighbors(word, word_set):
    for c in lowercase_letters:
        candidate = word[:i] + c + word[i+1:]
        if candidate in word_set:
            yield candidate

Comment on lines +213 to +224
def to_key(self, word):
for i in range(len(word)):
yield (word[:i], word[i+1:])

def add_word(self, word):
for key in self.to_key(word):
self.pattern_to_words[key].append(word)

def get_neighbors(self, word):
for key in self.to_key(word):
for w in self.pattern_to_words[key]:
yield w
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

to_keyメソッドはクラス内で利用するヘルパーメソッドだと思うので、_to_keyとして非公開にしたいなと思いました。_to_keyの非公開化に伴い定義位置をクラス末尾に移動したいと思いました。外部公開メソッドをより上に、非公開メソッドをより下にすることで意図が明確になり読みやすくなるという感覚です。

Suggested change
def to_key(self, word):
for i in range(len(word)):
yield (word[:i], word[i+1:])
def add_word(self, word):
for key in self.to_key(word):
self.pattern_to_words[key].append(word)
def get_neighbors(self, word):
for key in self.to_key(word):
for w in self.pattern_to_words[key]:
yield w
def add_word(self, word):
for key in self._to_key(word):
self.pattern_to_words[key].append(word)
def get_neighbors(self, word):
for key in self._to_key(word):
for w in self.pattern_to_words[key]:
yield w
def _to_key(self, word):
for i in range(len(word)):
yield (word[:i], word[i+1:])

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