Code Golf: Get sum of positive integers from a set

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

52 Replies - 21821 Views - Last Post: 06 November 2012 - 01:00 PM

#1 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Code Golf: Get sum of positive integers from a set

Post icon  Posted 20 September 2012 - 09:51 PM

This is a Code Golf challenge to implement the following in C/C++:
Make the shortest possible program that takes integer i as input. Then input i integers and print the sum of the non-negative ones. The catch is that the program should be as short as possible. Ideally under 100 characters(not including whitespace chars).

( Spoiler alert: This is from the thread Shortening This Code. )

Feel free to post your entries, but remember to use the spoiler tags to give folks a fresh view of the problem if they are coming into this late.

Also, if your code depends on a particular compiler, or a compiler "bug", be sure to note it in your description.

Is This A Good Question/Topic? 1
  • +

Replies To: Code Golf: Get sum of positive integers from a set

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: Code Golf: Get sum of positive integers from a set

Posted 20 September 2012 - 10:01 PM

95 characters for MSVC and GCC. Relies on the C++ standard where globals are initialized to zero.

Spoiler


94 characters if using GCC, and taking advantage of empty clauses within a ternary operator as proposed by aresh:
Spoiler



92 if abusing the MSVC headers and relying on _STD to be defined:

Spoiler


It's going to be tough beating aresh's 77 characters for Turbo C++, though.

This post has been edited by Skydiver: 20 September 2012 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 Tinix  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 55
  • Joined: 07-May 11

Re: Code Golf: Get sum of positive integers from a set

Posted 20 September 2012 - 10:48 PM

91 For GCC
Spoiler

This post has been edited by Tinix: 21 September 2012 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is online

  • member icon

Reputation: 262
  • View blog
  • Posts: 3,054
  • Joined: 01-December 05

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 01:51 AM

I believe I am in first now with 76 characters. In plain old C even.
Works on MS C. Relies on global init to 0, main not having a return type, and C not requiring an include directive.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 06:49 AM

View PostGWatt, on 21 September 2012 - 01:51 AM, said:

I believe I am in first now with 76 characters. In plain old C even.
Works on MS C. Relies on global init to 0, main not having a return type, and C not requiring an include directive.
Spoiler


Nice attempt, but in your race to get minimal code, you also lost some functionality that is required by this challenge.
Unfortunately, your solution doesn't filter out negative numbers from the sum. e.g. If I enter "3 1 -3 4", the output should be 5, not 2.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 07:00 AM

View PostTinix, on 20 September 2012 - 10:48 PM, said:

91 For GCC
Spoiler


Nice attempt at trying to save that extra character. Unfortunately, by dropping the semi-colon after the for loop statement to save the extra character, you end up not printing then sum of the numbers entered. Try "2 3 4". The final output should be 7, but your program prints out "03".
Was This Post Helpful? 0
  • +
  • -

#7 GWatt  Icon User is online

  • member icon

Reputation: 262
  • View blog
  • Posts: 3,054
  • Joined: 01-December 05

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 07:05 AM

Ahhhh, didn't read that carefully enough.
Was This Post Helpful? 0
  • +
  • -

#8 Tinix  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 55
  • Joined: 07-May 11

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 07:52 AM

View PostSkydiver, on 21 September 2012 - 04:00 PM, said:

View PostTinix, on 20 September 2012 - 10:48 PM, said:

91 For GCC
Spoiler


Nice attempt at trying to save that extra character. Unfortunately, by dropping the semi-colon after the for loop statement to save the extra character, you end up not printing then sum of the numbers entered. Try "2 3 4". The final output should be 7, but your program prints out "03".

I swear that semicolon was there earlier. Anyway it was 91 with semicolon so the character count does not change.
Was This Post Helpful? 0
  • +
  • -

#9 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 08:40 AM

Well, here is my solution, which I also posted earlier. It will only work on TC++ :P
Spoiler

Yeah, 77 characters.

This post has been edited by aresh: 21 September 2012 - 08:41 AM

Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 08:55 AM

Python 3.x implementation:
import sys
print(sum([int(i) for i in sys.argv[2:] if int(i) > 0]))
#57 chars C/C++ guys 

:D

EDIT:
Whoops, old code wouldn't work properly because py2 needs raw_input, and py3 needs parens.

This post has been edited by atraub: 21 September 2012 - 02:15 PM

Was This Post Helpful? 1
  • +
  • -

#11 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 08:58 AM

English implementation:
input i numbers, sum +ve ones, and print it

Simple, wasn't it? :P
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2016
  • View blog
  • Posts: 3,043
  • Joined: 21-June 11

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 11:38 AM

"Plusve" isn't an English word :P
Was This Post Helpful? 0
  • +
  • -

#13 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 11:40 AM

Huh? Where is it written?

And sepp, where is your attempt? We are not a code writing service, you should write your own code :P
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 11:49 AM

View Postatraub, on 21 September 2012 - 08:55 AM, said:

Python 2.7 implementation:
import sys
print sum([int(i) for i in sys.argv[2:] if int(i) > 0])
#55 chars C/C++ guys (2.x let's me get rid of parens around print statements for an extra 2 characters off!) 

:D


:) Nice. Next thing you know, we'll have some Perl hacker come in at around 45 characters. :lol:

Maybe I should have stipulate that the input is coming in from standard input or the console, not the command line.
Was This Post Helpful? 0
  • +
  • -

#15 Tinix  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 55
  • Joined: 07-May 11

Re: Code Golf: Get sum of positive integers from a set

Posted 21 September 2012 - 12:10 PM

View PostSkydiver, on 21 September 2012 - 08:49 PM, said:

View Postatraub, on 21 September 2012 - 08:55 AM, said:

Python 2.7 implementation:
import sys
print sum([int(i) for i in sys.argv[2:] if int(i) > 0])
#55 chars C/C++ guys (2.x let's me get rid of parens around print statements for an extra 2 characters off!) 

:D


:) Nice. Next thing you know, we'll have some Perl hacker come in at around 45 characters. :lol:

Maybe I should have stipulate that the input is coming in from standard input or the console, not the command line.

I saw one scoreboard for this problem and the top one was PERL with 6 characers.

This post has been edited by Tinix: 21 September 2012 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »