File: array_adds.rb

package info (click to toggle)
imagetooth 2.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 252 kB
  • sloc: ruby: 275; makefile: 2
file content (15 lines) | stat: -rw-r--r-- 254 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Array
  def sequence(i = 0, *a)
    return [a] if i == size
    self[i].map {|x|
      sequence(i+1, *(a + [x]))
    }.inject([]) {|m, x| m + x}
  end

  def permut(n)
    arr = Array.new
    1.upto(n){ arr.push(self) }
    arr.sequence
  end

end