teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

h_a さんの指摘で修正

2017/05/29 17:03

投稿

oskbt
oskbt

スコア1895

answer CHANGED
@@ -1,5 +1,10 @@
1
- repeated_combinationを使えばシンプルに書けますね。
1
+ repeated_combinationを使えばシンプルに書けますね。 <=間違いでした。
2
+
2
-
3
+ (追記)
4
+ h_aさんの指摘で間違いに気づきました。失礼しました。
5
+ 正解はrepeated_permutation でした。
6
+ (追記おしまい)
7
+
3
8
  ```ruby
4
9
 
5
10
  ary = {} # ary=[] でも好きな方で
@@ -7,7 +12,7 @@
7
12
  n = 10 # (1..10)
8
13
  m = 10 # [a * b * c * d * e * f * g * h * i * j]
9
14
 
10
- (1..n).to_a.repeated_combination(m){|comb|
15
+ (1..n).to_a.repeated_permutation(m){|comb|
11
16
 
12
17
  index = comb.reduce(:*)
13
18
  ary[index] = 0 if ary[index].nil?

2

nilのとき初期化をしないと駄目でした。

2017/05/29 17:03

投稿

oskbt
oskbt

スコア1895

answer CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```ruby
4
4
 
5
- ary = {}
5
+ ary = {} # ary=[] でも好きな方で
6
6
 
7
7
  n = 10 # (1..10)
8
8
  m = 10 # [a * b * c * d * e * f * g * h * i * j]
@@ -10,6 +10,7 @@
10
10
  (1..n).to_a.repeated_combination(m){|comb|
11
11
 
12
12
  index = comb.reduce(:*)
13
+ ary[index] = 0 if ary[index].nil?
13
14
  ary[index] += 1
14
15
 
15
16
  }

1

分かりやすいようにコメント追加

2017/05/29 16:56

投稿

oskbt
oskbt

スコア1895

answer CHANGED
@@ -4,8 +4,8 @@
4
4
 
5
5
  ary = {}
6
6
 
7
- n = 10
7
+ n = 10 # (1..10)
8
- m = 10
8
+ m = 10 # [a * b * c * d * e * f * g * h * i * j]
9
9
 
10
10
  (1..n).to_a.repeated_combination(m){|comb|
11
11