Conversation
|
|
||
| anagram_groups = {} | ||
| for str, str_sorted in anagrams.items(): | ||
| if str_sorted not in anagram_groups: |
There was a problem hiding this comment.
結局この段階でif文のチェックを行うなら、一回目にstrを走査する段階でanagramに[str_sorted]が入っているかをチェックし入ってなかったら[]を入れるとした方が簡潔だと思います。
There was a problem hiding this comment.
仰るとおりだと思います.for文を回す回数もへらせそうなのでやってみます.ありがとうございます.
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| anagrams = [] | ||
| str_dict = {} | ||
| for str in strs: |
There was a problem hiding this comment.
pythonの組み込み関数と同じ名前になっている変数は避けた方が良いという意見があります。
この場合はstr()が組み込み関数として存在するので、sやstringが良いかもしれません。
組み込み関数やpythonに既に予約されている名前を回避するために、後ろに_をつけるという方法も慣習としてあるみたいです。(今回だとstr_)
There was a problem hiding this comment.
string は標準ライブラリにあるため自分なら避けるかもしれません。どのぐらい避けるべきかはわかっていないです。
https://docs.python.org/3.13/library/string.html
There was a problem hiding this comment.
str_などの書き方があることを始めて知りました.ありがとうございます.
There was a problem hiding this comment.
まず、名前が衝突していると、読む人が勘違いするかもしれないですね。
また、このコードを編集する人が、sum 関数を使おうとして、上で代入されている事に気が付かずに、コードを書きました。多分、プロダクションに行く前に気がつくでしょうが、デバッグをして、変数を付け直して、といったらすぐに1時間くらい経ちますね。
こういう風に遠い帰結に想像がいくかが知りたいです。
「痛い目にあったことがあり、それを学習しているか」ということです。
https://discord.com/channels/1084280443945353267/1370297676255592498/1372122379509301339
|
|
||
| for str in strs: | ||
| str_sorted = "".join(sorted(str)) | ||
| anagrams[str] = str_sorted |
There was a problem hiding this comment.
私自身もいい変数名が思いつかないのですが、anagramsと見てkeyが文字列、valueがその文字列をソートしたもの、と理解するのは難しい気がするので、str_to_sortedなどとした方がわかりやすい気がします。
There was a problem hiding this comment.
ご指摘ありがとうございます.str_to_sortedのほうがわかりやすそうですね.
| ```python | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| anagrams = defaultdict(list) |
There was a problem hiding this comment.
dict の変数名は、キーと値にどのようなものが含まれているかが分かりやすい変数名にすると、読み手にとって読みやすくなると思います。 sorted_to_anagrams はいかがでしょうか?
| ```python | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| anagrams = {} |
There was a problem hiding this comment.
Step2 のコードが読みやすいです。ここで anagrams を経由する理由はなにかありますか。手作業でstr が書かれたカードをアナグラムごとに分類するような設定で考えると、一旦各カードに sorted(str) を書き込み、もう一度先頭からグループ分けするということはしないと思います。各カードを見て sorted(str) に変換したら、その時点で、これまであったグループに合流させるか新規グループにするか、になる気がします。
There was a problem hiding this comment.
anagramsを経由した理由は,特に有りません.強いて言えば,一旦辞書を作ったほうが自分の中で理解がしやすかったためです.仰る通り,1回のfor文で,各カードをグループ分けしたほうが自然ですし,効率も良いと思います.
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| anagrams = [] | ||
| str_dict = {} | ||
| for str in strs: |
There was a problem hiding this comment.
まず、名前が衝突していると、読む人が勘違いするかもしれないですね。
また、このコードを編集する人が、sum 関数を使おうとして、上で代入されている事に気が付かずに、コードを書きました。多分、プロダクションに行く前に気がつくでしょうが、デバッグをして、変数を付け直して、といったらすぐに1時間くらい経ちますね。
こういう風に遠い帰結に想像がいくかが知りたいです。
「痛い目にあったことがあり、それを学習しているか」ということです。
https://discord.com/channels/1084280443945353267/1370297676255592498/1372122379509301339
| if s_sorted not in str_to_sorted: | ||
| str_to_sorted[s_sorted] = [] | ||
| str_to_sorted[s_sorted].append(s) |
There was a problem hiding this comment.
あ、素直な感じになりましたね。私はこの形が一番好みです。
setdefault などもあります。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.s92rv0w8n4r6
Problem:
https://leetcode.com/problems/group-anagrams/description/