The next number that is not in an array

B

Bill Walton

Hi All,
I want to increment the current value of a variable to
the next number that does not appear in an array of
restricted numbers.
e.g.
restricted_numbers = [2,3,4,5,8,10,15,29]

if the variable is currently storing 11 then I want
to increment it to 12, however if the variable is
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 :)

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 Dober 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
 
D

David A. Black

Hi --

Hi All,
I want to increment the current value of a variable to
the next number that does not appear in an array of
restricted numbers.
e.g.
restricted_numbers = [2,3,4,5,8,10,15,29]

if the variable is currently storing 11 then I want
to increment it to 12, however if the variable is
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 :)

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 Dober 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
 
R

Robert Dober

Second question has to do with comments Robert Dober 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
 
M

Michael Fellinger

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.

Also, have a look at:
http://github.com/Pistos/better-benchmark
It helps you doing statistically correct benchmarking
 
B

Bill Walton

Hi David,
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.
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
 
R

Robert Dober

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top