回答編集履歴
2
importしてない
test
CHANGED
@@ -20,6 +20,8 @@
|
|
20
20
|
|
21
21
|
```Python3
|
22
22
|
|
23
|
+
from itertools import combinations
|
24
|
+
|
23
25
|
len([x for x in combinations(range(1, 44), 6) if sum(x) <= 52])
|
24
26
|
|
25
27
|
```
|
1
countよりsizeのほうが速そう
test
CHANGED
@@ -4,10 +4,26 @@
|
|
4
4
|
|
5
5
|
```Ruby
|
6
6
|
|
7
|
-
(1..43).to_a.combination(6).map(&:sum).select(&52.method(:>=)).
|
7
|
+
(1..43).to_a.combination(6).map(&:sum).select(&52.method(:>=)).size
|
8
8
|
|
9
9
|
```
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
+
これをPythonに書き直せば求めるコードになります。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
と言いたいところですが、Pythonはmapやfilterは邪道という文化ですので、リスト内包表記を使います。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```Python3
|
22
|
+
|
23
|
+
len([x for x in combinations(range(1, 44), 6) if sum(x) <= 52])
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
|
28
|
+
|
13
|
-
|
29
|
+
なお、最適化を全くしていないので、これよりも速いアルゴリズムは存在します。
|