[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

The next number that is not in an array

Tim Conner

7/16/2008 9:02:00 PM

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?
Thanks in advance
--
Posted via http://www.ruby-....

27 Answers

Joel VanderWerf

7/16/2008 9:14:00 PM

0

Tim Conner wrote:
> 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?

By writing a Ruby Quiz? :)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Eric I.

7/16/2008 9:40:00 PM

0

On Wed, Jul 16, 2008 at 5:01 PM, Tim Conner <crofty_james@hotmail.com> wrote:
> 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?

How about:

begin
value += 1
end while restricted_numbers.member?(value)

Depending on the size of your list of restricted values, you may want
to use a Set rather than an Array, as it can test membership much more
efficiently.

Eric

====

LearnRuby.com offers Rails & Ruby HANDS-ON public & ON-SITE workshops.
Please visit http://Lea... for all the details.

Jeff

7/16/2008 9:41:00 PM

0


> Tim Conner wrote:
> > 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?

How about this?

def find_next_slot(n)
n += 1 # start one higher
n += 1 until !restricted_numbers.include(n)
n # return the next empty slot
end

Jeff
softiesonrails.com
purpleworkshops.com

Siep Korteling

7/16/2008 10:01:00 PM

0

Jeff Cohen wrote:
>>
>> > How is this done in ruby?
>
> How about this?
>
> def find_next_slot(n)
> n += 1 # start one higher
> n += 1 until !restricted_numbers.include(n)
> n # return the next empty slot
> end
>
> Jeff
> softiesonrails.com
> purpleworkshops.com

Heh, I had

def permitted_next(n)
n += 1
n += 1 while @restricted_numbers.include?(n)
return n
end

find_next_slot is a way better method name.

Regards,

Siep
--
Posted via http://www.ruby-....

David A. Black

7/16/2008 11:51:00 PM

0

Hi --

On Thu, 17 Jul 2008, Tim Conner wrote:

> 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)

This (and I think the other versions) keeps incrementing off the end
of the array, so you'd have to add something to make that not happen.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

Harry Kakueki

7/17/2008 1:38:00 AM

0

On Thu, Jul 17, 2008 at 6:01 AM, Tim Conner <crofty_james@hotmail.com> wrote:
> 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?
> Thanks in advance
> --
> Posted via http://www.ruby-....
>
>

# If your range is not too big, you could try this.
# It is not so efficient, but....

ngnums = [2,3,4,5,8,10,15,29]
mynum = 1

p (0..100_000).select {|x| x > mynum and ngnums.include?(x) == false}[0]

Harry

--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Rob Biedenharn

7/17/2008 1:57:00 AM

0

On Jul 16, 2008, at 9:38 PM, Harry Kakueki wrote:
> On Thu, Jul 17, 2008 at 6:01 AM, Tim Conner
> <crofty_james@hotmail.com> wrote:
>> 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?
>> Thanks in advance
>>
> # If your range is not too big, you could try this.
> # It is not so efficient, but....
>
> ngnums = [2,3,4,5,8,10,15,29]
> mynum = 1
>
> p (0..100_000).select {|x| x > mynum and ngnums.include?(x) == false}
> [0]
>
> Harry
>
> --
> A Look into Japanese Ruby List in English
> http://www.kakueki.com/ruby...

Change that to:
p (0..100_000).detect {|x| x > mynum and ngnums.include?(x) == false}

and it doesn't have to go all the way to the end of the range. You
can use the alias find if that makes more sense than detect.

p (0..100_000).find {|x| x > mynum and ngnums.include?(x) == false}

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com


Robert Dober

7/17/2008 1:44:00 PM

0

> true while restricted_numbers.include?(n+=1)
I think there is a typo in here ;)

Did you mean

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

of course you did.

Robert Dober

7/17/2008 2:07:00 PM

0

On Thu, Jul 17, 2008 at 3:44 PM, Robert Dober <robert.dober@gmail.com> wrote:
>> true while restricted_numbers.include?(n+=1)
> I think there is a typo in here ;)
>
> Did you mean
>
> 42 while restricted_numbers.include?( n+= 1 )
>
> of course you did.
>
>
Now to add injustice to injury ;)

What about

n = ([*n.succ..rn.max.succ]-rn).min || n.succ

This will be about 10 times slower than David's beautiful code.
However in some extreme cases, e.g. very densely populated rn arrays
this functional approach becomes more interesting.

If rn is [*1..1_000] the functional code runs 50 times faster, and if
you have to jump over an restricted
numbers array of [*1..10_000] the functional code runs 500 times faster.

So maybe just in case you can afford the extra runtime for the
"normal" case you can assure nice performance in edge cases by the
functional approach.

Cheers
Robert




--
http://ruby-smalltalk.blo...

---
AALST (n.) One who changes his name to be further to the front
D.Adams; The Meaning of LIFF

Pit Capitain

7/17/2008 2:51:00 PM

0

2008/7/16 Tim Conner <crofty_james@hotmail.com>:
> How is this done in ruby?

2008/7/17 Robert Dober <robert.dober@gmail.com>:
> So maybe just in case you can afford the extra runtime for the
> "normal" case you can assure nice performance in edge cases by the
> functional approach.

Tim, I think Robert is right. Depending on the use case there might be
better ways to store the restricted numbers and to search for the next
unused one. For example it depends on how many restricted numbers
there are, how often and how you have to change the restricted
numbers, how often you need to fetch the next unused number and so on.
It's foremost a question of the appropriate algorithm and data
structure and only then how to do it in Ruby.

Regards,
Pit