Plus or minus polyglots!

14

3

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!

programmer5000

Posted 2017-04-18T12:25:47.667

Reputation: 7 828

Is trailing whitespace in the output allowed? – Business Cat – 2017-04-18T13:10:34.597

Are two different versions of the same language allowed? See the Python 2 / 3 Answer by HyperNeutrino

– Mr. Xcoder – 2017-04-18T13:12:06.860

@Mr.Xcoder it's allowed. – programmer5000 – 2017-04-18T13:24:35.880

@BasicSunset yes. You may also require a trailing newline in the input. – programmer5000 – 2017-04-18T13:41:55.770

Ugh, so many polyglot questions recently :P – Beta Decay – 2017-04-18T23:23:04.083

@BetaDecay is there...a problem?? – programmer5000 – 2017-04-18T23:38:39.117

Nah, just it'd be nice if there was a little more variation – Beta Decay – 2017-04-18T23:45:20.480

2@BetaDecay you think that's a problem, check out code-golf! – programmer5000 – 2017-04-18T23:46:32.977

1@programmer5000 You mean [code-golf] -[polyglot]? – Erik the Outgolfer – 2017-04-19T08:15:37.217

Answers

14

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)

Dennis

Posted 2017-04-18T12:25:47.667

Reputation: 196 637

22

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.

HyperNeutrino

Posted 2017-04-18T12:25:47.667

Reputation: 26 575

1Is using the same language, with other versions even allowed? – Mr. Xcoder – 2017-04-18T13:07:57.410

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 – 2017-04-18T13:08:58.033

Good use of math tricks due to version changes, though. – Mr. Xcoder – 2017-04-18T13:10:04.947

1

@Mr.Xcoder Thanks! I've asked the question on meta here; I should hopefully get an answer soon.

– HyperNeutrino – 2017-04-18T13:12:06.320

And I've asked the author of the challenge :) – Mr. Xcoder – 2017-04-18T13:12:58.133

@Mr.Xcoder I've also asked on TNB :P – HyperNeutrino – 2017-04-18T13:16:20.950

@Mr.Xcoder Turns out that it's up to OP, so we'll just wait for programmer5000 to answer your comment on the OP. :) Also move to chat if there's anything else you want to discuss to avoid chatty comments :)

– HyperNeutrino – 2017-04-18T13:19:21.467

@Mr.Xcoder as I said above, it's OK. – programmer5000 – 2017-04-18T13:24:36.930

Why does 1/2 evaluate to 0? – programmer5000 – 2017-04-18T13:26:06.633

1@programmer5000 Integer division. – HyperNeutrino – 2017-04-18T13:26:33.553

14

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!

betseg

Posted 2017-04-18T12:25:47.667

Reputation: 8 493

11

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.

Erik the Outgolfer

Posted 2017-04-18T12:25:47.667

Reputation: 38 134

Does this use the Jelly codepage? – programmer5000 – 2017-04-18T13:00:17.247

@programmer5000 This is a raw bytestream that uses CP-1252 for 05AB1E and JELLY for Jelly. Hence the 7f. – Erik the Outgolfer – 2017-04-18T13:01:19.363

Oh, didn't realize that! – programmer5000 – 2017-04-18T13:02:42.840

8

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 (_).

Dennis

Posted 2017-04-18T12:25:47.667

Reputation: 196 637

I read that as actually, Jelly. :) – programmer5000 – 2017-04-18T13:40:05.933

You abused the fact that an undefined character acts like :) – Erik the Outgolfer – 2017-04-18T13:42:14.677

1@EriktheOutgolfer More or less. To be perfectly honest, I'm not quite sure what unrecognized tokens do. – Dennis – 2017-04-18T13:50:02.273

On second thought, I think that you just chose Actually just because it has ü assigned to the perfect function >_> – Erik the Outgolfer – 2017-04-18T13:52:49.013

@programmer5000 It's jelly, actually. – Arjun – 2017-04-20T06:20:01.007

8

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)

ovs

Posted 2017-04-18T12:25:47.667

Reputation: 21 408

1Use '-+'[1/2>0] to save three bytes. – Jonathan Allan – 2017-04-18T18:44:00.363

7

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!

Business Cat

Posted 2017-04-18T12:25:47.667

Reputation: 8 927

5Jam and Jelly. Makes sense to use two similar things together. – mbomb007 – 2017-04-18T14:11:08.893

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 – 2017-04-19T08:11:14.770

2Alternatively, you can use UTF-8 encoding for Jelly, which makes this valid as it currently is, but please specify it. – Erik the Outgolfer – 2017-04-19T08:12:59.913

5

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.

Luke

Posted 2017-04-18T12:25:47.667

Reputation: 4 675

4

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.

programmer5000

Posted 2017-04-18T12:25:47.667

Reputation: 7 828

1You can shorten the JS version by using x=>eval(x.join\-`)`, saving 5B – Luke – 2017-04-18T18:00:12.003

Looks like 26 bytes. – Adám – 2017-04-20T14:22:40.740

@Adám you are right. Fixed. – programmer5000 – 2017-04-20T14:50:28.207

4

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.

Veedrac

Posted 2017-04-18T12:25:47.667

Reputation: 711

lambda l,*r:l+sum(r)*(1/2-.5)*2 for 31 bytes – ovs – 2017-04-21T12:10:31.267

@ovs I purposefully avoided returning a float. If you are allowed to, then *(1/2*4-1) is a byte less than yours. – Veedrac – 2017-04-21T12:34:32.923

I think the return type does not really matter as long the value is correct – ovs – 2017-04-21T12:40:21.713

4

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.)

Arjun

Posted 2017-04-18T12:25:47.667

Reputation: 4 544

3

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)

0 '

Posted 2017-04-18T12:25:47.667

Reputation: 3 439