LoginSignup
1
1

More than 5 years have passed since last update.

・連続した数を配列に入れる。・何通りか?(組合せ)

Last updated at Posted at 2019-03-10

①1〜5の連続した数を配列に入れる

m = (1..5) => 1..5
m = (1..5).to_a => [1,2,3,4,5]

to_aを使うと配列に変換される。

②文字列から何個抜き出すと何通りあるか?

文字列.combination
[1,2,3,4]の中からn個抜き取る。重複しない組み合わせ。逆順含まない(例[1,2][2,1]は含まない)

n = 2
[1,2,3,4].combination(n).to_a => [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]

文字列.permutation
[1,2,3,4]の中からn個抜き取る。重複しない組み合わせ。逆順含む(例[1,2][2,1]含む)

n = 2
[1,2,3,4].permutation(n).to_a => [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3]]
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1