From: Prasanth Ravi on
Robert Dober wrote:
> On Mon, Mar 8, 2010 at 10:38 PM, Prasanth Ravi <dare.take(a)gmail.com>
> wrote:
>>>> puts gets.split(' ').inject(0){|sum,x| x.to_i>0?sum+x.to_i : sum}
>>> Cheers
>>> R.
>>
>> puts gets.split.map(&:to_i).select{|x| x>0}.inject(&:+)
>>
>> --53 chars
>>
>
> I feel that your code has less characters and mine is shorter :)
> R.

yea your's is definitely better(it's more readable), but the problem
measure the code by character count :D
--
Posted via http://www.ruby-forum.com/.

From: Siep Korteling on
Prasanth Ravi wrote:
> Aaron D. Gifford wrote:
>> Ruby 1.9: 34 characters
>>
>> eval(gets.scan(/(?:^| )(\d+)/)*?+)
>>
>> Aaron out
>
> tx aaron this is by far the shortest code ive seen...

A variation:

p eval gets.split(/ |-\d+/)*?+

Siep
--
Posted via http://www.ruby-forum.com/.

From: Prasanth Ravi on
Siep Korteling wrote:
> Prasanth Ravi wrote:
>> Aaron D. Gifford wrote:
>>> Ruby 1.9: 34 characters
>>>
>>> eval(gets.scan(/(?:^| )(\d+)/)*?+)
>>>
>>> Aaron out
>>
>> tx aaron this is by far the shortest code ive seen...
>
> A variation:
>
> p eval gets.split(/ |-\d+/)*?+
>
> Siep

irb(main):003:0> p eval gets.split(/ |-\d+/)*?+
1 -2 -3 -5
SyntaxError: (eval):1: syntax error, unexpected $end
from (irb):3:in `eval'
from (irb):3
from /usr/bin/irb1.9:12:in `<main>'
irb(main):004:0> p eval gets.split(/ |-\d+/)*?+
1 2 3 4 5
15
=> 15
irb(main):005:0>

i got this output maybe some env change?
--
Posted via http://www.ruby-forum.com/.

From: Siep Korteling on
Prasanth Ravi wrote:
> Siep Korteling wrote:

> i got this output maybe some env change?

No, it just doesn't work with a trailing negative number. One more try:

p eval gets.split(/ |-\d+/)*'+0'

Siep
--
Posted via http://www.ruby-forum.com/.

From: Prasanth Ravi on
Siep Korteling wrote:
> Prasanth Ravi wrote:
>> Siep Korteling wrote:
>
>> i got this output maybe some env change?
>
> No, it just doesn't work with a trailing negative number. One more try:
>
> p eval gets.split(/ |-\d+/)*'+0'
>
> Siep

yea it works perfectly...
29 chars...
we went from 53 to 29 .. nice tx man..
and pythons was 27...
pretty nice.. gues it's my final solution.. tx again for all who post a
reply...
--
Posted via http://www.ruby-forum.com/.