IdeaMonk

thoughts, ideas, code and other things...

Monday, June 09, 2008

When Ruby comes to help!

Recently I have been trying to solve problems on the SPOJ online judge. Earlier I used to write in C then C++ and now in C++ andd Ruby. SIZECON is a challenge problem where you have to write the shortest code to summate positive integers give by users. The score is on how short you write.
This is where C/C++ won't help you out, you have to look foreward to how perl hackers write cryptic codes.
Heres my best solution in Ruby

s=0;gets.to_i.times{b=gets.to_i;s+=b>0?b:0}
puts s

This one gets 48. Anyone got better solutions in Ruby?
Perl hackers have written 6 byte codes... that still seems impossible :O in a way.
The best in perl I could get is
while(<>){$a+=$_>0?$_:0;}print $a;

thats 34 bytes, but sadly fails to compile on the judge :(
Anyone got better insights ?

Labels: , ,

1 Comments:

At September 3, 2010 at 4:40 AM , Anonymous kokosek said...

Here's a link to shortening contest where are other tasks to shorten:
http://www.spoj.pl/SHORTEN/

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home