Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ruby 1.9.1 Array#combination kinda lazy

59 views
Skip to first unread message

Phrogz

unread,
Jul 15, 2010, 2:29:11 PM7/15/10
to
C:\>type tmp.rb
count = 0
combo = nil
(1..166).to_a.combination(5).each do |c|
count+= 1; combo = c
end
p count, combo

C:\>ruby -v tmp.rb
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]
129462338
[5, 34, 54, 73, 81]

Yeah...no one could possibly need to run more than 129 million
combinations, and...I'm getting kinda tired, so...yeah, let's just
stop here.

WTF?

Ryan Davis

unread,
Jul 16, 2010, 4:20:16 AM7/16/10
to

wfm:

#!/usr/bin/ruby -w

class Integer
def f!
(1..self).inject { |prod, n| prod * n }
end
end

expected = ((166.f!)/(5.f! * 161.f!).to_f).to_i # => 988455798

count = 0
combo = nil

(1..166).to_a.combination(5).each do |c|
count += 1; combo = c
end

p count, combo
p count == expected

# outputs:
#
# ~/.multiruby/install/1.9.1-p129/bin/ruby wtf.rb
# 988455798
# [162, 163, 164, 165, 166]
# true


Phrogz

unread,
Jul 16, 2010, 12:40:58 PM7/16/10
to
On Jul 16, 2:20 am, Ryan Davis <ryand-r...@zenspider.com> wrote:
> On Jul 15, 2010, at 11:30 , Phrogz wrote:
> > C:\>ruby -v tmp.rb
> > ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]
> > 129462338
> > [5, 34, 54, 73, 81]

> wfm:


> # ~/.multiruby/install/1.9.1-p129/bin/ruby  wtf.rb
> # 988455798
> # [162, 163, 164, 165, 166]

Just reproduced using 1.9.1p378 on Ubuntu. Looks to me like it's not a
Windows bug, but something introduced sometime between p129 and p243.

Ryan Davis

unread,
Jul 16, 2010, 1:50:34 PM7/16/10
to
[Note: parts of this message were removed to make it a legal post.]

On Jul 16, 2010, at 9:45 AM, Phrogz <phr...@mac.com> wrote:

> Just reproduced using 1.9.1p378 on Ubuntu. Looks to me like it's not a
> Windows bug, but something introduced sometime between p129 and p243.

Report on ruby-core@ please.

Phrogz

unread,
Jul 16, 2010, 2:49:00 PM7/16/10
to

Interestingly, replacing 166 with 141 produces a RangeError:
in `combination': too big for combination

So this bug may be the same or related to http://redmine.ruby-lang.org/issues/show/3089,
which is marked as fixed in 1.9.2.

Will follow up with ruby-core.

Phrogz

unread,
Jul 16, 2010, 4:51:40 PM7/16/10
to
On Jul 15, 12:29 pm, Phrogz <phr...@mac.com> wrote:
> C:\>type tmp.rb
> count = 0
> combo = nil
> (1..166).to_a.combination(5).each do |c|
>   count+= 1; combo = c
> end
> p count, combo
>
> C:\>ruby -v tmp.rb
> ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]
> 129462338
> [5, 34, 54, 73, 81]

For posterity, this has now been logged as a bug here:
http://redmine.ruby-lang.org/issues/show/3581

0 new messages