Skip to content

Create GroupAnagrams.md #2

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 3 commits into
base: main
Choose a base branch
from
Open

Create GroupAnagrams.md #2

wants to merge 3 commits into from

Conversation

aiueoriku
Copy link
Owner


anagram_groups = {}
for str, str_sorted in anagrams.items():
if str_sorted not in anagram_groups:
Copy link

Choose a reason for hiding this comment

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

結局この段階でif文のチェックを行うなら、一回目にstrを走査する段階でanagramに[str_sorted]が入っているかをチェックし入ってなかったら[]を入れるとした方が簡潔だと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

仰るとおりだと思います.for文を回す回数もへらせそうなのでやってみます.ありがとうございます.

def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = []
str_dict = {}
for str in strs:
Copy link

Choose a reason for hiding this comment

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

pythonの組み込み関数と同じ名前になっている変数は避けた方が良いという意見があります。
この場合はstr()が組み込み関数として存在するので、sやstringが良いかもしれません。
組み込み関数やpythonに既に予約されている名前を回避するために、後ろに_をつけるという方法も慣習としてあるみたいです。(今回だとstr_)

Choose a reason for hiding this comment

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

string は標準ライブラリにあるため自分なら避けるかもしれません。どのぐらい避けるべきかはわかっていないです。
https://docs.python.org/3.13/library/string.html

Copy link
Owner Author

Choose a reason for hiding this comment

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

str_などの書き方があることを始めて知りました.ありがとうございます.

Copy link

Choose a reason for hiding this comment

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

これですかねー。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.34fedgfeaxcw

まず、名前が衝突していると、読む人が勘違いするかもしれないですね。
また、このコードを編集する人が、sum 関数を使おうとして、上で代入されている事に気が付かずに、コードを書きました。多分、プロダクションに行く前に気がつくでしょうが、デバッグをして、変数を付け直して、といったらすぐに1時間くらい経ちますね。
こういう風に遠い帰結に想像がいくかが知りたいです。
「痛い目にあったことがあり、それを学習しているか」ということです。
https://discord.com/channels/1084280443945353267/1370297676255592498/1372122379509301339


for str in strs:
str_sorted = "".join(sorted(str))
anagrams[str] = str_sorted

Choose a reason for hiding this comment

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

私自身もいい変数名が思いつかないのですが、anagramsと見てkeyが文字列、valueがその文字列をソートしたもの、と理解するのは難しい気がするので、str_to_sortedなどとした方がわかりやすい気がします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご指摘ありがとうございます.str_to_sortedのほうがわかりやすそうですね.

```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = defaultdict(list)
Copy link

Choose a reason for hiding this comment

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

dict の変数名は、キーと値にどのようなものが含まれているかが分かりやすい変数名にすると、読み手にとって読みやすくなると思います。 sorted_to_anagrams はいかがでしょうか?

```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = {}

Choose a reason for hiding this comment

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

Step2 のコードが読みやすいです。ここで anagrams を経由する理由はなにかありますか。手作業でstr が書かれたカードをアナグラムごとに分類するような設定で考えると、一旦各カードに sorted(str) を書き込み、もう一度先頭からグループ分けするということはしないと思います。各カードを見て sorted(str) に変換したら、その時点で、これまであったグループに合流させるか新規グループにするか、になる気がします。

Copy link
Owner Author

@aiueoriku aiueoriku May 22, 2025

Choose a reason for hiding this comment

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

anagramsを経由した理由は,特に有りません.強いて言えば,一旦辞書を作ったほうが自分の中で理解がしやすかったためです.仰る通り,1回のfor文で,各カードをグループ分けしたほうが自然ですし,効率も良いと思います.

コメントを元に修正
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = []
str_dict = {}
for str in strs:
Copy link

Choose a reason for hiding this comment

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

これですかねー。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.34fedgfeaxcw

まず、名前が衝突していると、読む人が勘違いするかもしれないですね。
また、このコードを編集する人が、sum 関数を使おうとして、上で代入されている事に気が付かずに、コードを書きました。多分、プロダクションに行く前に気がつくでしょうが、デバッグをして、変数を付け直して、といったらすぐに1時間くらい経ちますね。
こういう風に遠い帰結に想像がいくかが知りたいです。
「痛い目にあったことがあり、それを学習しているか」ということです。
https://discord.com/channels/1084280443945353267/1370297676255592498/1372122379509301339

Comment on lines +65 to +67
if s_sorted not in str_to_sorted:
str_to_sorted[s_sorted] = []
str_to_sorted[s_sorted].append(s)
Copy link

Choose a reason for hiding this comment

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

あ、素直な感じになりましたね。私はこの形が一番好みです。
setdefault などもあります。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.s92rv0w8n4r6

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.

6 participants