回答編集履歴

3

追記と成形

2017/11/01 08:02

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -1,3 +1,99 @@
1
+ [リンク先の問題](https://www.hackerrank.com/challenges/most-commons/problem)を見る限り...
2
+
3
+ > **Output Format**
4
+
5
+ > Print the three most common characters along with their occurrence count each on a separate line.
6
+
7
+ > Sort output in descending order of occurrence count.
8
+
9
+ > If the occurrence count is the same, sort the characters in ascending order.
10
+
11
+
12
+
13
+ ここでの**ascending**とは、アルファベット順のことではないでしょうか?
14
+
15
+ そう解釈してよいのなら、辞書に要素を追加した順を意識する必要はないはずです。
16
+
17
+ [Counter.most_common](https://docs.python.jp/3/library/collections.html#collections.Counter.most_common)で出現回数順に並び変え、その後にアルファベット順にソートすればよいかと。
18
+
19
+
20
+
21
+ ```Python
22
+
23
+ from collections import Counter
24
+
25
+
26
+
27
+ counter = Counter(input())
28
+
29
+
30
+
31
+ for k, v in sorted(counter.most_common(), key=lambda x: x[0])[:3]:
32
+
33
+ print(k, v)
34
+
35
+ ```
36
+
37
+
38
+
39
+ 動作テストはしていないので、何かバグがあるかもしれませんが。
40
+
41
+ **追記:バグ、ありました。ちょっと修正します。**
42
+
43
+
44
+
45
+ ---
46
+
47
+ 修正しました。結局Counter.most_commonは使ってないです。
48
+
49
+ ```Python
50
+
51
+ from collections import Counter
52
+
53
+
54
+
55
+ counter = Counter(input())
56
+
57
+ for k, v in sorted(counter.items(), key=lambda x: (-x[1], x[0]))[:3]:
58
+
59
+ print(k, v)
60
+
61
+ ```
62
+
63
+
64
+
65
+ ---
66
+
67
+ さらに改造しました。こっちの方がわかりやすい気もしますね。
68
+
69
+ ```Python
70
+
71
+ from collections import Counter
72
+
73
+ from operator import itemgetter
74
+
75
+
76
+
77
+ counter_list = list(Counter(input()).items())
78
+
79
+ counter_list.sort(key=itemgetter(0))
80
+
81
+ counter_list.sort(key=itemgetter(1), reverse=True)
82
+
83
+
84
+
85
+ for k, v in counter_list[:3]:
86
+
87
+ print(k, v)
88
+
89
+ ```
90
+
91
+
92
+
93
+ 順番が安定しない理由
94
+
95
+ ---
96
+
1
97
  Counterは要素の挿入順序を記憶しないからです。
2
98
 
3
99
  引用元:[Python標準ライブラリ - 8.3.2. Counter オブジェクト](https://docs.python.jp/3/library/collections.html#collections.Counter)
@@ -47,71 +143,3 @@
47
143
  > return self.__class__, (OrderedDict(self),)
48
144
 
49
145
  > ```
50
-
51
-
52
-
53
- ---
54
-
55
- ただし、[リンク先の問題](https://www.hackerrank.com/challenges/most-commons/problem)を見る限り...
56
-
57
- > **Output Format**
58
-
59
- > Print the three most common characters along with their occurrence count each on a separate line.
60
-
61
- > Sort output in descending order of occurrence count.
62
-
63
- > If the occurrence count is the same, sort the characters in ascending order.
64
-
65
-
66
-
67
- ここでの**ascending**とは、アルファベット順のことではないでしょうか?
68
-
69
- そう解釈してよいのなら、辞書に要素を追加した順を意識する必要はないはずです。
70
-
71
- [Counter.most_common](https://docs.python.jp/3/library/collections.html#collections.Counter.most_common)で出現回数順に並び変え、その後にアルファベット順にソートすればよいかと。
72
-
73
-
74
-
75
- ```Python
76
-
77
- from collections import Counter
78
-
79
-
80
-
81
- counter = Counter(input())
82
-
83
-
84
-
85
- for k, v in sorted(counter.most_common(), key=lambda x: x[0])[:3]:
86
-
87
- print(k, v)
88
-
89
- ```
90
-
91
-
92
-
93
- 動作テストはしていないので、何かバグがあるかもしれませんが。
94
-
95
- **追記:バグ、ありました。ちょっと修正します。**
96
-
97
-
98
-
99
- 修正
100
-
101
- ---
102
-
103
- 修正しました。結局Counter.most_commonは使ってないです。
104
-
105
- ```Python
106
-
107
- from collections import Counter
108
-
109
-
110
-
111
- counter = Counter(input())
112
-
113
- for k, v in sorted(counter.items(), key=lambda x: (-x[1], x[0]))[:3]:
114
-
115
- print(k, v)
116
-
117
- ```

2

追記

2017/11/01 08:02

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -93,3 +93,25 @@
93
93
  動作テストはしていないので、何かバグがあるかもしれませんが。
94
94
 
95
95
  **追記:バグ、ありました。ちょっと修正します。**
96
+
97
+
98
+
99
+ 修正
100
+
101
+ ---
102
+
103
+ 修正しました。結局Counter.most_commonは使ってないです。
104
+
105
+ ```Python
106
+
107
+ from collections import Counter
108
+
109
+
110
+
111
+ counter = Counter(input())
112
+
113
+ for k, v in sorted(counter.items(), key=lambda x: (-x[1], x[0]))[:3]:
114
+
115
+ print(k, v)
116
+
117
+ ```

1

追記

2017/11/01 07:45

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -91,3 +91,5 @@
91
91
 
92
92
 
93
93
  動作テストはしていないので、何かバグがあるかもしれませんが。
94
+
95
+ **追記:バグ、ありました。ちょっと修正します。**