Skip to content

Group Anagrams step1-3 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Group Anagrams step1-3 #13

wants to merge 1 commit into from

Conversation

bumbuboon
Copy link
Owner

Copy link

@t0hsumi t0hsumi left a comment

Choose a reason for hiding this comment

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

いいと思います

```
characters_to_wordじゃなくてcharacters_to_anagramsの方がわかりやすいか?

tuple(sorted(string))じゃなくてstr(sorted(string))でもいいのか。
Copy link

Choose a reason for hiding this comment

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

ここでstrを使った気持ちとしては、

みたいな流れです。charの出現の組み合わせという意味だとtupleの方が適当ですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

詳しい解説ありがとうございます。参考になります。

return list(characters_to_word.values())
```

sortedは文字列に使った場合、個々が分けられたlistを返す。上のコードは `tuple(sorted(word))` で十分
Copy link

Choose a reason for hiding this comment

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

sorted は iterable をソートしますね。str は iterable ですね。

def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
character_to_word = {}
for word in strs:
characters_tuple = sorted(tuple(word))

Choose a reason for hiding this comment

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

tupleに対して_tupleという接尾辞を持った変数名は冗長だと思っています。
tupleであることを強調したいとのであればこれで良いと思います。

sorted_charactersなどのほうがやろうとしていることに対して直截な名前で読みやすいです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにsortedかtupleのどちらを入れるかと言われればsortedを変数名に入れた方が良いと感じました。
長いコードだと変数の型を忘れてしまうことがあるので型を名前に含める癖がついてます。

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