Contradictory Polyglots
C and C++ (gcc), 117 107 bytes
-10 bytes thanks to @Steadybox!
#include<stdio.h>
int f(int a,int b){auto c=.5;a-b&&printf("%d %d",c?b<a?b:a:a>b?a:b,c?a-b>0?a-b:b-a:a+b);}
Explanation: In C, auto c=.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. So the variable's value will be truthy in C++ and falsy in C.
C - max language: Try it online!
C++ - min language: Try it online!
Python 3 / Jelly, 42 bytes
Using Jelly's code-page to encode the file.
Raw bytes:
6c 61 6d 62 64 61 20 78 2c 79 3a 5b 6d 61 78 28 lambda x,y:[max(
78 2c 79 29 2c 78 2b 79 5d 2a 28 78 21 3d 79 29 x,y),x+y]*(x!=y)
0a 23 7f fa 2c d3 f7 d3 cd 04 .#.ú,Ó÷ÓÍ.
Both define an unnamed dyadic function.
Python (the max language) sees:
lambda x,y:[max(x,y),x+y]*(x!=y)
#\x7fú,Ó÷ÓÍ\x04
Tests as Python.
Jelly (the min language) sees:
lambda x,y:[max(x,y),x+y]*(x!=y)½#
«,ạẋạṠ¥
Tests as Jelly.
How?
Jelly interprets 0x0a as ½
, the square root atom while Python interprets it as a newline. In Jelly 0x7f is interpreted as a separation between links (functions) and is represented by either a newline or a pilcrow in it's codepage. For Jelly the last link is the main function - here it does not call the link above (which the interpreter does still need to parse correctly). In Python 0x23, #
instructs that anything after it and before 0x0a, a newline, is a comment.
The Python code that gets executed:
lambda x,y:[max(x,y),x+y]*(x!=y)
lambda x,y: - A function taking two inputs, x and y
[ , ] - A list with two items
max(x,y) - take the maximum of x and y
x+y - x plus y
x!=y - x not equal to y?
*( ) - multiply - True is treated as if it were 1, False as if it were 0
The Jelly code that gets executed:
«,ạẋạṠ¥ - A dyadic link (function of two variables): x, y
« - minimum(x, y)
ạ - absolute difference(x, y)
, - pair left with right (creating a list)
¥ - last two links as a dyad:
ạ - absolute difference(x, y)
Ṡ - sign(result) (i.e. {0:0, +x:1, -x:-1} but -x never happens)
ẋ - repeat the list to the left right times (either the same list or an empty list)
- return the result
Ruby / Python 3, 102 bytes
Ruby returns max/sum, Python returns min/difference. Input is an array object read from STDIN.
a=eval((0and gets or input()))
b=a.sort()
x,y=(b or a)
z=0 or 1
x==y or print([[y,x][z],[x+y,y-x][z]])
Try it online: Ruby
Try it online: Python
The main quirk used here is the use of the truthy 0
in Ruby, which is falsy in Python. The other thing worth mentioning is that Python's sort
function modifies the list in-place and returns None
, while Ruby's doesn't and returns the sorted array, hence the need to use b or a
to get the min/max.
Python 3 is required because Python 2 complains if you try to call print
after the or
statement in the last line.