# Array Combinations

**URL:** <https://www.ruby-forum.com/t/array-combinations/142349>\
**Category:** Ruby\
**Created:** [July 7, 2008, 6:17am UTC](https://www.ruby-forum.com/t/array-combinations/142349 "2008-07-07T06:17:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [July 7, 2008, 6:17am UTC](https://www.ruby-forum.com/t/array-combinations/142349/1 "2008-07-07T06:17:05Z")

</div>

Hi,  
I want to find array combinations is there any way find combinations in  
array

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [July 7, 2008, 6:36am UTC](https://www.ruby-forum.com/t/array-combinations/142349/2 "2008-07-07T06:36:33Z")

</div>

On Mon, Jul 7, 2008 at 2:13 PM, Pragash Mr.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to find array combinations is there any way find combinations in  
> array

Yes.

If you want a more detailed answer, it might be a good idea to give an  
appropriately detailed question… 🙂

Best Regards

Mikel

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [July 7, 2008, 6:38am UTC](https://www.ruby-forum.com/t/array-combinations/142349/3 "2008-07-07T06:38:37Z")

</div>

Mikel L. wrote:

> On Mon, Jul 7, 2008 at 2:13 PM, Pragash Mr.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I want to find array combinations is there any way find combinations in  
> > array
> 
> Yes.
> 
> If you want a more detailed answer, it might be a good idea to give an  
> appropriately detailed question… 🙂
> 
> Best Regards
> 
> Mikel

Hi,  
i am having an array for ex:[1,2,3,4]

from this array i want to find the possible combinations like  
[1,3,4,2]  
[1,4,2,3]  
etc…

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [July 7, 2008, 6:44am UTC](https://www.ruby-forum.com/t/array-combinations/142349/4 "2008-07-07T06:44:09Z")

</div>

On Sun, Jul 6, 2008 at 9:35 PM, Pragash Mr.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i am having an array for ex:[1,2,3,4]
> 
> from this array i want to find the possible combinations like  
> [1,3,4,2]  
> [1,4,2,3]  
> etc…

[http://permutation.rubyforge.org/](http://permutation.rubyforge.org/)

martin

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [July 7, 2008, 7:19am UTC](https://www.ruby-forum.com/t/array-combinations/142349/5 "2008-07-07T07:19:16Z")

</div>

From: Pragash Mr. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# i am having an array for ex:[1,2,3,4]

# from this array i want to find the possible combinations like

# [1,3,4,2]

# [1,4,2,3]

# etc…

if you have ruby version 1.8.7 or 1.9, just try it

eg,

> RUBY\_VERSION  
> =\> “1.8.7”

> [1,2,3].permutation(3){|x| p x}  
> [1, 2, 3]  
> [1, 3, 2]  
> [2, 1, 3]  
> [2, 3, 1]  
> [3, 1, 2]  
> [3, 2, 1]  
> =\> [1, 2, 3]

> [1,2,3].permutation(2){|x| p x}  
> [1, 2]  
> [1, 3]  
> [2, 1]  
> [2, 3]  
> [3, 1]  
> [3, 2]  
> =\> [1, 2, 3]

> [1,2,3].combination(3){|x| p x}  
> [1, 2, 3]  
> =\> [1, 2, 3]

> [1,2,3].combination(2){|x| p x}  
> [1, 2]  
> [1, 3]  
> [2, 3]  
> =\> [1, 2, 3]

kind regards -botp
