回答編集履歴
3
追記と成形
answer
CHANGED
@@ -1,31 +1,4 @@
|
|
1
|
-
Counterは要素の挿入順序を記憶しないからです。
|
2
|
-
引用元:[Python標準ライブラリ - 8.3.2. Counter オブジェクト](https://docs.python.jp/3/library/collections.html#collections.Counter)
|
3
|
-
> **class collections.Counter([iterable-or-mapping])**
|
4
|
-
> Counter はハッシュ可能なオブジェクトをカウントする dict のサブクラスです。これは、要素を辞書のキーとして保存し、そのカウントを辞書の値として保存する、順序付けされていないコレクションです。
|
5
|
-
|
6
|
-
|
7
|
-
一方、OrderedDictは要素の挿入順序を記憶します。
|
8
|
-
引用元:[Python標準ライブラリ- 8.3.6. OrderedDict オブジェクト](https://docs.python.jp/3/library/collections.html#collections.OrderedDict)
|
9
|
-
> **class collections.OrderedDict([items])**
|
10
|
-
> 通常の dict メソッドをサポートする、辞書のサブクラスのインスタンスを返します。 OrderedDict は、キーが最初に追加された順序を記憶します。新しい項目が既存の項目を上書きしても、元の挿入位置は変わらないままです。
|
11
|
-
|
12
|
-
このように、これらの用途は異なります。
|
13
|
-
なお、[OrderedCounter](https://docs.python.jp/3/library/collections.html#ordereddict-examples-and-recipes)たるクラスが実装例として紹介されています。
|
14
|
-
> 順序付き辞書と Counter クラスを組み合わせると、要素が最初に現れた順を記憶するカウンタができます:
|
15
|
-
|
16
|
-
> ```
|
17
|
-
> class OrderedCounter(Counter, OrderedDict):
|
18
|
-
> 'Counter that remembers the order elements are first encountered'
|
19
|
-
|
20
|
-
> def __repr__(self):
|
21
|
-
> return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
|
22
|
-
|
23
|
-
> def __reduce__(self):
|
24
|
-
> return self.__class__, (OrderedDict(self),)
|
25
|
-
> ```
|
26
|
-
|
27
|
-
---
|
28
|
-
|
1
|
+
[リンク先の問題](https://www.hackerrank.com/challenges/most-commons/problem)を見る限り...
|
29
2
|
> **Output Format**
|
30
3
|
> Print the three most common characters along with their occurrence count each on a separate line.
|
31
4
|
> Sort output in descending order of occurrence count.
|
@@ -47,7 +20,6 @@
|
|
47
20
|
動作テストはしていないので、何かバグがあるかもしれませんが。
|
48
21
|
**追記:バグ、ありました。ちょっと修正します。**
|
49
22
|
|
50
|
-
修正
|
51
23
|
---
|
52
24
|
修正しました。結局Counter.most_commonは使ってないです。
|
53
25
|
```Python
|
@@ -56,4 +28,46 @@
|
|
56
28
|
counter = Counter(input())
|
57
29
|
for k, v in sorted(counter.items(), key=lambda x: (-x[1], x[0]))[:3]:
|
58
30
|
print(k, v)
|
59
|
-
```
|
31
|
+
```
|
32
|
+
|
33
|
+
---
|
34
|
+
さらに改造しました。こっちの方がわかりやすい気もしますね。
|
35
|
+
```Python
|
36
|
+
from collections import Counter
|
37
|
+
from operator import itemgetter
|
38
|
+
|
39
|
+
counter_list = list(Counter(input()).items())
|
40
|
+
counter_list.sort(key=itemgetter(0))
|
41
|
+
counter_list.sort(key=itemgetter(1), reverse=True)
|
42
|
+
|
43
|
+
for k, v in counter_list[:3]:
|
44
|
+
print(k, v)
|
45
|
+
```
|
46
|
+
|
47
|
+
順番が安定しない理由
|
48
|
+
---
|
49
|
+
Counterは要素の挿入順序を記憶しないからです。
|
50
|
+
引用元:[Python標準ライブラリ - 8.3.2. Counter オブジェクト](https://docs.python.jp/3/library/collections.html#collections.Counter)
|
51
|
+
> **class collections.Counter([iterable-or-mapping])**
|
52
|
+
> Counter はハッシュ可能なオブジェクトをカウントする dict のサブクラスです。これは、要素を辞書のキーとして保存し、そのカウントを辞書の値として保存する、順序付けされていないコレクションです。
|
53
|
+
|
54
|
+
|
55
|
+
一方、OrderedDictは要素の挿入順序を記憶します。
|
56
|
+
引用元:[Python標準ライブラリ- 8.3.6. OrderedDict オブジェクト](https://docs.python.jp/3/library/collections.html#collections.OrderedDict)
|
57
|
+
> **class collections.OrderedDict([items])**
|
58
|
+
> 通常の dict メソッドをサポートする、辞書のサブクラスのインスタンスを返します。 OrderedDict は、キーが最初に追加された順序を記憶します。新しい項目が既存の項目を上書きしても、元の挿入位置は変わらないままです。
|
59
|
+
|
60
|
+
このように、これらの用途は異なります。
|
61
|
+
なお、[OrderedCounter](https://docs.python.jp/3/library/collections.html#ordereddict-examples-and-recipes)たるクラスが実装例として紹介されています。
|
62
|
+
> 順序付き辞書と Counter クラスを組み合わせると、要素が最初に現れた順を記憶するカウンタができます:
|
63
|
+
|
64
|
+
> ```
|
65
|
+
> class OrderedCounter(Counter, OrderedDict):
|
66
|
+
> 'Counter that remembers the order elements are first encountered'
|
67
|
+
|
68
|
+
> def __repr__(self):
|
69
|
+
> return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
|
70
|
+
|
71
|
+
> def __reduce__(self):
|
72
|
+
> return self.__class__, (OrderedDict(self),)
|
73
|
+
> ```
|
2
追記
answer
CHANGED
@@ -45,4 +45,15 @@
|
|
45
45
|
```
|
46
46
|
|
47
47
|
動作テストはしていないので、何かバグがあるかもしれませんが。
|
48
|
-
**追記:バグ、ありました。ちょっと修正します。**
|
48
|
+
**追記:バグ、ありました。ちょっと修正します。**
|
49
|
+
|
50
|
+
修正
|
51
|
+
---
|
52
|
+
修正しました。結局Counter.most_commonは使ってないです。
|
53
|
+
```Python
|
54
|
+
from collections import Counter
|
55
|
+
|
56
|
+
counter = Counter(input())
|
57
|
+
for k, v in sorted(counter.items(), key=lambda x: (-x[1], x[0]))[:3]:
|
58
|
+
print(k, v)
|
59
|
+
```
|
1
追記
answer
CHANGED
@@ -44,4 +44,5 @@
|
|
44
44
|
print(k, v)
|
45
45
|
```
|
46
46
|
|
47
|
-
動作テストはしていないので、何かバグがあるかもしれませんが。
|
47
|
+
動作テストはしていないので、何かバグがあるかもしれませんが。
|
48
|
+
**追記:バグ、ありました。ちょっと修正します。**
|