Extracting the shortest string from an array

Discussion in 'Ruby' started by Iñaki Baz Castillo, Mar 3, 2011.

  1. Hi, given the following array:

    array =3D ["qwe", "qwerty"]

    How to get the shortest element ("qwe") from the array?
    I whink I must inspect all the elements within the array and manually
    select the shortest string, do I miss some cool feature of Array class
    or Enumerable module?

    Thanks a lot.

    --=20
    I=C3=B1aki Baz Castillo
    <>
     
    Iñaki Baz Castillo, Mar 3, 2011
    #1
    1. Advertisements

  2. Iñaki Baz Castillo

    Adam Hegge Guest

    array.min{|a,b| a.size <=> b.size }
     
    Adam Hegge, Mar 3, 2011
    #2
    1. Advertisements

  3. Hi,

    in Ruby 1.9 this should work:

    arr.sort_by(&:length)[0]

    or more portable/readable (1.8 compatible):

    arr.sort_by{|s| s.length }[0]


    Am 03.03.2011 15:47, schrieb Iñaki Baz Castillo:
     
    Thorsten Hater, Mar 3, 2011
    #3
  4. Hi,

    You could use

    array.min {|x,y| x.size <=3D> y.size}

    to force a min to use sizes rather then their natural order

    Mac
     
    Paul McKibbin, Mar 3, 2011
    #4

  5. Thanks to all :)

    --=20
    I=C3=B1aki Baz Castillo
    <>
     
    Iñaki Baz Castillo, Mar 3, 2011
    #5
  6. Iñaki Baz Castillo

    Jeremy Bopp Guest

    Jeremy Bopp, Mar 3, 2011
    #6
  7. Inefficient as it creates a new array. Better do

    irb(main):001:0> ["qwe", "qwerty"].min_by(&:length)
    => "qwe"
    Inefficient as well (see above).

    Cheers

    robert
     
    Robert Klemme, Mar 3, 2011
    #7
  8. [Note: parts of this message were removed to make it a legal post.]

    Very true

    array.min_by{|a| a.size} would also do the trick.

    Paul
     
    Paul McKibbin, Mar 3, 2011
    #8
  9. Iñaki Baz Castillo

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    They're also O( n lg n ) where the correct solution, using min, is O(n)
     
    Josh Cheek, Mar 3, 2011
    #9
  10. How do you know that it's O(n * log n)? A reasonable implementation
    of min_by would look like this:

    def min_by(enum, &c)
    min =3D nil
    val =3D nil

    enum.each do |e|
    e_val =3D c[e]

    if val.nil? || e_val < val
    min =3D e
    val =3D e_val
    end
    end

    min
    end

    As far as I can see this is O(n).

    Also, big O isn't everything. Usually object allocation is very
    expensive (compared to other operations) because of the GC
    housekeeping overhead.

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Mar 4, 2011
    #10
  11. Iñaki Baz Castillo

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    Right. This was my point: using min is O(n), using sort is O(n lg n)


    I don't contest this. I was pointing out that in addition to creating more
    objects, as you previously mentioned, using sort also has a worse time
    complexity.
     
    Josh Cheek, Mar 4, 2011
    #11
  12. OK, now I get what you mean. You were referring to the sorts. That
    wasn't clear to me.
    Yep, sorry for the noise.

    Cheers

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Mar 4, 2011
    #12
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.