Just another tech blog.

A blog abt GNU/Linux, programming, hacking, and my life.

Size contest

Posted by ajay on June 1, 2006

Recently I was solving problems on Shpere online Judge and I found an interesting challange Size contest. The problems statement is that you are given a number n and a set of n integars, you have to calculate the sum of all the positive integars amongst those numbers. But the requirement is not to write the fast program but to write the shortest code for it. You might choose any language. Since I know only c/c++ and python so here is what i wrote in both of them respectively ..

# c++ code ..


int n,s=0,x;





this code is 93  characters without spaces and newlines ..

now here is the python code ….

# python code ..

for i in range(n):
if z>0:
print y

this is 53 characters without spaces and new lines ..

can anybody help  me reduce it to 70 ( in c/c++) and 26( in python ) .. which are the best codes for respective languages there or if not to that level at least reduce the code size .. 🙂 ..


10 Responses to “Size contest”

  1. its me said

    for i in range(input()):
    print x

  2. ajay said

    that was a better idea i guess .. but what if the user inputs a negative number .. we need not add that number in that case .. i think you hvnt checked that ..

  3. chaos said

    move the statement
    int n,s=0,x;
    above the main..thn u can omit initialization of s i.e ur statement can be:
    int n,s,x; //global ints initialized to 0 by default

    • Jonalyn said

      Hey Ellie’s Husband! The logo IS awesome. Thanks for the encouragement. By the way, LOTS of men are using GOLGI products too! I can’t keep my hands off of my husband’s face since he started using GOLGI – AND he looks younger! Try it! &##210;Hi&88222; to Ellie – Regards, Rita

  4. _0_m3g4 said

    for i in range(input()):
    print x
    #this is 49 chars 😉

  5. vinod said

    int n,x,s;main(){for(cin>>n;n–;s+=x>0?x:0)cin>>x;cout

  6. ajay said


    // this is 87 chars .. but this time in c

  7. rocky said

    A list of N numbers a1, a2, …, aN are given, find i and j, such that ai xor aj is maximized.can any body code a c++ proggramme for it..?????

  8. kushal7 said

    main(){int a,b,c=0;cin>>a;while(a–){cin>>b;b>0?c+=b:1;}cout<<c;}

    // 84 chars

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: