2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

๐Ÿถ็ต„ใฟๅˆใ‚ใ›ใ‚’ๅˆ—ๆŒ™ใ™ใ‚‹ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใซใคใ„ใฆ(Ruby)

Last updated at Posted at 2018-11-03

็ต„ใฟๅˆใ‚ใ›

Rubyใง็ต„ใฟๅˆใ‚ใ›ใ‚’ๆฑ‚ใ‚ใ‚‹ๆฉŸไผšใŒใ‚ใฃใŸใ€‚
Rubyใซใฏไพฟๅˆฉใชใƒกใ‚ฝใƒƒใƒ‰ใŒใ‚ใฃใฆใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซๆฑ‚ใ‚ใ‚‹ใ“ใจใŒใงใใ‚‹ใ€‚

arr = [1, 2, 3, 4, 5]
p arr.combination(2).to_a

# OUTPUT
[[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]

ใ—ใ‹ใ—ใ€ใ“ใ‚Œใ‚’ๅฎŸ้š›ใซ่‡ชๅˆ†ใงๅฎŸ่ฃ…ใ—ใ‚ˆใ†ใจใ™ใ‚‹ใจๆ„ๅค–ใจ้›ฃใ—ใ„ใชใจๆ„Ÿใ˜ใŸใฎใงใพใจใ‚ใฆใฟใ‚‹ใ€‚

ๅฎŸ่ฃ…

่€ƒใˆๆ–น

็›ดๆ„Ÿ็š„ใช่€ƒใˆๆ–นใฏใ€่‡ชๅˆ†ใงๆจนๅฝขๅ›ณใ‚’ๆ›ธใใƒ—ใƒญใ‚ปใ‚นใงใ‚ใ‚‹ใ€‚
ไพ‹ใˆใฐใ€[1,2,3,4]ใ‹ใ‚‰3ใคใฎ่ฆ็ด ใ‚’ๅ–ใ‚‹็ต„ใฟๅˆใ‚ใ›ใ‚’่€ƒใˆใ‚‹ใ€‚
ใพใšใ€1ใ‚’ๅ–ใ‚‹ใ€‚ๆฎ‹ใฃใŸ[2,3,4]ใ‹ใ‚‰2ใคใ‚’ๅ–ใฃใฆใใฆ1ใซๅŠ ใˆใ‚‹ใ€‚
[1,2,3][1,2,4][1,3,4]ใŒใงใใ‚‹ใ€‚
ๆฌกใซใ€2ใ‚’ๅ–ใ‚‹ใ€ๆฎ‹ใฃใŸ[3,4]ใ‹ใ‚‰2ใคๅ–ใฃใฆใใฆ2ใซๅŠ ใˆใ‚‹ใ€‚
[2,3,4]ใŒใงใใ‚‹ใ€‚
ๅˆใ‚ใ›ใฆใ€๏ผ”ใคใฎ็ต„ใฟๅˆใ‚ใ›ใŒใงใใ‚‹ใ€‚
ใ“ใ‚Œใฏๅ†ๅธฐ็š„ใซๅฎŸ่ฃ…ใ‚’ใ™ใ‚‹ใ“ใจใŒใงใใ‚‹ใ€‚

ๅ†ๅธฐ็ต‚ไบ†ๆกไปถ
[1,2,3]ใ‹ใ‚‰3ใคๅ–ใฃใฆใใ‚‹ใ‚ˆใ†ใชใจใใฏ[[1,2,3]]ใ‚’่ฟ”ใ™ใ€‚
[1,2,3]ใ‹ใ‚‰1ใคๅ–ใฃใฆใใ‚‹ใ‚ˆใ†ใชใจใใฏ[[1],[2],[3]]ใ‚’่ฟ”ใ™ใ€‚

Rubyใงใฏใ€ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใƒกใ‚ฝใƒƒใƒ‰ไธญใงใƒฌใ‚ทใƒผใƒใ‚’็œ็•ฅใ™ใ‚‹ใจselfใŒๆš—้ป™็š„ใซใƒฌใ‚ทใƒผใƒใซใชใ‚‹ใ€‚

ใ€€ใ‚ณใƒผใƒ‰

class Array
  # EXAMPLE: [1.2.3.4].combination_array(3) => [[1,2,3], [1,2,4], [1,3,4], [2,3,4]]
  def combination_array(comb_size)
    return [] if comb_size > size # 4C5ใฎใ‚ˆใ†ใชใ‚‚ใฎใฏ็ฉบใ‚’่ฟ”ใ™
    return [] if comb_size == 0 # 4C0ใฎใ‚ˆใ†ใชใ‚‚ใฎใฏ็ฉบใ‚’่ฟ”ใ™
    return [self] if comb_size == size
    return map { |e| [e] } if comb_size == 1

    ret = []
    (0..size - comb_size).each do |i|
      picked = self[i]
      rest = self[i + 1..-1]
      rest.combination_array(comb_size - 1).each do |c|
        ret << [picked] + c
      end
    end
    ret
  end
end

if $PROGRAM_NAME == __FILE__
  p %i[asahi kirin sapporo suntory].combination_array(2)

  # ้‡่ค‡ใŒใชใ„ๆ™‚(ๆ—ขๅญ˜ใƒกใ‚ฝใƒƒใƒ‰ใจใฎๆฏ”่ผƒ)
  p [1, 2, 3, 4, 5, 6, 7, 8, 9].combination_array(3) == [1, 2, 3, 4, 5, 6, 7, 8, 9].combination(3).to_a

  # ้‡่ค‡ใŒใ‚ใ‚‹ๆ™‚(ๆ—ขๅญ˜ใƒกใ‚ฝใƒƒใƒ‰ใจใฎๆฏ”่ผƒ)
  p [1, 2, 3, 4, 5, 6, 1, 8, 3].combination_array(4) == [1, 2, 3, 4, 5, 6, 1, 8, 3].combination(4).to_a
end

## OUTPUT
[[:asahi, :kirin], [:asahi, :sapporo], [:asahi, :suntory], [:kirin, :sapporo], [:kirin, :suntory], [:sapporo, :suntory]]
true
true

ๅ‚่€ƒ

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?