Shortest code

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?

On Mon, Mar 8, 2010 at 10:10 PM, Prasanth R. remo[email protected]
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.

Robert D. wrote:

On Mon, Mar 8, 2010 at 10:10 PM, Prasanth R. [email protected]
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…

puts eval(gets.gsub(/-\d+|[^0-9]+/, ‘+’)<<’+0’)

Am 08.03.2010 um 22:10 schrieb Prasanth R.:

Prasanth R. wrote:

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

You can save a char by replacing

y=0;s.split.each{|x|z=x.to_i;y+=z if z>0}

with

y=0;s.split.each{|x|y+=x.to_i if /-/!~x}

but this is even shorter

y=0;s.scan(/ \d+|^\d+/){|x|y+=x.to_i}

Florian Aßmann wrote:

puts eval(gets.gsub(/-\d+|[^0-9]+/, ‘+’)<<’+0’)

Am 08.03.2010 um 22:10 schrieb Prasanth R.:

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

Aaron D. Gifford wrote:

Ruby 1.9: 34 characters

eval(gets.scan(/(?:^| )(\d+)/)*?+)

Aaron out

edit:
y=0
s=""
s.scan(/ \d+|^\d+/){|x|y+=x.to_i}
puts y

i put puts y so i can do it in a script file instead of irb and removed
semicolons as ‘;’ counts as a char,

38 char-w/o puts y
43 chars-with puts y

this is the shortest i have seen w/o getting into an infinite loop :D,ty

Ruby 1.9: 34 characters

eval(gets.scan(/(?:^| )(\d+)/)*?+)

Aaron out

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…

On Mon, Mar 8, 2010 at 10:38 PM, Prasanth R. [email protected]
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 :slight_smile:
R.

Robert D. wrote:

On Mon, Mar 8, 2010 at 10:38 PM, Prasanth R. [email protected]
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 :slight_smile:
R.

yea your’s is definitely better(it’s more readable), but the problem
measure the code by character count :smiley:

Siep K. wrote:

Prasanth R. 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
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?

Prasanth R. 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

Prasanth R. wrote:

Siep K. 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

Siep K. wrote:

Prasanth R. wrote:

Siep K. 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…

Florian Aßmann wrote:

interesting…

ree-1.8.7-2010.01 > puts eval(gets.gsub(/(?:-\d|[^0-9])+/, ‘+’)<<’+0’)
1 2 3 -4
6
=> nil
ree-1.8.7-2010.01 > puts eval(gets.gsub(/(?:-\d|[^0-9])+/, ‘+’)<<’+0’)
1 -2 3 4
8
=> nil

ol’ ruby?

Am 09.03.2010 um 04:39 schrieb Prasanth R.:

irb(main):010:0> puts eval(gets.gsub(/(?:-\d|[^0-9])+/, ‘+’)<<’+0’)
3 4 -5
7
=> nil
irb(main):011:0> puts eval(gets.gsub(/(?:-\d|[^0-9])+/, ‘+’)<<’+0’)
-56
6
=> nil

was it -(56) or -5 & 6 ? :smiley:

interesting…

ree-1.8.7-2010.01 > puts eval(gets.gsub(/(?:-\d|[^0-9])+/, ‘+’)<<’+0’)
1 2 3 -4
6
=> nil
ree-1.8.7-2010.01 > puts eval(gets.gsub(/(?:-\d|[^0-9])+/, ‘+’)<<’+0’)
1 -2 3 4
8
=> nil

ol’ ruby?

Am 09.03.2010 um 04:39 schrieb Prasanth R.:

On Wed, Mar 10, 2010 at 9:07 AM, Prasanth R. [email protected]
wrote:

=> nil
-56
6
=> nil

was it -(56) or -5 & 6 ? :smiley:

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

1 -2 8 is nice too :wink:
Even spec your golfs !!!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs