Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions word_ladder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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.

leetcodeならば、関数名はともかく、引数となっている変数名に関しては変更可能です。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python の標準的な naming convention では、snake 使いますね。
https://peps.python.org/pep-0008/#naming-conventions

Python は呼び出し方が、位置とキーワードの2つがあるので、本当は変更すると動かなくなる可能性はあります。

def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):

https://docs.python.org/3/tutorial/controlflow.html#special-parameters


```py
from collections import deque

class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
wordSet = set(wordList)
if endWord not in wordSet:
return 0

queue = deque([(beginWord, 1)])
while queue:
word, steps = queue.popleft()
if word == endWord:
return steps

for i in range(len(word)):
for ch in range(26):
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
Owner Author

Choose a reason for hiding this comment

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

確かにそうですね。こちらで指摘されているように、string.ascii_lowercase を使うとよいかもしれないと思いました。
SuperHotDogCat/coding-interview#45 (comment)

transformed = word[:i] + chr(ord('a') + ch) + word[i+1:]
if transformed in wordSet:
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
Owner Author

Choose a reason for hiding this comment

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

なるほど、ご意見ありがとうございます!!

wordSet.remove(transformed)
queue.append((transformed, steps + 1))
return 0
```

2回目。変数をリネームした(入力はそのままキャメルケースだが、新たに宣言する変数はスネークケースにした。混在するのは欠点だが、入力とローカル変数の区別がしやすい利点もある)。

```py
from collections import deque

class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
word_set = set(wordList)
if endWord not in word_set:
return 0

queue = deque([(beginWord, 1)])

while queue:
word, num_steps = queue.popleft()

if word == endWord:
return num_steps

for i in range(len(word)):
for alpha_offset in range(26):
transformed_word = word[:i] + chr(ord('a') + alpha_offset) + word[i + 1:]
if transformed_word in word_set:
word_set.remove(transformed_word)
queue.append((transformed_word, num_steps + 1))
return 0
```

3回目。

```py
from collections import deque

class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
word_set = set(wordList)
if endWord not in word_set:
return 0

queue = deque([(beginWord, 1)])

while queue:
word, num_steps = queue.popleft()
if word == endWord:
return num_steps
for i in range(len(word)):
for alpha_offset in range(26):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

マジックナンバーを使っているので、自分ならstring.ascii_lowercaseを使うか、26は別で定義します。

transformed_word = word[:i] + chr(ord('a') + alpha_offset) + 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.

ord('a') + alpha_offsetという部分に関して、以下のリンクが参考になるかもしれません。
t0hsumi/leetcode#12 (comment)

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.

とても参考になります、ありがとうございます!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

文字列の連結には、f-stringもしくはjoinを使うと良いと思います。
https://google.github.io/styleguide/pyguide.html#310-strings

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.

これは知りませんでした。ありがとうございます!

if transformed_word in word_set:
word_set.remove(transformed_word)
queue.append((transformed_word, num_steps + 1))
return 0
```