Array index of first bigger number...

Discussion in 'Ruby' started by Josselin, Jun 2, 2007.

  1. Josselin

    Josselin Guest

    Is there a simple function (I solve it w a loop.. C-minded) to find the
    index of the first bigger element in an array

    limit mini is first_element , if item < last_element
    item = -2.23
    anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    0.0 index 0

    item = 2.85
    anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    5.0 index 2

    item = 12.55
    anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    15.0 index 4

    limit maxi is last_element , if item > last_element
    item = 36.59
    anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    25.0 index 5

    can it be writtent in just one line or a loop is unavoidable ?

    thanks

    joss
    Josselin, Jun 2, 2007
    #1
    1. Advertising

  2. Alle sabato 2 giugno 2007, Josselin ha scritto:
    > Is there a simple function (I solve it w a loop.. C-minded) to find the
    > index of the first bigger element in an array
    >
    > limit mini is first_element , if item < last_element
    > item = -2.23
    > anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    > 0.0 index 0
    >
    > item = 2.85
    > anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    > 5.0 index 2
    >
    > item = 12.55
    > anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    > 15.0 index 4
    >
    > limit maxi is last_element , if item > last_element
    > item = 36.59
    > anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    > 25.0 index 5
    >
    > can it be writtent in just one line or a loop is unavoidable ?
    >
    > thanks
    >
    > joss


    You can do:

    anArray.index(anArray.find{|i| i > item})

    anArray.find will return the first element of the array for which the block
    returns true, i.e the first element which is greater than item; then
    anArray.index will return the index of the item. (This returns nil if no
    element in the array is greater than item)

    I hope this helps

    Stefano
    Stefano Crocco, Jun 2, 2007
    #2
    1. Advertising

  3. On 02.06.2007 09:30, Josselin wrote:
    > Is there a simple function (I solve it w a loop.. C-minded) to find the
    > index of the first bigger element in an array
    >
    > limit mini is first_element , if item < last_element
    > item = -2.23
    > anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    > 0.0 index 0
    >
    > item = 2.85
    > anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    > 5.0 index 2
    >
    > item = 12.55
    > anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    > 15.0 index 4
    >
    > limit maxi is last_element , if item > last_element
    > item = 36.59
    > anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    > 25.0 index 5
    >
    > can it be writtent in just one line or a loop is unavoidable ?


    Yes, it can. Just use Enumerator.

    irb(main):001:0> anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0]
    => [0.0, 2.0, 5.0, 10.0, 15.0, 25.0]

    irb(main):007:0> anArray.to_enum:)each_with_index).find {|n,i| n > 30}
    => nil
    irb(main):008:0> anArray.to_enum:)each_with_index).find {|n,i| n > 10}
    => [15.0, 4]
    irb(main):009:0> anArray.to_enum:)each_with_index).find {|n,i| n > -2.23}
    => [0.0, 0]
    irb(main):010:0> val, idx = anArray.to_enum:)each_with_index).find
    {|n,i| n > -2.23}
    => [0.0, 0]
    irb(main):011:0> val
    => 0.0
    irb(main):012:0> idx
    => 0

    This also has the advantage of having to traverse the array just once
    vs. the solution Stefano presented.

    Kind regards

    robert
    Robert Klemme, Jun 2, 2007
    #3
  4. Josselin

    Josselin Guest

    On 2007-06-02 10:59:03 +0200, Robert Klemme <> said:

    > On 02.06.2007 09:30, Josselin wrote:
    >> Is there a simple function (I solve it w a loop.. C-minded) to find the
    >> index of the first bigger element in an array
    >>
    >> limit mini is first_element , if item < last_element
    >> item = -2.23
    >> anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    >> 0.0 index 0
    >>
    >> item = 2.85
    >> anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    >> 5.0 index 2
    >>
    >> item = 12.55
    >> anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    >> 15.0 index 4
    >>
    >> limit maxi is last_element , if item > last_element
    >> item = 36.59
    >> anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first_bigger_element =
    >> 25.0 index 5
    >>
    >> can it be writtent in just one line or a loop is unavoidable ?

    >
    > Yes, it can. Just use Enumerator.
    >
    > irb(main):001:0> anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0]
    > => [0.0, 2.0, 5.0, 10.0, 15.0, 25.0]
    >
    > irb(main):007:0> anArray.to_enum:)each_with_index).find {|n,i| n > 30}
    > => nil
    > irb(main):008:0> anArray.to_enum:)each_with_index).find {|n,i| n > 10}
    > => [15.0, 4]
    > irb(main):009:0> anArray.to_enum:)each_with_index).find {|n,i| n > -2.23}
    > => [0.0, 0]
    > irb(main):010:0> val, idx = anArray.to_enum:)each_with_index).find
    > {|n,i| n > -2.23}
    > => [0.0, 0]
    > irb(main):011:0> val
    > => 0.0
    > irb(main):012:0> idx
    > => 0
    >
    > This also has the advantage of having to traverse the array just once
    > vs. the solution Stefano presented.
    >
    > Kind regards
    >
    > robert


    thanks, Robert
    that's what I was trying to avoid... I was close to Stefano's solution
    Josselin, Jun 2, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TG
    Replies:
    3
    Views:
    453
    Robert Kern
    Jul 26, 2006
  2. Replies:
    2
    Views:
    765
    Filip Larsen
    Apr 10, 2007
  3. Marcus
    Replies:
    25
    Views:
    1,597
    Francesco
    Aug 19, 2009
  4. Shawn W_
    Replies:
    5
    Views:
    260
    Aldric Giacomoni
    Sep 16, 2009
  5. Yannick Turgeon

    App getting bigger and bigger

    Yannick Turgeon, Oct 13, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    129
    Yannick Turgeon
    Oct 14, 2003
Loading...

Share This Page