The next number that is not in an array

Hi All,

David A. Black wrote:

currently storing 1 then I want to increment it to 6.

How is this done in ruby?

Here’s a pretty terse way – maybe too terse, and rather
side-effect-ish, but kind of interesting:

true while restricted_numbers.include?(n+=1)

I’ve been working in Rails for about 3 years now, but have only recently
begun to really dig in to Ruby. I say that in hopes you’ll answer a
couple
of questions to help me ‘dig’ in more productive locations :slight_smile:

My first take on this ‘quiz’ was to initialize a new array with members
between the current value and the largest restricted number (including
the
largest), subtract the restricted_number array from the new one, and
return
the first member of the result. None of the solutions proposed involved
anything like this, so I’m hoping one or more of you will say a little
bit
about why.

Second question has to do with comments Robert D. made like ‘10 times
slower’ and ‘50 times faster’. I’m assuming there’s some benchmarking
capability I need to learn more about. Could somebody point me? Also,
is
this capability available in IRB?

TIA,
Bill

On Sat, Jul 19, 2008 at 7:57 PM, Bill W. [email protected]
wrote:

Second question has to do with comments Robert D. made like ‘10 times
slower’ and ‘50 times faster’. I’m assuming there’s some benchmarking
capability I need to learn more about. Could somebody point me? Also, is
this capability available in IRB?
I would not use it with IRB because often you have to twist parameters.

Unfortunately I have not kept my benchmarks that gave the above
results, but I gladly post the benchmark code
that I used to benchmark my approach with Erik’s enhancements. This
with the rdoc documentation of benchmark should be enough to get you
going.

http://pastie.org/237335
http://www.ruby-doc.org/stdlib/libdoc/benchmark/rdoc/index.html

HTH
Robert

Hi –

On Sun, 20 Jul 2008, Bill W. wrote:

I’ve been working in Rails for about 3 years now, but have only recently
begun to really dig in to Ruby. I say that in hopes you’ll answer a couple
of questions to help me ‘dig’ in more productive locations :slight_smile:

My first take on this ‘quiz’ was to initialize a new array with members
between the current value and the largest restricted number (including the
largest), subtract the restricted_number array from the new one, and return
the first member of the result. None of the solutions proposed involved
anything like this, so I’m hoping one or more of you will say a little bit
about why.

So like this?

irb(main):020:0> a = [2,3,4,8,12,13,15]
=> [2, 3, 4, 8, 12, 13, 15]
irb(main):021:0> ([*a[0]…20] - a).shift
=> 5

It’s a cool idea, though I would find it somewhat hard to see what’s
going on at a glance than some of the others.

Second question has to do with comments Robert D. made like ‘10 times
slower’ and ‘50 times faster’. I’m assuming there’s some benchmarking
capability I need to learn more about. Could somebody point me? Also, is
this capability available in IRB?

Have you used the Benchmark module? It’s the usual tool for getting
comparisons of different snippets that do the same thing as each
other.

David

On Sun, Jul 20, 2008 at 5:48 PM, Robert D. [email protected]
wrote:

that I used to benchmark my approach with Erik’s enhancements. This
with the rdoc documentation of benchmark should be enough to get you
going.

Also, have a look at:

It helps you doing statistically correct benchmarking

Hi Robert,

Robert D. wrote:

Unfortunately I have not kept my benchmarks that gave the above
results, but I gladly post the benchmark code
that I used to benchmark my approach with Erik’s enhancements. This
with the rdoc documentation of benchmark should be enough to get you
going.

http://pastie.org/237335
http://www.ruby-doc.org/stdlib/libdoc/benchmark/rdoc/index.html

Thank you, Robert. I appreciate the help!

Best regards,
Bill

Hi Michael,

Michael F. wrote:

Also, have a look at:
GitHub - Pistos/better-benchmark: Statistically correct benchmarking for Ruby.
It helps you doing statistically correct benchmarking

Looks very interesting. Thanks!

Best regards,
Bill

Hi David,

David A. Black wrote:

So like this?

irb(main):020:0> a = [2,3,4,8,12,13,15]
=> [2, 3, 4, 8, 12, 13, 15]
irb(main):021:0> ([*a[0]…20] - a).shift
=> 5

It’s a cool idea,

Thanks.

though I would find it somewhat hard to see what’s
going on at a glance than some of the others.

Yeah. I wasn’t sure of the overall context. If the reserved numbers
list
was static and there was a need to call the get_next method more than
once,
I thought getting and saving the array difference (as a separate step
before
the shift) could pay dividends later that might make it a reasonable
choice.

Second question has to do with comments Robert D. made like ‘10 times
slower’ and ‘50 times faster’. I’m assuming there’s some benchmarking
capability I need to learn more about. Could somebody point me? Also,
is

this capability available in IRB?

Have you used the Benchmark module? It’s the usual tool for getting
comparisons of different snippets that do the same thing as each
other.

I haven’t used it, but will definitely check it out. Thanks for the
pointer.

Best regards.
Bill

On Sat, Jul 19, 2008 at 8:36 PM, David A. Black [email protected]
wrote:

irb(main):020:0> a = [2,3,4,8,12,13,15]
=> [2, 3, 4, 8, 12, 13, 15]
irb(main):021:0> ([*a[0]…20] - a).shift
=> 5

It’s a cool idea, though I would find it somewhat hard to see what’s
going on at a glance than some of the others.
I am somehow surprised, maybe you should look at Erik’s enhancement of
my first functional proposed solution
to see where the problems with this solution are.
HTH
Robert