How should I print only the last combination when using Array#combination(n)?

a = [1,2,3]
=> [1, 2, 3]

a.combination(2)
=> #<Enumerator: [1, 2, 3]:combination(2)>

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

a.combination(2){|arr| print arr.length;print ">";print arr;print"=>";p arr}
2
>[1, 2]=>[1, 2]
2~~>[1, 3]=>[1, 3]
2~~>[2, 3]=>[2, 3]
=> [1, 2, 3]

Any chance to get only the last combination,i.e [[2,3]] or 2~~>[2,
3]=>[2, 3] ?

On Sun, Feb 24, 2013 at 8:55 AM, Love U Ruby [email protected]
wrote:

2~~>[1, 2]=>[1, 2]
2~~>[1, 3]=>[1, 3]
2~~>[2, 3]=>[2, 3]
=> [1, 2, 3]

Any chance to get only the last combination,i.e [[2,3]] or 2~~>[2,
3]=>[2, 3] ?


Posted via http://www.ruby-forum.com/.

1.9.3-head :001 > [1,2,3].combination(2).to_a.last
=> [2, 3]

tamouse mailing lists wrote in post #1098785:

On Sun, Feb 24, 2013 at 8:55 AM, Love U Ruby [email protected]
wrote:

2~~>[1, 2]=>[1, 2]
2~~>[1, 3]=>[1, 3]
2~~>[2, 3]=>[2, 3]
=> [1, 2, 3]

Any chance to get only the last combination,i.e [[2,3]] or 2~~>[2,
3]=>[2, 3] ?


Posted via http://www.ruby-forum.com/.

1.9.3-head :001 > [1,2,3].combination(2).to_a.last
=> [2, 3]

Yes! but if I use block then any chance to control the same. as an
example I said here last. It can be any specific number. But yes
obviously when using with block.

On Sun, Feb 24, 2013 at 9:15 AM, Love U Ruby [email protected]
wrote:


Posted via http://www.ruby-forum.com/.

I have no idea what you’re saying there.

Other than get the last combination, what is it you actually want to
do? What is the application?

a.combination(2).reverse_each.first #=> [2, 3]
a[-2…-1] #=> [2, 3]

with block:
a.combination(2).find {|x,y| x == 2 } #=> [2, 3]

with block and index:
a.combination(2).find.with_index {|(x,y),i| i == 2 } #=> [2, 3]

tamouse mailing lists wrote in post #1098790:

On Sun, Feb 24, 2013 at 9:15 AM, Love U Ruby [email protected]
wrote:


Posted via http://www.ruby-forum.com/.

I have no idea what you’re saying there.

Other than get the last combination, what is it you actually want to
do? What is the application?

Okay! I want to write a one liner which can give me the below output:

which is currently I am not able by the below block code with
combination,as in the below block I couldn’t track the,say,when second
‘arr’ is coming just print it,but not the others.:

a.combination(2){|arr| print arr.length;print ">";print arr ; print “=>” ; p
arr}
2
>[1, 2]=>[1, 2]
2~~>[1, 3]=>[1, 3]
2~~>[2, 3]=>[2, 3]
=> [1, 2, 3]

Please let me know if still my need confuses you.

Hans M. wrote in post #1098791:

The below tips I am looking for. Thanks @Hans :slight_smile:

with block:
a.combination(2).find {|x,y| x == 2 } #=> [2, 3]

with block and index:
a.combination(2).find.with_index {|(x,y),i| i == 2 } #=> [2, 3]

Am 24.02.2013 16:42, schrieb Love U Ruby:

Hans M. wrote in post #1098791:

The below tips I am looking for. Thanks @Hans :slight_smile:

with block:
a.combination(2).find {|x,y| x == 2 } #=> [2, 3]

with block and index:
a.combination(2).find.with_index {|(x,y),i| i == 2 } #=> [2, 3]

a.combination(2).to_a[2]  # => [2, 3]

Why do you insist on using the block version when you already
know the index?

On Sun, Feb 24, 2013 at 9:40 AM, Love U Ruby [email protected]
wrote:

Other than get the last combination, what is it you actually want to
do? What is the application?

Okay! I want to write a one liner which can give me the below output:

I don’t see any example output here…

Please let me know if still my need confuses you.

Still don’t know what your need is…

If I guessed, is it this?

1.9.3-head :007 > [1,2,3].combination(2).to_a.last.tap{|t| puts
"#{t.length}>#{t}=>#{t}" }
2
>[2, 3]=>[2, 3]

?

Can you describe what you’re trying to do from the beginning before
you decided to go down the combination route?

see the below code :

a = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]

a.combination(3){|arr| p arr}
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 3, 4]
[1, 3, 5]
[1, 4, 5]
[2, 3, 4]
[2, 3, 5]
[2, 4, 5]
[3, 4, 5]
=> [1, 2, 3, 4, 5]

Here,say I want to see the output only(without any fancy I write it
here,just to get to see the trick):

[1, 3, 5] #~~ means 5th combination
[2, 3, 5] #~~ means 8th combination

But yes only with block. Hope it is now clear to you @tamouse. My last
reply went to you by mistake without the output being written by me. I
tried to edit,but it passed then 15 mins,thus couldn’t.

Thanks

On Feb 24, 2013 10:21 AM, “Love U Ruby” [email protected] wrote:

[1, 3, 5]
[1, 3, 5] #~~ means 5th combination

Is thissupposed to be a general algorithm, say out of any combination,
you
will want the n-th combination? Or is there some scheme you have in mind
to
choose which combinations you are seeking?

Why does it have to be passed to a block from combination?

Generally, what is the problem you are trying to solve with this?

the combination array can be enormous …
thats why find.with_index can be better than to_a

Am 24.02.2013 16:53, schrieb Hans M.:

the combination array can be enormous …
thats why find.with_index can be better than to_a

My question was intended for “Love U Ruby”.

What the better solution is depends solely on the actual use
case, which the OP did not provide, even after the an explicit
inquiry by Tamouse. I do not want to speculate about this.

I strongly assume he himself does not know what kind of solution he
really needs.

Am 24.02.2013 17:11, schrieb Love U Ruby:

[1, 4, 5]
[2, 3, 5] #~~ means 8th combination

But yes only with block.

Why???
What are you trying to do?
Why do you insist on using a block?
What is your use case?

We can give better advice when you tell us what you are trying
to accomplish. Using a block might not be the most effective solution.

unknown wrote in post #1098826:

Am 24.02.2013 17:11, schrieb Love U Ruby:

[1, 4, 5]
[2, 3, 5] #~~ means 8th combination

But yes only with block.

Why???
What are you trying to do?
Why do you insist on using a block?
What is your use case?

We can give better advice when you tell us what you are trying
to accomplish. Using a block might not be the most effective solution.

what I was looking for is already given by - “Hans M.”.

So thanks for your curiosity. Please don’t curse always,no need to stick
on the topic that “I am trolling”. If you really like to help,you can,
But honest request don’t be off-topic.