[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Can anyone try to solve this problems?

David Madden

1/15/2007 7:23:00 PM


> 2. Write a function to find the longest string in an array of
> strings.
>
Is this from the Brian Schroder Ruby Course PDF?

Here is my function (no doubt a better on exists):

def longest(a)
word = ''
a.each do |i|
if word.length < i.length
word = i
end
end
word
end


a = ['a', 'fish', 'is', 'messy', 'dog']

puts longest(a)




12 Answers

Daniel Finnie

1/16/2007 12:10:00 AM

0

That is the classic searching algorithm (almost). However there is
something more idiomatic.

Here's an example to find the biggest number:
[1, 2, 3].max {|a, b| a <=> b }

Try to adapt the array contents in that and the block to find the
longest string.

Dan

David Madden wrote:
>
>> 2. Write a function to find the longest string in an array of strings.
>>
> Is this from the Brian Schroder Ruby Course PDF?
>
> Here is my function (no doubt a better on exists):
>
> def longest(a)
> word = ''
> a.each do |i|
> if word.length < i.length
> word = i
> end
> end
> word
> end
>
>
> a = ['a', 'fish', 'is', 'messy', 'dog']
>
> puts longest(a)
>
>
>
>
>

Gregory Brown

1/16/2007 1:51:00 AM

0

On 1/15/07, David Madden <moose56@gmail.com> wrote:
>
> > 2. Write a function to find the longest string in an array of
> > strings.
> >
> Is this from the Brian Schroder Ruby Course PDF?
>
> Here is my function (no doubt a better on exists):
>
> def longest(a)
> word = ''
> a.each do |i|
> if word.length < i.length
> word = i
> end
> end
> word
> end

['fodfsafdfsdfso','bar','bazzzzzz'].sort_by { |s| s.length }.pop

Nobuyoshi Nakada

1/16/2007 3:59:00 AM

0

Hi,

Date: Tue, 16 Jan 2007 10:51:18 +0900
Gregory Brown wrote in [ruby-talk:234222]:
> ['fodfsafdfsdfso','bar','bazzzzzz'].sort_by { |s| s.length }.pop

max_by(&:length)

--
Nobu Nakada

Gregory Brown

1/16/2007 4:53:00 AM

0

On 1/15/07, Nobuyoshi Nakada <nobu@ruby-lang.org> wrote:
> Hi,
>
> Date: Tue, 16 Jan 2007 10:51:18 +0900
> Gregory Brown wrote in [ruby-talk:234222]:
> > ['fodfsafdfsdfso','bar','bazzzzzz'].sort_by { |s| s.length }.pop
>
> max_by(&:length)

Looking forward to 1.9 Nobu! :)

David Madden

1/16/2007 5:00:00 AM

0

>>
>> def longest(a)
>> word = ''
>> a.each do |i|
>> if word.length < i.length
>> word = i
>> end
>> end
>> word
>> end
>
> ['fodfsafdfsdfso','bar','bazzzzzz'].sort_by { |s| s.length }.pop
>

The beauty of a one line solution :-)

also...what is this about:

max_by(&:length)

Dave.

Gregory Brown

1/16/2007 5:39:00 AM

0

On 1/15/07, David Madden <moose56@gmail.com> wrote:
> >>
> >> def longest(a)
> >> word = ''
> >> a.each do |i|
> >> if word.length < i.length
> >> word = i
> >> end
> >> end
> >> word
> >> end
> >
> > ['fodfsafdfsdfso','bar','bazzzzzz'].sort_by { |s| s.length }.pop
> >
>
> The beauty of a one line solution :-)
>
> also...what is this about:
>
> max_by(&:length)

This is an Enumerable method from 1.9
http://ruby-doc.org/core-1.9/classes/Enume...

It is a much better solution than what I offered, since it doesn't
sort the list first.
The actual method is implemented in C, but here is one way of doing it
in Ruby, to help you understand what's going on (maybe :) )

>> seltzer:~ sandal$ irb
>> module Enumerable
>> def max_by
>> m={}
>> each { |e|
?> v = yield(e)
>> m[:value] ||= v
>> if v > m[:value]
>> m[:obj] = e; m[:value] = v;
?> end
>> }
>> m[:obj]
>> end
>> end
=> nil
>> ['foo','bartdfaggdgs','afdfd'].max_by { |e| e.length }
=> "bartdfaggdgs"
>> class Symbol
>> def to_proc
>> lambda { |x| x.send(self) }
>> end
>> end
=> nil
>> ['foo','bartdfaggdgs','afdfd'].max_by &:length
=> "bartdfaggdgs"

Daniel Martin

1/16/2007 7:45:00 PM

0

David Madden <moose56@gmail.com> writes:

>> 2. Write a function to find the longest string in an array of
>> strings.
>>
> Is this from the Brian Schroder Ruby Course PDF?
>
> Here is my function (no doubt a better on exists):

Well, okay, we've heard from folks with their fancy 1.9 ruby with
max_by and that &:sym notation. However, I'm a little bit surprised
that this one-liner with inject didn't pop up:

def longest(a)
a.inject { |x,y| x.length < y.length ? y : x }
end

ObQuickIRBSanityCheck:

irb(main):001:0> def longest(a)
irb(main):002:1> a.inject { |x,y| x.length < y.length ? y : x }
irb(main):003:1> end
=> nil
irb(main):004:0> longest(%w[a bc de fgh ijkl mnopq rstu vwxyz])
=> "mnopq"

foldr is your friend.

--
s=%q( Daniel Martin -- martin@snowplow.org
puts "s=%q(#{s})",s.map{|i|i}[1] )
puts "s=%q(#{s})",s.map{|i|i}[1]

Gregory Brown

1/16/2007 8:11:00 PM

0

On 1/16/07, Daniel Martin <martin@snowplow.org> wrote:
> David Madden <moose56@gmail.com> writes:
>
> >> 2. Write a function to find the longest string in an array of
> >> strings.
> >>
> > Is this from the Brian Schroder Ruby Course PDF?
> >
> > Here is my function (no doubt a better on exists):
>
> Well, okay, we've heard from folks with their fancy 1.9 ruby with
> max_by and that &:sym notation. However, I'm a little bit surprised
> that this one-liner with inject didn't pop up:
>
> def longest(a)
> a.inject { |x,y| x.length < y.length ? y : x }
> end

Daniel, this is a much better solution than my sort_by one. So good work! :)

Sam Smoot

1/16/2007 8:16:00 PM

0

Here's my try at a Ruby 1.8 implementation of the Enumerable#max_by:

> a = %w{ one two three four }

> def a.max_by
> pairs = inject({}) do |hash, obj|
> hash[yield obj] = obj; hash
> end
> pairs[pairs.keys.max]
> end

> require 'facet/symbol/to_proc'
> a.max_by &:size
=> "three"

Gregory Brown

1/16/2007 8:36:00 PM

0

On 1/16/07, Sam Smoot <ssmoot@gmail.com> wrote:
> Here's my try at a Ruby 1.8 implementation of the Enumerable#max_by:
>
> > a = %w{ one two three four }
>
> > def a.max_by
> > pairs = inject({}) do |hash, obj|
> > hash[yield obj] = obj; hash
> > end
> > pairs[pairs.keys.max]
> > end

Yep, this is smarter than my ruby attempt :)

Avoid the dependency on facets, Symbol#to_proc is the easiest thing in
the world to write

class Symbol
def to_proc
lambda { |x| x.send(self) }
end
end