Solved 49. Grouped Anagrams#4
Conversation
| 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) |
There was a problem hiding this comment.
なんで動くのかわからなくてけっこう考えてしまいました。
動かないコードを乗せるときは明示していただけると助かります。
| for s in word: | ||
| unicode_to_frequency[ord(s)] += 1 | ||
|
|
||
| return unicode_to_frequency |
There was a problem hiding this comment.
ここをこう変えれば動きますね
return tuple(sorted(unicode_to_frequency))
There was a problem hiding this comment.
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)There was a problem hiding this comment.
@fuga-98
追加のコメントありがとうございます!
これなら問題なさそうですね。
sorted_unicode_to_frequency = sorted([(u, f) for u, f in unicode_to_frequency.items()])
僕の感覚として、2次元のものをそのままsortの関数に入れると、どちらの軸を基準にソートされるかがパッとわからなくて嫌だなという感覚あります。一般的にはそうでもないんですかね?
There was a problem hiding this comment.
二次元のものをソートするものはわりと見る気がします。
ソートを使わないなら二次元のfrozensetにしても良いかもしれません。
There was a problem hiding this comment.
sorted というよりは dict の仕様ですね。iterable として解釈すると key を舐めます。
https://peps.python.org/pep-0234/#dictionary-iterators
frozendict もありますね。
There was a problem hiding this comment.
@oda
まずはレビューありがとうございます!
そうか。そこには思い至りませんでした!
sorted(iterable, /, *, key=None, reverse=False)
iterable の要素を並べ替えた新たなリストを返します。
https://docs.python.org/ja/3.13/library/functions.html#sorted
sortedがdictをiterableとして扱っていることに気づくべきでした。
There was a problem hiding this comment.
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
らしい。知らなかった。
| ```python | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| if not isinstance(strs, list): |
There was a problem hiding this comment.
Duck Typing, type, isinstanceの違いを見てみてもよいかもしれません
fuga-98/arai60#37 (comment)
There was a problem hiding this comment.
@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の違いを見てみてもよいかもしれません
ありがとうございます! 参照します!
| raise TypeError(f'expected: str, actual: {type(word)}') | ||
|
|
||
| unicode_to_frequency = defaultdict(int) | ||
| for s in word: |
There was a problem hiding this comment.
sが意味が入ってこないというのはご指摘の通りですね。
ただwordという変数名をつけたなら、characterよりletterかなとも思います。
一文字変数を使ってしまいましたがfor letter in wordとするのがいい気がしてきました。
| 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) |
There was a problem hiding this comment.
正確なことは分かりませんが、26文字のソートは計算量が少なそうです。
詳しい方教えてください。
There was a problem hiding this comment.
26文字のソートは計算量が少なそうです。
26文字のソートというのは何を指しますでしょうか?
アルファベットのソートは特別に高速でありそうだということでしょうか?
This problem
https://leetcode.com/problems/group-anagrams/
Next
https://leetcode.com/problems/intersection-of-two-arrays/