-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Step1 | ||
何も見ずに解く | ||
```python | ||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
anagrams = [] | ||
str_dict = {} | ||
for str in strs: | ||
str_dict[str] = sorted(str) | ||
print(str_dict) | ||
``` | ||
出力 | ||
``` | ||
{'eat': ['a', 'e', 't'], 'tea': ['a', 'e', 't'], 'tan': ['a', 'n', 't'], 'ate': ['a', 'e', 't'], 'nat': ['a', 'n', 't'], 'bat': ['a', 'b', 't']} | ||
``` | ||
辞書のバリューから逆引きしたかったが,文法がわからず断念. | ||
ChatGPTに聞いた結果,str_dict.items()でキーとバリューが取得できる. | ||
listはハッシュに使えないので,予め文字列に変換したほうが良さそう. | ||
また,defaultdictという方法が有効だと感じたのでそれを使うことにする. | ||
|
||
|
||
# Step2 | ||
調べた結果を受けてときなおし | ||
```python | ||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
anagrams = defaultdict(list) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dict の変数名は、キーと値にどのようなものが含まれているかが分かりやすい変数名にすると、読み手にとって読みやすくなると思います。 sorted_to_anagrams はいかがでしょうか? |
||
for str in strs: | ||
str_sorted = ''.join(sorted(str)) | ||
anagrams[str_sorted].append(str) | ||
return list(anagrams.values()) | ||
``` | ||
defaultdictという文法を使うことで簡潔にかけたが,初期の方針でも書けるようにしたほうが良い気がする. | ||
|
||
# Step3 | ||
初期の方針,つまりdefauldictを用いらずに解く | ||
```python | ||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
anagrams = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Step2 のコードが読みやすいです。ここで anagrams を経由する理由はなにかありますか。手作業でstr が書かれたカードをアナグラムごとに分類するような設定で考えると、一旦各カードに sorted(str) を書き込み、もう一度先頭からグループ分けするということはしないと思います。各カードを見て sorted(str) に変換したら、その時点で、これまであったグループに合流させるか新規グループにするか、になる気がします。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anagramsを経由した理由は,特に有りません.強いて言えば,一旦辞書を作ったほうが自分の中で理解がしやすかったためです.仰る通り,1回のfor文で,各カードをグループ分けしたほうが自然ですし,効率も良いと思います. |
||
|
||
for str in strs: | ||
str_sorted = "".join(sorted(str)) | ||
anagrams[str] = str_sorted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 私自身もいい変数名が思いつかないのですが、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ご指摘ありがとうございます.str_to_sortedのほうがわかりやすそうですね. |
||
|
||
anagram_groups = {} | ||
for str, str_sorted in anagrams.items(): | ||
if str_sorted not in anagram_groups: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 結局この段階でif文のチェックを行うなら、一回目にstrを走査する段階でanagramに[str_sorted]が入っているかをチェックし入ってなかったら[]を入れるとした方が簡潔だと思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 仰るとおりだと思います.for文を回す回数もへらせそうなのでやってみます.ありがとうございます. |
||
anagram_groups[str_sorted] = [] | ||
anagram_groups[str_sorted].append(str) | ||
|
||
return [x for x in anagram_groups.values()] | ||
``` | ||
strs = ["",""]に対して,出力がstrs = | ||
[""]となりエラー. | ||
|
||
# Step4 | ||
コメントを元に解き直し | ||
```python | ||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
str_to_sorted = {} | ||
for s in strs: | ||
s_sorted = "".join(sorted(s)) | ||
if s_sorted not in str_to_sorted: | ||
str_to_sorted[s_sorted] = [] | ||
str_to_sorted[s_sorted].append(s) | ||
Comment on lines
+65
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あ、素直な感じになりましたね。私はこの形が一番好みです。 |
||
return [word for word in str_to_sorted.values()] | ||
``` | ||
Step3でおきたエラーも消えた. |
There was a problem hiding this comment.
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_)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
str_などの書き方があることを始めて知りました.ありがとうございます.
There was a problem hiding this comment.
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