>> 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 2013-02-24 15:54
on 2013-02-24 16:09
On Sun, Feb 24, 2013 at 8:55 AM, Love U Ruby <lists@ruby-forum.com> 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]
on 2013-02-24 16:15
tamouse mailing lists wrote in post #1098785: > On Sun, Feb 24, 2013 at 8:55 AM, Love U Ruby <lists@ruby-forum.com> > 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 2013-02-24 16:31
On Sun, Feb 24, 2013 at 9:15 AM, Love U Ruby <lists@ruby-forum.com> 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?
on 2013-02-24 16:36
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]
on 2013-02-24 16:40
tamouse mailing lists wrote in post #1098790: > On Sun, Feb 24, 2013 at 9:15 AM, Love U Ruby <lists@ruby-forum.com> > 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.
on 2013-02-24 16:42
Hans Mackowiak wrote in post #1098791: The below tips I am looking for. Thanks @Hans :) > 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]
on 2013-02-24 16:49
Am 24.02.2013 16:42, schrieb Love U Ruby: > Hans Mackowiak wrote in post #1098791: > > > The below tips I am looking for. Thanks @Hans :) > >> 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 2013-02-24 16:52
the combination array can be enormous ... thats why find.with_index can be better than to_a
on 2013-02-24 17:01
On Sun, Feb 24, 2013 at 9:40 AM, Love U Ruby <lists@ruby-forum.com> 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?
on 2013-02-24 17:11
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 2013-02-24 17:49
On Feb 24, 2013 10:21 AM, "Love U Ruby" <lists@ruby-forum.com> 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?
on 2013-02-24 19:15
Am 24.02.2013 16:53, schrieb Hans Mackowiak: > 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.
on 2013-02-24 19:21
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.
on 2013-02-24 20:00
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 Mackowiak". 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.