14
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ Is trailing whitespace in the output allowed? \$\endgroup\$ – Business Cat Apr 18 '17 at 13:10
  • \$\begingroup\$ Are two different versions of the same language allowed? See the Python 2 / 3 Answer by HyperNeutrino \$\endgroup\$ – Mr. Xcoder Apr 18 '17 at 13:12
  • \$\begingroup\$ @Mr.Xcoder it's allowed. \$\endgroup\$ – programmer5000 Apr 18 '17 at 13:24
  • 2
    \$\begingroup\$ @BetaDecay you think that's a problem, check out code-golf! \$\endgroup\$ – programmer5000 Apr 18 '17 at 23:46
  • 1
    \$\begingroup\$ @programmer5000 You mean [code-golf] -[polyglot]? \$\endgroup\$ – Erik the Outgolfer Apr 19 '17 at 8:15

12 Answers 12

14
\$\begingroup\$

Jelly / 05AB1E, 3 bytes

00000000: c6 71 53                                         .qS

This is a hexdump (xxd) of the submitted program.

Try it online!

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)
\$\endgroup\$
22
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is using the same language, with other versions even allowed? \$\endgroup\$ – Mr. Xcoder Apr 18 '17 at 13:07
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ – HyperNeutrino Apr 18 '17 at 13:08
  • \$\begingroup\$ Good use of math tricks due to version changes, though. \$\endgroup\$ – Mr. Xcoder Apr 18 '17 at 13:10
  • 1
    \$\begingroup\$ @Mr.Xcoder Thanks! I've asked the question on meta here; I should hopefully get an answer soon. \$\endgroup\$ – HyperNeutrino Apr 18 '17 at 13:12
  • 1
    \$\begingroup\$ @programmer5000 Integer division. \$\endgroup\$ – HyperNeutrino Apr 18 '17 at 13:26
14
\$\begingroup\$

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!

\$\endgroup\$
11
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ Does this use the Jelly codepage? \$\endgroup\$ – programmer5000 Apr 18 '17 at 13:00
  • \$\begingroup\$ @programmer5000 This is a raw bytestream that uses CP-1252 for 05AB1E and JELLY for Jelly. Hence the 7f. \$\endgroup\$ – Erik the Outgolfer Apr 18 '17 at 13:01
  • \$\begingroup\$ Oh, didn't realize that! \$\endgroup\$ – programmer5000 Apr 18 '17 at 13:02
8
\$\begingroup\$

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 (_).
\$\endgroup\$
  • \$\begingroup\$ I read that as actually, Jelly. :) \$\endgroup\$ – programmer5000 Apr 18 '17 at 13:40
  • \$\begingroup\$ You abused the fact that an undefined character acts like :) \$\endgroup\$ – Erik the Outgolfer Apr 18 '17 at 13:42
  • 1
    \$\begingroup\$ @EriktheOutgolfer More or less. To be perfectly honest, I'm not quite sure what unrecognized tokens do. \$\endgroup\$ – Dennis Apr 18 '17 at 13:50
  • \$\begingroup\$ On second thought, I think that you just chose Actually just because it has ü assigned to the perfect function >_> \$\endgroup\$ – Erik the Outgolfer Apr 18 '17 at 13:52
  • \$\begingroup\$ @programmer5000 It's jelly, actually. \$\endgroup\$ – Arjun Apr 20 '17 at 6:20
8
\$\begingroup\$

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)

\$\endgroup\$
  • 1
    \$\begingroup\$ Use '-+'[1/2>0] to save three bytes. \$\endgroup\$ – Jonathan Allan Apr 18 '17 at 18:44
7
\$\begingroup\$

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!

\$\endgroup\$
  • 5
    \$\begingroup\$ Jam and Jelly. Makes sense to use two similar things together. \$\endgroup\$ – mbomb007 Apr 18 '17 at 14:11
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Erik the Outgolfer Apr 19 '17 at 8:11
  • 2
    \$\begingroup\$ Alternatively, you can use UTF-8 encoding for Jelly, which makes this valid as it currently is, but please specify it. \$\endgroup\$ – Erik the Outgolfer Apr 19 '17 at 8:12
5
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ lambda l,*r:l+sum(r)*(1/2-.5)*2 for 31 bytes \$\endgroup\$ – ovs Apr 21 '17 at 12:10
  • \$\begingroup\$ @ovs I purposefully avoided returning a float. If you are allowed to, then *(1/2*4-1) is a byte less than yours. \$\endgroup\$ – Veedrac Apr 21 '17 at 12:34
  • \$\begingroup\$ I think the return type does not really matter as long the value is correct \$\endgroup\$ – ovs Apr 21 '17 at 12:40
4
\$\begingroup\$

JS (ES6), CGL (CGL Golfing Language), 32 26 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.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can shorten the JS version by using x=>eval(x.join`-`), saving 5B \$\endgroup\$ – Luke Apr 18 '17 at 18:00
  • \$\begingroup\$ Looks like 26 bytes. \$\endgroup\$ – Adám Apr 20 '17 at 14:22
  • \$\begingroup\$ @Adám you are right. Fixed. \$\endgroup\$ – programmer5000 Apr 20 '17 at 14:50
4
\$\begingroup\$

JavaScript (ES6)/ QBasic, 84 83 bytes

'';f=x=>eval(x.join`+`)/*
INPUT a
FOR i=1 TO 2
i=0
INPUT n
a=a-n
PRINT a
NEXT i
'*/

Another solution with the comment hack!

JavaScript computes the sum. It takes in an array containing numbers as input. Outputs as function return. You can call the function like alert(f([10,20,30])).

QBasic computes the difference. Repeatedly asks for input. As soon as you enter a value, it outputs the difference of all the numbers you have entered 'til the time of hitting Enter and again asks for input. Keeps on doing the same until the end of everything.


How does it work?

In QBasic (a language of structured BASIC family; it does not require line numbers), ' marks the beginning of a comment which goes till the end of the line. Whereas in JavaScript, it marks the start of a string. So, the whole first line is marked as a comment in QBasic but in JavaScript, the line is executed (and this line contains the JavaScript part that adds the numbers as well as a /* at the end which starts a comment in order to hide the rest of the QBasic code from JavaScript interpreter.)

The code from second line to second-last line contains the QBasic code to compute the difference of all the input numbers (the code is very self-explanatory).

The last line contains '*/. ' causes the QBasic interpreter to interpret the following code as a comment, whereas in JavaScript, it does not have any effect as it is a part of a comment (which was started at the end of the first line). The following code (*/) causes JavaScript to end the comment which was started in the first line, but it is not executed in QBasic because QBasic thinks it's a part of a comment.


Test Cases

JavaScript (adds):

'';f=x=>eval(x.join`+`);/*
INPUT a
FOR i=1 TO 2
i=0
INPUT n
a=a-n
PRINT a
NEXT i
'*/
console.log(f([12,5,7,2]));

QBasic (subtracts):

Go to this website. Copy paste the following code in their text-editor :

1 '';f=x=>eval(x.join`+`);/*
2 INPUT a
3 FOR i=1 TO 2
4 i=0
5 INPUT n
6 a=a-n
7 PRINT a
8 NEXT i
9 '*/

The reason why line numbers are required is that the website I mentioned only supports unstructured BASIC languages. And that website is the only decent online BASIC interpreter I could find. However, running the code present in the top of the post (the code without line numbers) should work fine in any good QBasic interpreter that supports structured BASIC and ' as a comment-starter (few do not, most do, though).


Notes

  • This is my first post! I hope it's a good one!
  • The QBasic part of the code does not require END because the interpreter will never reach it! (It will forever be stuck in the infinite loop; always asking for more input.)
\$\endgroup\$
3
\$\begingroup\$

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)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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