I am trying to solve these three problems in Ruby. I do not know if my solutions are really correct. I just wanted to ask help, if somebody can show their solutions. I would appreciated your help.
1. Write a one-line in irb using Range#inject to calculate 20!. Generalize this into a function.
2. Write a function to find the longest string in an array of strings.
3. Write an iterator function n_times(n) that calls the given block n times.
Thanks.
Cyrus
···
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Please don't post your homework problems on this list. If you are having trouble with a particular part of this, we'd be happy to help.
Brad
···
On Jan 15, 2007, at 1:02 AM, Cyrus Gabilla wrote:
I am trying to solve these three problems in Ruby. I do not know if my solutions are really correct. I just wanted to ask help, if somebody can show their solutions. I would appreciated your help.
1. Write a one-line in irb using Range#inject to calculate 20!. Generalize this into a function.
2. Write a function to find the longest string in an array of strings.
3. Write an iterator function n_times(n) that calls the given block n times.
Neat! Where are you taking classes on ruby? seriously.
···
On 1/15/07, Cyrus Gabilla <gabilla19992004@yahoo.com> wrote:
Hi to everybody.
I am trying to solve these three problems in Ruby. I do not know if my
solutions are really correct. I just wanted to ask help, if somebody can
show their solutions. I would appreciated your help.
1. Write a one-line in irb using Range#inject to calculate 20!. Generalize
this into a function.
2. Write a function to find the longest string in an array of strings.
3. Write an iterator function n_times(n) that calls the given block n
times.
Hi to everybody.
I am trying to solve these three problems in Ruby. I do not know if my solutions are really correct. I just wanted to ask help, if somebody can show their solutions. I would appreciated your help.
1. Write a one-line in irb using Range#inject to calculate 20!. Generalize this into a function.
2. Write a function to find the longest string in an array of strings.
3. Write an iterator function n_times(n) that calls the given block n times.
You say you have solutions but you don't know if they are correct or not. Perhaps you need to learn how to test correctness. As an example, the calculation of 20 factorial -- does Ruby have a factorial built in? If so, you could compare its results with the results from your code. If not, find another language that does, or borrow someone's engineering calculator to get the (rather large) number.
The other two are easy to test -- factorial is moderately difficult because it's such a common exercise for students that languages tend not to build it in, and because the resulting number gets very large very quickly; unless a language has facilities for computing and printing integers beyond machine precision.
So, courtesy of the Axiom system, here's 20! you can cut and paste into your test driver. I ran off 50! too because ... well, because I can.
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]
Daniel, this is a much better solution than my sort_by one. So good work!
···
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
>>
>> 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
>
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 )