19
\$\begingroup\$

Well, everyone loves Polyglots. You will be given two integers, in any standard form of input (no hardcoding). Your task is to write a polyglot which finds the minimum value in a language and the maximum value between the two numbers in the other language, and performs the following operations:

  • The code which finds the maximum value must also compute their sum.
  • The program which finds the minimum value must also compute the result of their subtraction (max - min)
  • Here is the "tricky part": If the two numbers are equal, both the programs must not output/ return anything (both to STDOUT and STDERR or any other return method)
  • See the Output specs section for more details about formatting

Input

As stated above, two integers taken as input in any standard method, common to both languages.

Output specs

  • For the program which finds the max, the format should be: max_value, addition result
  • For the program which finds the min, the format should be min_value, subtraction result (max - min)
  • The results can be printed, with any clear delimiter ( ,\n,, or whatever else you want), returned from the function as a string containing the two expected values with a delimiter or as a list of numbers (e.g: [max_value,sum])

Examples:

Input   || Max Language Output || Min Language Output

100, 40 || 100, 140            || 40, 60
63, 67  || 67, 130             || 63, 4
-45, -5 || -5, -50             || -45, 40
21, 21  ||                     ||
-1, 1   || 1, 0                || -1, 2 

Scoring:

This is , so try to write the shortest code to get the desired results, while taking note that Standard Loopholes are strictly disallowed. You must use two different languages, not other versions of the same language (e.g: Python 2-Python 3 pairs are not valid)

\$\endgroup\$
3
  • \$\begingroup\$ @downvoter why was that? \$\endgroup\$
    – Mr. Xcoder
    Commented Apr 18, 2017 at 10:49
  • 7
    \$\begingroup\$ I'd recommend keeping questions at the sandbox for at least a couple of days so that they gain enough attention before posting on main. I didn't downvote. \$\endgroup\$ Commented Apr 18, 2017 at 11:19
  • \$\begingroup\$ Goddammit, wrote a Py2/Py3 answer and just as I was about to post saw the rule. 30 minutes i'll never get back Haha. \$\endgroup\$
    – sagiksp
    Commented May 3, 2017 at 6:24

6 Answers 6

18
\$\begingroup\$

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!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Very smart solution. I like the fact that no comments were used. \$\endgroup\$
    – Mr. Xcoder
    Commented Apr 18, 2017 at 11:28
  • 5
    \$\begingroup\$ To save a few bytes, you could declare a variable auto c=.5, and then use c instead of sizeof'a'-1. 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. \$\endgroup\$
    – Steadybox
    Commented Apr 18, 2017 at 12:18
9
\$\begingroup\$

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
\$\endgroup\$
6
  • \$\begingroup\$ Interesting language choice \$\endgroup\$
    – Mr. Xcoder
    Commented Apr 18, 2017 at 12:24
  • \$\begingroup\$ It's almost certainly possible to combine the Jelly code with another golfing language to get the byte count down. \$\endgroup\$ Commented Apr 18, 2017 at 12:33
  • \$\begingroup\$ Umm... your code snippets appear to be different. \$\endgroup\$ Commented Apr 18, 2017 at 12:49
  • 1
    \$\begingroup\$ @EriktheOutgolfer those code blocks are just representations of the raw bytes (hence the \x7f and \x04 for the unprintables in the Python one). \$\endgroup\$ Commented Apr 18, 2017 at 12:56
  • 1
    \$\begingroup\$ @JonathanAllan No, I mean that your snippets are really different. Just see the Python code for each one. \$\endgroup\$ Commented Apr 18, 2017 at 12:59
8
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ That's clever! +1 \$\endgroup\$
    – Arjun
    Commented Apr 20, 2017 at 10:31
6
\$\begingroup\$

05AB1E / Jelly, 21 20 bytes

-1 byte after asking for some help - thanks Emigna! (` will push(uwrapped(pop())))

Raw bytes (the dump to the right shows what my Windows machine displays):

60 ca 69 b9 5a 73 4f 29 71 93 18 fa 2c d3 f7 d3    `Êi¹ZsO)q..ú,Ó÷Ó
cd 04 18 2f                                         Í../

Both take input from STDIN and output to STDOUT as a list representation [x, y].

The max language is 05AB1E:

`Êi¹ZsO)q“.ú,Ó÷ÓÍ../

Where the . represent unprintable bytes in it's codepage (cp1252), and probably here in whatever you are using (0x18=CAN and 0x04=EOT).

Try the 05AB1E version

The min language is Jelly:

İ__iỤZs0)qƓð«,ạẋạṠ¥ð/

Try the Jelly version.

How?

05AB1E:

`Êi¹ZsO)q“.ú,Ó÷ÓÍ../ - parsed left to right, executed as parsed
                     - implicit input taken
  i                  - If:
`                    -     push(unwrapped(pop()))
 Ê                   -     push(pop()!=pop())
                     - ...Then:
   ¹                 -     push(1st item from input history)
    Z                -     push(max(top of stack))
     s               -     push(reverse(pop(),pop()))
      O              -     push(sum(pop()))
       )             -     wrap(pop())
        q            -     quit - implicit print of top of stack
         “.ú,Ó÷ÓÍ../ - unexecuted and unparsed string

Jelly:

`ȮiỤZs0)qƓð«,ạẋạṠ¥ð/ - parsed left to right, not executed until parsed
`ȮiỤZs0              - a dyadic chain which would error if executed (monad from dyad, index of, grade up, transpose, split into chunks of length 0)
       )q            - currently unassigned code points each currently creates a new link
         Ɠ           - read and evaluate a line of input
          ð       ð  - dyadic chain separations
                   / - reduce by:
           «         -     minimum(left, right)
             ạ       -     absolute difference(left, right)
            ,        -     pair(result)
                 ¥   -     last two links as a dyad:
               ạ     -         absolute difference(left, right)
                Ṡ    -         sign(result) (i.e. {0:0, +x:1, -x:-1} but -x never happens)
              ẋ      -     repeat list left right times
                     - implicit print
\$\endgroup\$
4
\$\begingroup\$

Java/AWK, 219 217 212 196 bytes

/*#\/* /
{$0=((x=$1)<y=$2)?x" "y-x:y" "x-y}x!=y
#*/interface B{static void main(String[]A){Integer x=Integer.parseInt(A[0]);int y=x.parseInt(A[1]);System.out.print(x==y?"":(x<y?y:x)+" "+(x+y));}}

Try it online!

Java outputs the max and sum, AWK outputs min and difference. No output for either if the inputs are identical.

This is my first polyglot and first TIO :)

\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6)/ QBasic, 172 171 bytes

'';f=x=>{m=Math.max(...x);s=Math.min(...x);if(x[0]!=x[1])alert([m,m-s])}/*
INPUT c
LET m=c
LET s=c
INPUT n
IF n>c THEN m=n
IF n<c THEN s=n
IF n<>c THEN PRINT m,m+s
END
'*/

Based on this solution of mine of a similar question.

This solution too use the comment approach!

JavaScript is the min-language. It takes in an array containing numbers as input. Outputs two numbers separated by , (1st number is the smallest value of the input-array and 2nd number is the difference of the greatest and the smallest values of the input-array) by alert()ing. Does not alert() anything if the numbers are equal. You can call the function like f([100,40]).

QBasic is the max-language. Repeatedly asks for input, twice. Prints the largest number of the input-numbers as well as the sum of largest and smallest numbers of the input. Does not PRINT anything if the numbers are equal.


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 computes the smallest number and the difference of greatest and smallest 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 greatest number and the sum of greatest and smallest number (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 (min-language):

'';f=x=>{m=Math.max(...x);s=Math.min(...x);if(x[0]!=x[1])alert([m,m-s])}/*
INPUT c
LET m=c
LET s=c
INPUT n
IF n>c THEN m=n
IF n<c THEN s=n
IF n<>c THEN PRINT m,m+s
END
'*/

f([100,40]);

QBasic (max-language):

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

1 '';f=x=>{m=Math.max(...x);s=Math.min(...x);if(x[0]!=x[1])alert([m,m-s])}/*
2 INPUT c
3 LET m=c
4 LET s=c
5 INPUT n
6 IF n>c THEN m=n
7 IF n<c THEN s=n
8 IF n<>c THEN PRINT m,m+s
9 END
10 '*/

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)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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