Your submission must take a list of numbers (in whatever list format your language supports, or using multiple function / command-line parameters) or a string of numbers separated by any character that is not 0123456789. In one language, it must add all of them and output the sum. In another language, it must output them subtracted from each other in order. Example:

12
5
7
2

In one language, it must output 26, and in another it must output -2. Note that all numbers inputted will be positive integers less than 100. There will never be more than 20 numbers provided, so you will never output greater than 2000 or less than -1899. Any questions? Comment below!

share|improve this question
    
Is trailing whitespace in the output allowed? – Basic Sunset yesterday
    
Are two different versions of the same language allowed? See the Python 2 / 3 Answer by HyperNeutrino – Mr. Xcoder yesterday
    
@Mr.Xcoder it's allowed. – programmer5000 yesterday
2  
@BetaDecay you think that's a problem, check out code-golf! – programmer5000 yesterday
1  
@programmer5000 You mean [code-golf] -[polyglot]? – Erik the Outgolfer 20 hours ago

11 Answers 11

Python 2 / 3, 52 bytes

lambda l:sum(l[:1]+[x*int(1-(1/2)*4)for x in l[1:]])

int(1-(1/2)*4) returns 1 in Python 2 because 1/2 evaluates first to 0, and then 0 * 4 = 0.

int(1-(1/2)*4) returns -1 in Python 3 because 1/2 evaluates first to 0.5, and then int(0.5 * 4) = int(2.0) = 2.

share|improve this answer
1  
Is using the same language, with other versions even allowed? – Mr. Xcoder yesterday
3  
@Mr.Xcoder I don't see why not, I've seen other languages using either different versions of C, Java, Python, and Befunge. I'll ask on meta though or see if I can find a related meta post. – HyperNeutrino yesterday
    
Good use of math tricks due to version changes, though. – Mr. Xcoder yesterday
1  
@Mr.Xcoder Thanks! I've asked the question on meta here; I should hopefully get an answer soon. – HyperNeutrino yesterday
1  
@programmer5000 Integer division. – HyperNeutrino yesterday

C and C++ (both from GCC), 81 75 73 bytes

int c;int f(int*a,int l){auto d=.5;c=*a++;for(;--l;a++)c+=*a-4*d**a;c=c;}

Takes a pointer to array and length.

Explanation: still using @Steadybox' explanation :p In C, auto d=.5 declares an integer variable with the auto storage class (which is the default), which is then initialized to 0, whereas in C++11 it declares a double, which is initialized to 0.5.

C - plus language: Try it online!

C++ - minus language: Try it online!

share|improve this answer

05AB1E / Jelly, 5 bytes

00000000: 4f71 7f5f 2f                             Oq._/

Try it online! (05AB1E)
Try it online! (Jelly)

05AB1E sees:

Oq_/
Explanation:

Oq_/ 1 implicit argument.
O     Take the sum of the first input item.
 q    Quit.
  _/ Not functional.

Jelly sees:

Oq
_/
Explanation:

_/ Main link. Arguments: z
_/ Subtract z's elements in order.

Oq Helper link. Not functional.
share|improve this answer
    
Does this use the Jelly codepage? – programmer5000 yesterday
    
@programmer5000 This is a raw bytestream that uses CP-1252 for 05AB1E and JELLY for Jelly. Hence the 7f. – Erik the Outgolfer yesterday
    
Oh, didn't realize that! – programmer5000 yesterday

Jelly / 05AB1E, 3 bytes

00000000: c6 71 53                                         .qS

This is a hexdump (xxd) of the submitted program. It cannot be tested in raw form online; TIO doesn't support the CP437 encoding.

Jelly: Sum

Jelly uses the Jelly code page, so it sees the following characters.

İqS

Try it online!

How it works

İqS  Main link. Argument: A (array)

İ    (ignored)
 q   Unrecognized token. This breaks the link; nothing to the left is executed.
  S  Take the sum of A.

05AB1E: Difference

05AB1E uses Windows-1252, so it sees the following characters.

ÆqS

Try it online!

How it works

Æ     Take the difference of the input.
 q    Quit.
  S   (ignored)
share|improve this answer

Actually / Jelly, 4 bytes

00000000: e4 81 5f 2f                                      .._/

This is a hexdump (xxd) of the submitted program. It cannot be tested in raw form online; TIO doesn't support the CP437 encoding.

Actually: Sum

Actually uses CP 437, so it sees the following characters.

Σü_/

Try it online!

How it works

Σ     Take the sum on the input.
 ü    Print and empty the stack.
  _   Natural logarithm. Ignored since the stack is empty.
   /  Float division. Ignored since the stack is empty.

Jelly: Difference

Jelly uses the Jelly code page, so it sees the following characters.

ỵ¹_/

Try it online!

How it works

ỵ¹_/  Main link. Argument: A (array)

ỵ     Unrecognized token. Splits the link.
 ¹    Identity; yield A.
  _/  Reduce (/) A by subtraction (_).
share|improve this answer
    
I read that as actually, Jelly. :) – programmer5000 yesterday
    
You abused the fact that an undefined character acts like :) – Erik the Outgolfer yesterday
1  
@EriktheOutgolfer More or less. To be perfectly honest, I'm not quite sure what unrecognized tokens do. – Dennis yesterday
    
On second thought, I think that you just chose Actually just because it has ü assigned to the perfect function >_> – Erik the Outgolfer yesterday

JS (ES6), CGL (CGL Golfing Language), 32 27 bytes

 x=>eval(x.join`-`)
//-⨥

JS does the subtraction and CGL does the addition.

JS:

x=>eval(x.join`-`)

An anonymous function that subtracts each element in the array using Array#reduce.

//-⨥

A comment.

CGL

 x=>eval(x.join`-`)

What looks like a space on the first line is actually a non-breaking space, a comment in CGL. The first line is ignored.

//-⨥

The /s do nothing. The - decrements the current stack pointer so it is pointing to input. adds the current stack (input) together, pushes that to the next stack, and increments the current stack. It is implicitly outputted.

share|improve this answer
1  
You can shorten the JS version by using x=>eval(x.join`-`), saving 5B – Luke yesterday
    
@Luke smart! Thanks! – programmer5000 yesterday

Python 2 and 3, 44 41 bytes

lambda l:eval(l.replace(' ','-+'[1/2>0]))

Takes space-separated numbers.


-3 bytes thanks to @JonathanAllan

Try it online: Python 2 (minus) Python 3 (plus)

share|improve this answer
1  
Use '-+'[1/2>0] to save three bytes. – Jonathan Allan yesterday

CJam/Jelly, 6 bytes

q~:-
S

CJam

q~    e# Read a list from input
:-    e# Reduce by subtraction
S     e# Push a space
      e# Implicit output

Try it online!

Jelly

(using UTF-8, not the Jelly code page)

q~:- is the helper link. Since it doesn't get called, it really doesn't matter what it does. S computes the sum of an array.

Try it online!

share|improve this answer
3  
Jam and Jelly. Makes sense to use two similar things together. – mbomb007 yesterday
1  
This is invalid. Jelly has 0x7f for newline, this has 0x0a for newline. In Jelly, this is actually the same as q~:-½S. Unfortunately, the fix (q~:-e#\x7fS) is larger. Try it online! (CJam) and Try it online!, each one with its own encoding to test for yourself. – Erik the Outgolfer 20 hours ago
1  
Alternatively, you can use UTF-8 encoding for Jelly, which makes this valid as it currently is, but please specify it. – Erik the Outgolfer 20 hours ago

Python 2 and 3, 33 bytes

lambda l,*r:l+sum(r)*(1/2>0 or-1)

Takes input as separate parameters.


Python 2.
Python 3.

share|improve this answer

Brain-Flak / Brain-Flueue, 20 bytes

({}<([({{}})]){}>{})

Try it online! (Brain-Flak) (plus)

Try it online! (Brain-Flueue) (minus)

Explanation

The only difference between Brain-Flak and Brain-Flueue is that Brain-Flueue replaces the two stacks (last in first out) used in Brain-Flak with two queues (first in first out). Naturally this program uses this difference.

Annotated Code

(                  ) Push the sum of...
 {}                   the first input,
   <            >     zero,
                 {}   the remaining sum (see below)
    ([      ])       Push the of below line
      ({{}})         Pop all the input sans first, push the sum
              {}     Pop (in Brain-Flak, this discards the negative sum,
                            in Brain-Flueue, the positive)
share|improve this answer

JavaScript / Cubix, 36 bytes

//.!v+u;$I^@O<.Iu
a=>eval(a.join`-`)

Try it!

The JavaScript function can be tested using the snippet below, the Cubix program can be tested here.

How does this work?

JavaScript

The first line is a line comment to JavaScript, since it starts with two slashes, so JavaScript only sees the bottom line (a=>eval(a.join`-`)), which takes an array as input, joins it with minus signs inbetween, and then runs that as code, resulting in the subtraction of all elements in the array.

let f=
//.!v+u;$I^@O<.Iu
a=>eval(a.join`-`)

console.log(f([1,2,3,4,5]))
console.log(f([3,1,4,1,5]))

Cubix

Cubix sees the following cube (notice that Cubix ignores all whitespace):

      / / .
      ! v +
      u ; $
I ^ @ O < . I u a = > e
v a l ( a . j o i n ` -
` ) . . . . . . . . . .
      . . .
      . . .
      . . .

The Beginning

The IP starts on the third line, pointing east. It hits the 'I' command, which takes a number from the input, and pushes it to the stack. Then, it is redirected by '^' into the sum-loop.

Sum-loop

I removed all characters not part of the sum loop, and replaced them by no-ops ('.'). The IP initally arrives on the second line, pointing east.

      . . .
      ! v +
      u ; $
. . . . . . I u . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
      . . .
      . . .
      . . .

First, the '!' command checks the top element. If that is 0 (i.e. we have reached the end of the input), the next instruction ('v') is executed, reflecting the IP out of the loop. If we have not yet reached the end of the input, we add the top two items together ('+', the second item is the sum up to that point, the top item the new input). Then, the IP wraps to another face of the cube, into the 'u' character, which causes the IP to make a u-turn, and execute a 'I' command (read another input integer), while pointing north. The IP wraps back to the top face, skips ('$') the delete instruction (';') and makes another u-turn, back to the point at which we started.

The End

If the IP is reflected out of the loop, the following characters are executed:

      . . .
      . v .
      . ; .
. . @ O < . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
      . . .
      . . .
      . . .

These instructions delete the top element (which is zero), and then output the top element (the sum) as an integer. Then the '@' command is reached, so the program ends.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.