Skip to content

Solved 49. Grouped Anagrams#4

Open
chanseok-lim wants to merge 3 commits into
mainfrom
49
Open

Solved 49. Grouped Anagrams#4
chanseok-lim wants to merge 3 commits into
mainfrom
49

Conversation

@chanseok-lim
Copy link
Copy Markdown
Owner

Comment thread 49/49.md
unicode_frequency_maps = defaultdict(list)
for s in strs:
uc_to_freq = self.count_unicodes_in_word(s)
unicode_frequency_maps[uc_to_freq].append(s)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

なんで動くのかわからなくてけっこう考えてしまいました。
動かないコードを乗せるときは明示していただけると助かります。

Comment thread 49/49.md
for s in word:
unicode_to_frequency[ord(s)] += 1

return unicode_to_frequency
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここをこう変えれば動きますね
return tuple(sorted(unicode_to_frequency))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sortの仕様を間違えて覚えていました。正しくはこうですね。

sorted_unicode_to_frequency = sorted([(u, f) for u, f in unicode_to_frequency.items()])
return tuple(sorted_unicode_to_frequency)
sorted_unicode_to_frequency = sorted(unicode_to_frequency.items(), key=lambda x:x[0])
return tuple(sorted_unicode_to_frequency)

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.

@fuga-98
追加のコメントありがとうございます!
これなら問題なさそうですね。

sorted_unicode_to_frequency = sorted([(u, f) for u, f in unicode_to_frequency.items()])

僕の感覚として、2次元のものをそのままsortの関数に入れると、どちらの軸を基準にソートされるかがパッとわからなくて嫌だなという感覚あります。一般的にはそうでもないんですかね?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

二次元のものをソートするものはわりと見る気がします。
ソートを使わないなら二次元のfrozensetにしても良いかもしれません。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sorted というよりは dict の仕様ですね。iterable として解釈すると key を舐めます。
https://peps.python.org/pep-0234/#dictionary-iterators

frozendict もありますね。

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.

@oda
まずはレビューありがとうございます!

そうか。そこには思い至りませんでした!

sorted(iterable, /, *, key=None, reverse=False)
iterable の要素を並べ替えた新たなリストを返します。
https://docs.python.org/ja/3.13/library/functions.html#sorted

sortedがdictをiterableとして扱っていることに気づくべきでした。

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.

dictはsortedに投げても上手く働いたのでsortedにエラーを吐かせたくなった。
そこでtupleのlistを投げるとエラーとならず出力を得た。

tuple_list = [(1, 2), (5, 6), (3, 9)]
tuple_list = sorted(tuple_list )
print(tuple_list )
# 出力: [(1, 2), (3, 9), (5, 6)]

Lexicographical comparison between built-in collections works as follows:
For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal (for example, [1,2] == (1,2) is false because the type is not the same).
Collections that support order comparison are ordered the same as their first unequal elements (for example, [1,2,x] <= [1,2,y] has the same value as x <= y). If a corresponding element does not exist, the shorter collection is ordered first (for example, [1,2] < [1,2,3] is true).
https://docs.python.org/3/reference/expressions.html#value-comparisons

らしい。知らなかった。

Comment thread 49/49.md
```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
if not isinstance(strs, list):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Duck Typing, type, isinstanceの違いを見てみてもよいかもしれません
fuga-98/arai60#37 (comment)

Copy link
Copy Markdown
Owner Author

@chanseok-lim chanseok-lim Apr 30, 2025

Choose a reason for hiding this comment

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

@fuga-98
まずはレビューありがとうございます!

動かないコードを乗せるときは明示していただけると助かります。

そうですね。次からそのようにします!
あのコードが動かないのはdictのkeyとしてhashableではないdictを使っているからであるというのが僕の理解です。

ここをこう変えれば動きますね
return tuple(sorted(unicode_to_frequency))

めちゃくちゃ興味深いです!
dictをsortedした時の挙動を初めて知りました。keyのlistが返ってくるんですね。
そしてlistをtuple (immutable; immutableならhashable) に変換してkeyとして与えると。
テストをクリアする面白いアイディアだと思うのですが、この実装ですと"aa"と"a"が同じアナグラムとして扱われるのでは?

Duck Typing, type, isinstanceの違いを見てみてもよいかもしれません

ありがとうございます! 参照します!

Copy link
Copy Markdown

@fuga-98 fuga-98 Apr 30, 2025

Choose a reason for hiding this comment

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

すみません、不正確な情報を書いてしまいました。

Comment thread 49/49.md
raise TypeError(f'expected: str, actual: {type(word)}')

unicode_to_frequency = defaultdict(int)
for s in word:
Copy link
Copy Markdown

@fuga-98 fuga-98 Apr 30, 2025

Choose a reason for hiding this comment

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

文字はsではなくcharacterのcが普通ですかね

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.

sが意味が入ってこないというのはご指摘の通りですね。
ただwordという変数名をつけたなら、characterよりletterかなとも思います。

一文字変数を使ってしまいましたがfor letter in wordとするのがいい気がしてきました。

Comment thread 49/49.md
sortedの計算量はO(nlogn)らしい<br>
[https://stackoverflow.com/questions/14434490/what-is-the-complexity-of-the-sorted-function](https://stackoverflow.com/questions/14434490/what-is-the-complexity-of-the-sorted-function)<br>
すると
計算量:O(mnlogn), m, n = strs.length, max(strs[i].length)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

正確なことは分かりませんが、26文字のソートは計算量が少なそうです。
詳しい方教えてください。

Copy link
Copy Markdown
Owner Author

@chanseok-lim chanseok-lim May 1, 2025

Choose a reason for hiding this comment

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

26文字のソートは計算量が少なそうです。

26文字のソートというのは何を指しますでしょうか?
アルファベットのソートは特別に高速でありそうだということでしょうか?

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.

3 participants