Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/array_combs_perms.rb
Overview
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Instance Method Summary collapse
- #comb_helper(head, tail, &block) ⇒ Object
- #combinations(&block) ⇒ Object
- #perm_helper(head, tail, &block) ⇒ Object
- #permutations(&block) ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#comb_helper(head, tail, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/array_combs_perms.rb', line 31 def comb_helper head, tail, &block if tail.length == 0 block.call(head) else tail.each_index do |i| t = tail.clone t.delete_at(i) comb_helper head.clone << tail[i], t, &block end end end |
#combinations(&block) ⇒ Object
43 44 45 |
# File 'lib/array_combs_perms.rb', line 43 def combinations &block comb_helper [], self, &block end |
#perm_helper(head, tail, &block) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/array_combs_perms.rb', line 51 def perm_helper head, tail, &block block.call(head) t = tail.clone until t.empty? val = t.pop comb_helper head.clone << val, t, &block end end |
#permutations(&block) ⇒ Object
47 48 49 |
# File 'lib/array_combs_perms.rb', line 47 def permutations &block perm_helper [self[0]], self[1...self.length], &block end |
#to_s ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/array_combs_perms.rb', line 22 def to_s s = "[" each_index do |i| s << " " + self[i].to_s s << ", " unless (i == self.length-1) end s << "]" end |