Skip to content

347. Top K Frequent Elements.md #9

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 1 commit into
base: main
Choose a base branch
from

Conversation

katataku
Copy link
Owner

count_frequency[num] += 1
else:
count_frequency[num] = 1
return list(map(lambda x:x[0], sorted(count_frequency.items(), key=lambda x:x[1], reverse=True)[:k]))
Copy link

Choose a reason for hiding this comment

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

一行に詰め込み過ぎており、読むにあたり認知負荷が高く感じられました。

Copy link

Choose a reason for hiding this comment

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

fhiyo/leetcode#17 (comment)
7つあたりが限界に思いますね。
それに、これ、実行順序が、items, sorted, slice, map, list ですよね。目が左右に動きます。
sorted の結果を一回変数に置きたいです。

それはそうとして、文法への慣れの問題はあると思っています。いくつか案。

list(map(lambda x:x[0], items))

この形は

[k for k, _ in items]

のほうが見やすいでしょう。

また、sorted を使うならば、

sorted(count_frequency, key=count_frequency.get, reverse=True)[:k]

も手です。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

文法への慣れの問題

すごくご指摘通りで、リストの内包表記が慣れないなと思ってるところでした。(書いてあったら読めるんですが、どうも自分で書く際の選択肢に出てこないです。。)
慣れ、ということでしばらく挑戦してみます!

Comment on lines +35 to +38
if num in count_frequency:
count_frequency[num] += 1
else:
count_frequency[num] = 1
Copy link

Choose a reason for hiding this comment

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

これも defaultdict 使わないとしてもいくつかあって、

if num not in count_frequency:
    count_frequency[num] = 0
count_frequency[num] += 1

count_frequency.setdefault(num, 0)
count_frequency[num] += 1

count_frequency[num] = count_frequency.get(num, 0) + 1

ですかね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

以下自分メモ:
dictが持ってるメソッドを確認した。
https://docs.python.org/3/library/stdtypes.html#mapping-types-dict

Copy link

Choose a reason for hiding this comment

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

あ、そうそう。これを開いて読むの大事です。翻訳でもいいです。

Comment on lines +16 to +19
if num in count_frequency:
count_frequency[num] += 1
else:
count_frequency[num] = 1

Choose a reason for hiding this comment

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

こっちの書き方のほうが個人的には好きです。(が、趣味の範疇な気がします)

Suggested change
if num in count_frequency:
count_frequency[num] += 1
else:
count_frequency[num] = 1
if num not in count_frequency:
count_frequency[num] = 0
count_frequency[num] += 1

Comment on lines +22 to +23
for key, value in sorted(count_frequency.items(), key=lambda x:x[1], reverse=True)[:k]:
top_k_elements.append(key)

Choose a reason for hiding this comment

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

ループの対象がわかりづらいので、事前に別変数で定義したほうが可読性が上がりそうです。(frequency_ranking は ChatGPT が変数名案として返してきたものなんですが、意外とわかりやすいかもと思ってここで採用してみました)

また key, value は汎用的な用語なので、この問題に適した変数名にしたほうがよさそうです。(value は使っていないのでそもそも _ でよいですね)

Suggested change
for key, value in sorted(count_frequency.items(), key=lambda x:x[1], reverse=True)[:k]:
top_k_elements.append(key)
frequency_ranking = sorted(count_frequency.items(), key=lambda x:x[1], reverse=True)
for element, _ in frequency_ranking[:k]:
top_k_elements.append(element)

- 別のパターン
- `dict()`のかわりに`defaultdict()`をつかう
- https://github.com/thonda28/leetcode/pull/17/files#r1769492893
- top kを選ぶときにheapqを使う。

Choose a reason for hiding this comment

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

優先度付きキューとかは割と見るイメージがあるので、(heapq ライブラリではなく)自分で一度実装してみるのもおもしろいと思います。

if num in count_frequency:
count_frequency[num] += 1
else:
count_frequency[num] = 1

Choose a reason for hiding this comment

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

step2以降でお気づきですが、default_dictでこのあたりの初期化処理をなくせそうですね

Copy link

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

実はPythonだとCounterにmost_commonというのがあり、簡単にかけます。とはいえこの問題が求めていることではないでしょうが。

https://docs.python.org/3/library/collections.html#collections.Counter
https://docs.python.org/3/library/collections.html#collections.Counter.most_common
https://github.com/python/cpython/blob/d610d821fd210dce63a1132c274ffdf8acc510bc/Lib/collections/__init__.py#L619

まああとはクイックセレクトとかバケットソートとかでも書いてみると良いと思います。

Copy link

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

実はPythonだとCounterにmost_commonというのがあり、簡単にかけます。とはいえこの問題が求めていることではないでしょうが。

https://docs.python.org/3/library/collections.html#collections.Counter
https://docs.python.org/3/library/collections.html#collections.Counter.most_common
https://github.com/python/cpython/blob/d610d821fd210dce63a1132c274ffdf8acc510bc/Lib/collections/__init__.py#L619

まああとはクイックセレクトとかバケットソートとかでも書いてみると良いと思います。

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