Forum: Ruby Shortest code

Posted by Prasanth Ravi (daretake)
on 2010-03-08 22:10
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 by Robert Dober (Guest)
on 2010-03-08 22:20
(Received via mailing list)
On Mon, Mar 8, 2010 at 10:10 PM, Prasanth Ravi <dare.take@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.
Posted by Prasanth Ravi (daretake)
on 2010-03-08 22:38
Robert Dober wrote:
> On Mon, Mar 8, 2010 at 10:10 PM, Prasanth Ravi <dare.take@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 by Florian Aßmann (Guest)
on 2010-03-08 23:50
(Received via mailing list)
puts eval(gets.gsub(/-\d+|[^0-9]+/, '+')<<'+0')

Am 08.03.2010 um 22:10 schrieb Prasanth Ravi:
Posted by Prasanth Ravi (daretake)
on 2010-03-09 04:39
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 by Joel VanderWerf (Guest)
on 2010-03-09 05:49
(Received via mailing list)
Prasanth Ravi 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}
Posted by Aaron D. Gifford (Guest)
on 2010-03-09 06:00
(Received via mailing list)
Ruby 1.9: 34 characters

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

Aaron out
Posted by Prasanth Ravi (daretake)
on 2010-03-09 06:31
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
Posted by Prasanth Ravi (daretake)
on 2010-03-09 07:53
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...
Posted by Robert Dober (Guest)
on 2010-03-09 08:50
(Received via mailing list)
On Mon, Mar 8, 2010 at 10:38 PM, Prasanth Ravi <dare.take@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.
Posted by Prasanth Ravi (daretake)
on 2010-03-09 09:45
Robert Dober wrote:
> On Mon, Mar 8, 2010 at 10:38 PM, Prasanth Ravi <dare.take@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 by Siep Korteling (steenslag)
on 2010-03-09 11:17
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 by Prasanth Ravi (daretake)
on 2010-03-09 17:14
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 by Siep Korteling (steenslag)
on 2010-03-09 21:41
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 by Prasanth Ravi (daretake)
on 2010-03-10 05:35
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 by Florian Aßmann (Guest)
on 2010-03-10 08:23
(Received via mailing list)
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:
Posted by Prasanth Ravi (daretake)
on 2010-03-10 09:07
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 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
Posted by Robert Dober (Guest)
on 2010-03-10 10:25
(Received via mailing list)
On Wed, Mar 10, 2010 at 9:07 AM, Prasanth Ravi <dare.take@gmail.com> 
wrote:
>>  => nil
> -56
> 6
> => nil
>
> was it -(56) or -5 & 6 ? :D
> --
> Posted via http://www.ruby-forum.com/.
>
>
1 -2 8 is nice too ;)
Even spec your golfs !!!
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.