Shortest code

P

Prasanth Ravi

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

Robert Dober

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 basi=
c)

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

would be my choice.

R.

is there a shorter version?



--=20
Learning without thought is labor lost; thought without learning is perilou=
s.=94
--- Confucius
 
P

Prasanth Ravi

Robert said:
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...
 
F

Florian Aßmann

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

Am 08.03.2010 um 22:10 schrieb Prasanth Ravi:
 
P

Prasanth Ravi

Florian said:
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
 
J

Joel VanderWerf

Prasanth said:
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}
 
P

Prasanth Ravi

Aaron said:
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
 
R

Robert Dober

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

--46 chars

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.
 
P

Prasanth Ravi

Robert said:
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
 
P

Prasanth Ravi

Siep said:
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?
 
P

Prasanth Ravi

Siep said:
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...
 
F

Florian Aßmann

interesting...

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

ol' ruby?

Am 09.03.2010 um 04:39 schrieb Prasanth Ravi:
Florian said:
puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
=20
Am 08.03.2010 um 22:10 schrieb Prasanth Ravi:
=20
irb(main):001:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
1 2 3 4
10
=3D> nil
irb(main):002:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
1 -2 3 4
10
=3D> nil
irb(main):003:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
1 -2 -34 5
42
=3D> nil
irb(main):004:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
-234
0
=3D> nil
irb(main):005:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')
-2 -3 -4
7
=3D> nil
irb(main):006:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'0')
1 2 3 43
49
=3D> nil
irb(main):007:0> puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'0')
1 -2 3 4
10
=3D> nil
=20
=20
it's shorter code(45 chars) but i think negative numbers also get = added=20
to result( or not- check case 5), seems can't get below 35
--=20
Posted via http://www.ruby-forum.com/.
=20
 
P

Prasanth Ravi

Florian said:
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 Ravi:

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 ? :D
 
R

Robert Dober

Florian said:
interesting...

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

ol' ruby?

Am 09.03.2010 um 04:39 schrieb Prasanth Ravi:

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

was it -(56) or -5 & 6 ? :D
1 -2 8 is nice too ;)
Even spec your golfs !!!


--=20
Learning without thought is labor lost; thought without learning is perilou=
s.=94
--- Confucius
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top