52 Replies - 11247 Views - Last Post: 06 November 2012 - 01:00 PM
#1
Code Golf: Get sum of positive integers from a set
Posted 20 September 2012 - 09:51 PM
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.
Replies To: Code Golf: Get sum of positive integers from a set
#2
Re: Code Golf: Get sum of positive integers from a set
Posted 20 September 2012 - 10:01 PM
94 characters if using GCC, and taking advantage of empty clauses within a ternary operator as proposed by aresh:
92 if abusing the MSVC headers and relying on _STD to be defined:
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
#3
Re: Code Golf: Get sum of positive integers from a set
Posted 20 September 2012 - 10:48 PM
This post has been edited by Tinix: 21 September 2012 - 07:53 AM
#4
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 01:51 AM
Works on MS C. Relies on global init to 0, main not having a return type, and C not requiring an include directive.
#5
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 06:49 AM
GWatt, on 21 September 2012 - 01:51 AM, said:
Works on MS C. Relies on global init to 0, main not having a return type, and C not requiring an include directive.
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.
#6
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 07:00 AM
Tinix, on 20 September 2012 - 10:48 PM, said:
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".
#7
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 07:05 AM
#8
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 07:52 AM
Skydiver, on 21 September 2012 - 04:00 PM, said:
Tinix, on 20 September 2012 - 10:48 PM, said:
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.
#9
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 08:40 AM
Yeah, 77 characters.
This post has been edited by aresh: 21 September 2012 - 08:41 AM
#10
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 08:55 AM
import sys print(sum([int(i) for i in sys.argv[2:] if int(i) > 0])) #57 chars C/C++ guys
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
#11
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 08:58 AM
input i numbers, sum +ve ones, and print it
Simple, wasn't it?
#12
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 11:38 AM
#13
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 11:40 AM
And sepp, where is your attempt? We are not a code writing service, you should write your own code
#14
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 11:49 AM
atraub, on 21 September 2012 - 08:55 AM, said:
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!)
Maybe I should have stipulate that the input is coming in from standard input or the console, not the command line.
#15
Re: Code Golf: Get sum of positive integers from a set
Posted 21 September 2012 - 12:10 PM
Skydiver, on 21 September 2012 - 08:49 PM, said:
atraub, on 21 September 2012 - 08:55 AM, said:
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!)
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
|
|

New Topic/Question
Reply


MultiQuote




|