2 lines in one...

J

Josselin

Could I write it in just 1 line ?

find the first value greater than d, in a list (special one : each
value is the double of the previous...)
then give me the index of this value (with special case ...)

i =
([0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].select
{|v| v if v >= d }).first

zl = d <0.5 ? 0 :
[0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].index(i)

joss
 
R

rretzbach

Hi Joss,

I think you want an alternative to select + index, right?
I don't have one so far, but:

arr = [0.5,1.0,2.0,4.0,8.0,16.0,33.0]
d = 3
p arr.index(arr.find{|v| v >= d }) #=> 3

doesn't look so bad imo.

You can use Array#each_with_index and use an own counter though.

Good luck!
 
S

Stefano Crocco

Alle venerd=EC 9 marzo 2007, Josselin ha scritto:
Could I write it in just 1 line ?

find the first value greater than d, in a list (special one : each
value is the double of the previous...)
then give me the index of this value (with special case ...)

i =3D
([0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].sel= ec
t {|v| v if v >=3D d }).first

zl =3D d <0.5 ? 0 :
[0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].inde= x(
i)

joss

What about this?

a.each_index{|i| break i if a > d}

The only problem is that it returns a if no element greater than d is found=
=2E=20
You could turn it into a method, though:

def find_greater array, value
array.each_index{|i| return i if array > value}
nil
end
 
B

benjohn

Could I write it in just 1 line ?
find the first value greater than d, in a list (special one : each
value is the double of the previous...)
then give me the index of this value (with special case ...)

i =
([0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].select
{|v| v if v >= d }).first

zl = d <0.5 ? 0 :
[0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].index(i)

Hi Joss,

In this specific case, you could simply do:

(Math.log(d)/Math.log(2) + 1).ceil

Which is a different kind of solution, and doesn't need the array.

Cheers,
Benj
 
B

Brian Candler

Could I write it in just 1 line ?

find the first value greater than d, in a list (special one : each
value is the double of the previous...)
then give me the index of this value (with special case ...)

i =
([0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].select
{|v| v if v >= d }).first

zl = d <0.5 ? 0 :
[0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].index(i)

a = [0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0]
d = 7.0

# version 1: matches your verbal specification
zl = a.each_with_index { |v,i| break i if v > d }

# version 2: matches your sample code's behaviour
zl = a.each_with_index { |v,i| break i if v >= d }

However this gives different behaviour to your code in the event that d is
greater than the last element in the array (yours returns nil, mine returns
the whole array). Maybe this is OK; you can always stick a 1.0/0.0
(infinity) at the end of the array. Or:

zl = nil; a.each_with_index { |v,i| break zl = i if v >= d }

But in this particular case you also could use your power-of-two property
and not construct or search an array at all:

zl = d < 0.5 ? 0 : (d*2).to_i.to_s(2).length

B.
 
R

Robert Klemme

2007/3/9 said:
Could I write it in just 1 line ?

find the first value greater than d, in a list (special one : each
value is the double of the previous...)
then give me the index of this value (with special case ...)

i =
([0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].select
{|v| v if v >= d }).first

zl = d <0.5 ? 0 :
[0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].index(i)

irb(main):015:0>
arr=[0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0]
=> [0.5, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0,
1024.0, 2048.0]
irb(main):016:0> idx = arr.to_enum:)each_with_index).inject(nil)
{|x,(e,i)| break i if e > 100}
=> 8
irb(main):017:0> idx = arr.to_enum:)each_with_index).inject(nil)
{|x,(e,i)| break i if e > 1000000}
=> nil

Kind regards

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top