From: Prasanth Ravi on
hi i'm a newbie in ruby and was test out some interesting problems in
ruby,

i came across a small one to print the sum of positive numbers from a
list of n numbers... with the shortest code possible..

well the best i could do was,
puts gets.split(' ').inject(0){|sum,x| x.to_i>0?sum+x.to_i : sum}

is there a shorter version?
--
Posted via http://www.ruby-forum.com/.

From: Robert Dober on
On Mon, Mar 8, 2010 at 10:10 PM, Prasanth Ravi <dare.take(a)gmail.com> wrote:
> hi i'm a newbie in ruby and was test out some interesting problems in
> ruby,
>
> i came across a small one to print the sum of positive numbers from a
> list of n numbers... with the shortest code possible..
>
> well the best i could do was,
> puts gets.split(' ').inject(0){|sum,x| x.to_i>0?sum+x.to_i : sum}
I am afraid so
puts gets.split.map(&:to_i).inject(&:+)
although in Ruby 1.8 you need
gets.split.inject(0){ |sum, x | sum + x.to_i }
or
... inject{ | sum, x | sum.to_i + x.to_i }
if you prefer.

What do *you* think is the most readable solution BTW ;)?

Cheers
Robert

P.S.
BTW if you meant to not use negative numbers (sorry my English is very basic)

map(&:to_i).select{ |x| x > 0 }. ...

would be my choice.

R.


>
> is there a shorter version?
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
Learning without thought is labor lost; thought without learning is perilous.”
--- Confucius

From: Prasanth Ravi on
Robert Dober wrote:
> On Mon, Mar 8, 2010 at 10:10 PM, Prasanth Ravi <dare.take(a)gmail.com>
> wrote:
>> hi i'm a newbie in ruby and was test out some interesting problems in
>> ruby,
>>
>> i came across a small one to print the sum of positive numbers from a
>> list of n numbers... with the shortest code possible..
>>
>> well the best i could do was,
>> puts gets.split(' ').inject(0){|sum,x| x.to_i>0?sum+x.to_i : sum}
> I am afraid so
> puts gets.split.map(&:to_i).inject(&:+)
> although in Ruby 1.8 you need
> gets.split.inject(0){ |sum, x | sum + x.to_i }
> or
> ... inject{ | sum, x | sum.to_i + x.to_i }
> if you prefer.
>
> What do *you* think is the most readable solution BTW ;)?
>
> Cheers
> Robert
>
> P.S.
> BTW if you meant to not use negative numbers (sorry my English is very
> basic)
>
> map(&:to_i).select{ |x| x > 0 }. ...
>
> would be my choice.
>
> R.

tx for the reply, i originally used
y=0
gets.split.each{ |x|
z=x.to_i
y+=z if z>0
}
print y

--46 chars

puts gets.split.map(&:to_i).select{|x| x>0}.inject(&:+)

--53 chars

seems going the old fashioned way is shorter code,
yup wanted for x>0 and between this was a problem to see on different
languages
python was around 26 , so i was thinking if i could do less than that on
ruby...
--
Posted via http://www.ruby-forum.com/.

From: Florian Aßmann on
puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')

Am 08.03.2010 um 22:10 schrieb Prasanth Ravi:

> puts gets.split(' ').inject(0){|sum,x| x.to_i>0?sum+x.to_i : sum}


From: Prasanth Ravi on
Florian Aßmann wrote:
> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
>
> Am 08.03.2010 um 22:10 schrieb Prasanth Ravi:

irb(main):001:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
1 2 3 4
10
=> nil
irb(main):002:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
1 -2 3 4
10
=> nil
irb(main):003:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
1 -2 -34 5
42
=> nil
irb(main):004:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
-234
0
=> nil
irb(main):005:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
-2 -3 -4
7
=> nil
irb(main):006:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'0')
1 2 3 43
49
=> nil
irb(main):007:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'0')
1 -2 3 4
10
=> nil


it's shorter code(45 chars) but i think negative numbers also get added
to result( or not- check case 5), seems can't get below 35
--
Posted via http://www.ruby-forum.com/.