I already did this one on SPOJ. A while ago they promoted all the Python2.6 answers to Python3 even though some of them wont run under Python3 and would be longer in Python3 - eg have to use int(input()) instead of input() and print(x) instead of print x. So I don't take SPOJ very seriously anymore. I'm tied with Tim Peters and that's good enough for me :)
–
gnibbler♦Feb 24 '11 at 21:22

4

I just want to point out, that skipping the T (number of ...errr...numbers (?) ) is not an option...since the testcases involve extra data after T numbers...your code will fail at SPOJ. Everyone (3 answers below) seemed to have cleverly skipped the first integer.
–
st0leFeb 25 '11 at 9:36

(+1) Nice program! A tiny "FWIW": 9 characters can be removed due to 9 instances of an unneeded S in the binary coding of a number. These are all in push-number-to-stack instructions of the form SSSS...N, where the 4th S codes a superfluous leading 0. (Of course this has no effect on the score.)
–
r.e.s.Mar 29 '12 at 12:18

Element, 17 characters plus 1 space

_'[_ 2:n;0>[n~+]]`

This is my first constructed language. It is designed to be very compact and human-readable. All of the instructions are one character long and perform a single function.

Element has two stacks and a hash as memory structures. The two stacks are called the main stack and the control stack. The main stack is where arithmetic, I/O, and hash manipulation occurs. The control stack is where logic operations occur, and this stack controls the while and for loops.

The basic idea behind Element is that there is a hash that stores numbers/strings, while the stack is used to perform calculations on these numbers. The results to these calculation can then assigned a certain place in the hash for future use. The different contents of the hash are called elements, so it is similar to an array but can have non-numerical names.

Here is the list of operators: In some of these examples, m and n represent numbers already on the stack.

text --pushes the string "text" onto the main stack
' --pops from main stack and pushes onto control stack
" --pops from control stack and pushes onto main stack
# --pops from main stack and destroys
[] --FOR statement (view the top number number from control stack and eval those many times)
{} --WHILE (loop until top number on control stack is 0)
( --pops from main stack, removes first character, pushes the remaining string onto stack, and pushes the removed character onto stack
) --pops from main stack, removes last character, pushes the remaining string onto stack, and pushes the removed character onto stack
~ --pops from main stack, pushes contents of the element with that name
+-*/%^ --pops two most recently named elements, adds/negates/multiplies/divides/modulates/exponentiates them, and places the result on the stack
mn; --pops m and n and assigns element n the value of m
mn@ --pops m and n and moves mth thing in stack to move to place n in stack
m$ --pops m and pushs size of m onto the stack
mn: --pops m and n and pushes m onto the stack n times
mn. --pops m and n and pushes m concatonated with n
m? --pops m and pushes 0 onto control stack if m is '0' or and empty string, else pushes 1
\ --escapes out of next character, so it isn't an operator and con be pushed onto the stack
><= --pops two numbers off of stack and tests, pushes 1 onto control stack if true and 0 if false
` --pops from main stack and prints
&| --pops two items from control stack, performs and/or respectively, and pushes result back onto control stack
! --pops a number off of control stack, pushes 1 if 0 or empty string, 0 otherwise
_ --inputs a word and pushes onto main stack
m, --pops m from main stack, coverts it to char and pushes, converts to num and pushes
Newlines and spaces separate different elements to be pushed onto the stack individually, but can pushed onto the stack using \

Here is a walkthrough of how the program works:

_'[ --take the first line of input, transfer it to the control stack, and start a for loop
_ 2: --take one more line of input, and duplicate it so that there are two copies
n; --take one copy and put into element n
0> --push a zero onto the stack, remove the zero and the other copy of the input, and compare. A 1 will be placed on the control stack if the input was greater than zero, a 0 otherwise.
[ --starts another for loop if the comparison was true. This loop will be repeated once if the comparison was true and no times if it was false, so it is the same as an IF statement.
n~ --pushes n onto the main stack, then pops it ans replaces it with the contents of n, which is the number stored earlier
+ --takes the number and adds it to the running total, which is contained as the last item on the stack
] --ends the inner for loop
] --ends the outer for loop
` --print the top item (also the only item) on the stack to output

1) I have counted 90 characters. 2) return 0; is not necessary, the for cycle may be contracted to for(scanf(x,&t);t--;scanf(x,&a),a>0?b+=a:a); == which make that 78 characters...
–
V-XMar 5 '14 at 11:34

Nice function, but how does this handle the specified newline-separated input? How does it not incorporate the number of test cases parameter t as part of the sum? How does it sum only up to the number of test cases specified, even if more are given?
–
Jonathan Van MatreMar 5 '14 at 23:08

Just the standard tricks: The return is unnecessary in standard C++ or C99, there is an implicit return 0 in main. By making the variables global you can drop the =0 initialization. Finally, for(;;) is the same number of characters as while() but you get two extra places to put an expression in.
–
hanMar 25 '12 at 11:10

This is already old, but also, writing std:: before cin and cout and getting rid of the using namespace std; can save 5 more characters.
–
MorwennJul 3 '13 at 11:35

Welcome to CodeGolf.SE! If you look at the other answer, you'll see that they have formatted code and a minimal header noting the implementation language; on more complicated challenges many also have notes on the implementation and any limits or surprises in the code. Without some of this, you answer is unlikely to be well received.
–
dmckee♦Oct 12 '11 at 15:28

I counted the chars, added the indentation to make code-layout work, and removed the decoration of the output. Oh - now I have to count again. :)
–
user unknownOct 13 '11 at 14:35

Added language name. There's a lot of room for reductions here - sum can be reduced to s, the output string can just be "%d", etc.
–
GarethOct 31 '12 at 9:17