183
\$\begingroup\$

Isn't it annoying when you find a piece of code and you don't know what language it was written in? This challenge attempts to somewhat solve this.

Challenge

You will have to write a program that when run in two different languages, will output the string:

This program wasn't written in <language program compiled/interpreted in>, it was built for <other language the program can be run in>!

  • In the output, language names should have official capitalization. eg: CJam, C++

  • Neither program should take any input from the user.

  • When run in both languages, output should be to stdout or equivalent.

  • There should be no output to stderr in either program.

  • You may use comments in either language.

  • Two different versions of the same language count as different languages.

    • If this is done, the program should output the major version number, and if running on two different minor versions, should report the minor version also.

    • You should not use prebuilt version functions (this includes variables that have already been evaluated at runtime).

Example output:

Perl and Ruby:

  • Perl: This program wasn't written in Perl, it was built for Ruby!

  • Ruby: This program wasn't written in Ruby, it was built for Perl!

Python and C:

  • Python: This program wasn't written in Python, it was built for C!

  • C: This program wasn't written in C, it was built for Python!

Python 2 and Python 3:

  • Python 2: This program wasn't written in Python 2, it was built for Python 3!

  • Python 3: This program wasn't written in Python 3, it was built for Python 2!

Python 2.4 and Python 2.7:

  • Python 2.4: This program wasn't written in Python 2.4, it was built for Python 2.7!

  • Python 2.7: This program wasn't written in Python 2.7, it was built for Python 2.4!

This is code golf so the shortest code in bytes wins.

\$\endgroup\$
  • 2
    \$\begingroup\$ "Two different versions of the same language count as different languages." So tricking like with C pre and past 99 comments is valid? easy ^^ \$\endgroup\$ – Zaibis Sep 3 '15 at 13:54
  • 1
    \$\begingroup\$ Added it, it feels paradoxical but fits to your rules. \$\endgroup\$ – Zaibis Sep 3 '15 at 14:48
  • 1
    \$\begingroup\$ No one's doing a Whitespace/Python polyglot? \$\endgroup\$ – Not that Charles Sep 4 '15 at 15:22
  • \$\begingroup\$ nevermind. Saw the 23 implementation \$\endgroup\$ – Not that Charles Sep 4 '15 at 15:26
  • \$\begingroup\$ Does relying on the program to be run with a certain interpreter count as input? that is, switching on argv[0]? \$\endgroup\$ – cat Oct 28 '16 at 12:10

111 Answers 111

1
\$\begingroup\$

C/Java, 277 bytes

//\u000apublic class Main{static String s="This program wasn't written in Java, it was built for C!";public static void
main(//\u000aString[] a
)
{printf("This program wasn't written in C, it was built for Java!");}
//\u000astatic void printf(String a){System.out.println(s);}}
\$\endgroup\$
1
\$\begingroup\$

Python / CJam, 144 bytes.

Not gonna win this, but it's interesting.

"""This program wasn't written in CJam, it was built for Python!" """;print('This program wasn\'t written in Python, it was built for CJam!')#";

To Python:

"""This program wasn't written in CJam, it was built for Python!" """

A multi-line string with """ delimiters. This is evaluated and the result is discarded.

;

End a statement and begin a new one.

print('...')

Print the message.

#";

A single-line comment.

To CJam:

""

Push an empty string to the stack.

"..."

Push the message to the stack.

 ""

The leading space does nothing, and "" pushes the empty string to the stack.

";print('This program wasn\'t written in Python, it was built for CJam!')#"

Push another string literal.

;

Delete that string literal. Now the stack contains "" "message" "". The stack items are concatenated together and printed.

\$\endgroup\$
1
\$\begingroup\$

Jelly/Pyth, 104 bytes (non-competing)

_"!ylleJ rof tliub saw ti ,htyP ni nettirw t'nsaw margorp sihT".q."
“zỤƦSṾƬỤƁɗe⁻ȷ6qḣ¢ƓṆ[²İ¦¥ỴȧỊ:Ċ¹ḃ$7f»

Run it in Jelly or Pyth

\$\endgroup\$
1
\$\begingroup\$

C (gcc) / Processing.org (Java mode) 174 bytes

void setup(){//\
System.out.
printf("This program wasn't written in %s it was built for %s !",//\
"Processing","C"//\
/*
"C","Processing"//\
*/
);}//\
/*
main(){setup();}//*/
\$\endgroup\$
1
\$\begingroup\$

VBScript/JScript, 117 bytes

a="J"
b="VB"
rem=c=a;a=b;b=c
WScript.echo("This program wasn't written in "+a+"Script, it was built for "+b+"Script")
\$\endgroup\$
  • \$\begingroup\$ Clever use of the rem comment keyword \$\endgroup\$ – Taylor Scott Mar 14 at 1:30
1
\$\begingroup\$

Brainfuck/VBA (immediate), 568 bytes

?"This program wasn't written in VBA, it was built for Brainfuck!"'>--->->>>>-->>>>>>->>-->>->>>-->>-->>>>>>>>-->>>-->->>->>>>>>>-->-->>>-->>->>-->>>>>>-->>>>-->->->->-->+[-[>+++++++<-]<+++]>---->++>--->-->->+>->->-->--->->+++>-->+>>+++>->-->++>->+>>->-->->->--->-->+>-->-->+>->->+++>-->-->--->->--->-->+++>+>-->->+>>+++>->+>--->->-->-->->+>--->-->->+>++>->->+>+[-[>+++++++<-]<++++]>---->--->+++>--->>+>--->->+++>++>->+++>+>+>--->+++>>++>+>+>+>--->->--->+>+>>++>+>--->++>+>>->+++>--->++>+>++>-->->->+>--->+>+>--->+++>>+>--->++>--->>+>+>+>+++>->+>->>->++>+[-<++++]>[.>]

Try it online!

Pretty simple approach.

\$\endgroup\$
1
\$\begingroup\$

Haskell/Racket, 177 bytes

Just some simple comment play. I go to a bit of trouble to share strings.

Haskell

;p=putStr{-
#lang racket
(display(string-append;-};main=do{p
"This program wasn't written in ";p"Haskell"{-
"Racket";-};p
", it was built for ";p"Racket!\n"{-
"Haskell!\n"));-}}

Try it online!

Racket

;p=putStr{-
#lang racket
(display(string-append;-};main=do{p
"This program wasn't written in ";p"Haskell"{-
"Racket";-};p
", it was built for ";p"Racket!\n"{-
"Haskell!\n"));-}}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Java 7/Java 8: 230 bytes

class M{public static void main(String[]a){Class c=null;try{c=Class.forName("java.time.Period");}catch(Throwable t){}System.out.printf("This program wasn't written in Java %d, it was built for Java %d!",c==null?7:8,c==null?8:7);}}

Try it online

Explanation
The java.time package was added in Java 8, which means it doesn't exist in Java 7 (or older), which will cause Class.forName to throw a ClassNotFoundException. The variable c stays null, which is then checked for by the tenary statements.

\$\endgroup\$
1
\$\begingroup\$

Common Lisp / C, 208 156 150 119 bytes

;main(){puts(
//(format't"~A~2*~A~2@*~AC!"
"This program wasn't written in ""C"", it was built for ""Common Lisp""!");}

Try it online for C

Try it online for Common Lisp

-52 bytes thanks to @ceilingcat!

-37 bytes thanks to @ASCII-only and his excellent mastering of CL format function

\$\endgroup\$
  • \$\begingroup\$ Thanks a lot @ceilingcat, I did'nt know that puts could be used without include! \$\endgroup\$ – Renzo Aug 22 '17 at 13:26
  • \$\begingroup\$ You appear to have an extra slash in the codeblock \$\endgroup\$ – ASCII-only Mar 11 at 3:36
  • \$\begingroup\$ :( smart way is 158 \$\endgroup\$ – ASCII-only Mar 11 at 3:41
  • \$\begingroup\$ 150 \$\endgroup\$ – ASCII-only Mar 11 at 3:52
  • \$\begingroup\$ Wonder if there's an easier way to concatenate strings, tried format but that doesn't seem to work too well (actually... 147) \$\endgroup\$ – ASCII-only Mar 11 at 8:42
1
\$\begingroup\$

Python 3/Clojure, 121 112 bytes

Based off nihilazo's answer.

# is comment in Python, and strings appearing next to each other are concatenated together, as in C.

Clojure is a Lisp, so parentheses need to surround every function. Since Python's print needs parentheses, we add printf inside the parentheses since it's the closest to a no-op we can use. As a bonus, printf lets us choose format the arguments very flexibly.
#_ skips the next form (list () or literal, e.g. number 0). Note that this doubles as comment character in Python.
%s and %n$s are the the same as in C, resolving to the next argument and the nth argument respectively.

Python 3

#_0
print(#_0 printf"%s%4$s%3$s%s!"
"This program wasn't written in ""Clojure"
", it was built for ""Python""!")

Try it online!

Clojure

#_0
print(#_0 printf"%s%4$s%3$s%s!"
"This program wasn't written in ""Clojure"
", it was built for ""Python""!")

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 6 / Perl 5, 106 bytes

my $a=5;my @a=6;print "This program wasn't written in Perl ",11-$a[0],", it was built for Perl ",$a[0],"!"

Pretty standard fare with sigil inflection.

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ This can be 96 bytes through how Perl treats lists \$\endgroup\$ – Jo King Nov 5 at 5:18
1
\$\begingroup\$

Bash/Zsh, 86 bytes

a=({Z,Ba,Z}sh)
echo This program wasn\'t written in ${a[1]}, it was built for ${a[2]}!

Try it online! (Bash) | Try it online! (Zsh)

Bash indexes from 0, Zsh indexes from 1.

\$\endgroup\$
1
\$\begingroup\$

Haskell and ink, 160 bytes

--{i}ink{c}Haskell!->END
data K=VAR
g
 VAR i="This program wasn't written in "
f
 VAR c=", it was built for "
main=mapM putStr[g VAR 0,"Haskell",f VAR 0,"ink!"]

Try it online! (Haskell, ink)

In ink, the lines that begin with VAR are global variable declarations. Those happen at the start of the program regardless of whether those lines are ever reached during execution. This means we can end the program at the first line and not have to worry about printing anything we surround those with to make them look like valid Haskell.

In Haskell, the first line is a comment. We then define a datatype with VAR as a constructor. This lets us create functions that take VAR as a parameter, which is what lets us sneak ink's variable declarations in.

There is also a pretty boring 140-byte solution that abuses comment syntax to just concatenate two entirely separate programs (Haskell, ink)

\$\endgroup\$
1
\$\begingroup\$

Go/Java 10+, 308 bytes

Takes advantage of the var keyword introduced in Java 10. Will not run on earlier Java versions.

package main;/*\u002a\u002fpublic class Main{public static void main(String[]args){/**/var g="\u0022+";func main(){//";
g="Go";var j="Java";if(g!="Go"){var t=j;j=g;g=t;}
/*\u002a\u002fSystem.err./**/print("This program wasn't written in "+g+", it was built for "+j+"!");g="\u0022+"}//";
var w="\u0022+"//";}}

Ungolfed and commented:

// Required at the beginning of all Go programs.
// Consequently, the Java file needs to be saved in a directory called "main".
package main;
// Java substitutes unicode escape sequences like \u002a before lexing the code.
// It sees "/**/public class...", starting a main class and function.
// Go sees it as a comment.
/*\u002a\u002fpublic class Main{public static void main(String[]args){/**/

// Use another escape sequence to end a string for Java but not for Go, then
// declare Go's main function. Close the Java string with a quote hidden in a
// comment. Java sees "var g = ""+"...//";" while Go sees var g = "\"+";func...
var g = "\u0022+";func main() { //";
    // Reuse the variable g to save space
    g = "Go";
    var j = "Java";
    // The meat of detecting the language we're using.
    // Go's == compares strings by comparing their characters;
    // Java's == tests whether the strings are the same object.
    if (g == "Go") {
        var t = j;
        j = g;
        g = t;
    }
    // In Java, calls System.err.print. In Go, calls builtin "print",
    // which writes to stderr.
    /*\u002a\u002fSystem.err./**/print("This program wasn't written in " + g + ", it was built for " + j + "!");
    // Boilerplate to close Go. We can't close Java first nor let them share a
    // closing brace because Go needs and Java forbids "var" outside functions.
    g = "\u0022+"}//";
// Boilerplate to close Java.
var w = "\u0022+"  //";}}
\$\endgroup\$
1
\$\begingroup\$

Keg/><>, 246 123 103 bytes

 "This program wasn't written in ><> it was built for Keg!"r>o<ø“0⊂kB7≤1⑵“` in Keg, it` “034⊂06“` ><>!`

-23 bytes saved to using string compression methods

Try it online! (><>)

TIO will be added after Keg is updated.

Answer History

123 bytes

 "This program wasn't written in ><> it was built for Keg!"r>o<ø`This program wasn't written in Keg, it was built for ><>!`

Try it online! (Keg)

Try it online! (><>)

-123 bytes due to @JoKing pointing out that I clearly haven't used ><> for a while. ;)

246 bytes

 "This program wasn't written in ><> it was built for Keg!"rvø`This program wasn't written in Keg, it was built for ><>!`#
#                                                     vo;!?l<
#                                                     >     ^

Try it online! (Keg)

Try it online! (><>)

Apparently, Keg polyglots well with 2d languages. Probably because strings aren't delimited with "'s but backticks rather.

\$\endgroup\$
  • \$\begingroup\$ You can also push it in reverse before printing to save on the r instruction Try it online! \$\endgroup\$ – Jo King 2 days ago
0
\$\begingroup\$

JavaScript ES6 / Python2, 98 bytes, REPL ONLY

js = 'python'
python = 'js'
s = 'this program is not ' + `js` + ', it was built for ' + `python`
s

Tested with Python 2.7.10 and Chrome 52.

The caveat here is actually printing the output....Python uses print s and JS uses console.log(js). So instead I assume you are using a REPL and that s alone will print to stdout.

\$\endgroup\$
  • \$\begingroup\$ this doesn't actually print anything. I know Python and Javascript have different comment syntaxes \$\endgroup\$ – Blue Sep 1 '16 at 17:15
  • \$\begingroup\$ @muddyfish Updated to print via REPL \$\endgroup\$ – styfle Sep 1 '16 at 17:16
  • \$\begingroup\$ @muddyfish Additionally I wanted to see if it was possible to do it without comments. \$\endgroup\$ – styfle Sep 1 '16 at 17:16
  • \$\begingroup\$ Also you're not printing out the entire required string but I agree that not using comments makes it interesting \$\endgroup\$ – Blue Sep 1 '16 at 17:18
  • \$\begingroup\$ @muddyfish I added another answer below because it is an entirely different approach using python3 instead of python2. \$\endgroup\$ – styfle Sep 8 '16 at 5:29
0
\$\begingroup\$

AWK/C++ (gcc), 201 bytes

#include <cstdio>
/*#\/* /
BEGIN{
#*/int main(){const char *a[2];
a[0]="AWK";a[1]="C++";
/*#\/* / 
b=0
#*/int b=1;
printf("This program wasn't written in %s, it was built for %s!\n",a[b], a[(b+1)%2]);}

Try it online - AWK!

Try it online - C++(gcc)!

I suppose this is essentially and AWK/C, but I tested it with C++ and am using #include <cstdio> so that makes it C++ right? :)

\$\endgroup\$
0
\$\begingroup\$

Yabasic / VBA, 130 bytes

n=-8*(Now>0):?"This program wasn't written in "+Mid$("YabasicVBA",n,8)+", it was built for "+Trim$(Mid$("VBA    Yabasic",n,8))+"!"

Explaination

n=-8*(Now>0)                            ''  Key operation: Differentiates Between VBA and Yabasic
                                        ''  Now is defined in VBA with the system time, but not in
                                        ''  Yabasic; Evaluates to 0 in Yabasic, and 8 in VBA
?"This program wasn't written in "      ''  Print beginning of sentance
  +Mid$("YabasicVBA",n,8);              ''  Print current language
  +", it was built for "                ''  Print middle
  +Trim$(Mid$("VBA    Yabasic",n,8))    ''  Print other language
  +"!"                                  ''  Print Exclaimation mark

Try it online! (Yabasic)

\$\endgroup\$
0
\$\begingroup\$

Racket/Standard ML, 166 bytes

;val p=print(*
#lang racket
(display(string-append;*);p
"This program wasn't written in ";p"SML"(*
"Racket"; *);p
", it was built for ";p"Racket!\n";(*
"SML!\n")); *)

Try it online!

This is a port of my Racket/Haskell solution to ML.

\$\endgroup\$
0
\$\begingroup\$

Aheui/Python3

'''
밣밙반따따밥다맣밙밚밤따따밥다맣밙밚밤따따밙다맣밙밚밤따따밙반따밙다다맣받밙반따따박다맣밙밚밤따따밙반따박다다맣밙밚밤따따밙반따밥다다맣밙밚밤따따밙반따반밧나다다맣밙밚밤따따받다맣밙밚밤따따밙반따밥다다맣밞밙반따따밠다맣밙밚밤따따밞다맣받밙반따따박다맣밙밚밤따따밙반따밞다다맣밞밙반따따밠다맣밙밚밤따따밙반따밙다다맣밙밚밤따따밙반따다맣받밙반따따밞다맣밙밚밤따따밙반따밦다다맣받밙반따따박다맣밙밚밤따따밙반따밞다다맣밙밚밤따따밙반따밥다다맣밙밚밤따따밙다맣밙밚밤따따밙반따밦다다맣밙밚밤따따밙반따밦다다맣밙밚밤따따반밧나다맣밙밚밤따따밙반따다맣받밙반따따박다맣밙밚밤따따밙다맣밙밚밤따따밙반따다맣받밙반따따박다맣밙밙밚밙밚밥밤따따따따따따밙밙밚밤따따따다맣밙밙밚밙밚밥밤따따따따따따밙밙밚밙밣따따따따밙밚밤따따밥밙반따따밣다다다다맣밥밙반따따밥다맣받밙반따따박다맣밙밚밤따따밙다맣밙밚밤따따밙반따밦다다맣받밙반따따박다맣밙밚밤따따밙반따밞다다맣밞밙반따따밠다맣밙밚밤따따밙반따밙다다맣받밙반따따박다맣밞밙반따따밣다맣밙밚밤따따밙반따밠다다맣밙밚밤따따밙다맣밙밚밤따따밣다맣밙밚밤따따밙반따밦다다맣받밙반따따박다맣밙밚밤따따박다맣밙밚밤따따밙반따반밧나다다맣밙밚밤따따밙반따밥다다맣받밙반따따박다맣밣밙반따따맣밙밚밤따따박밙반따따반밧나다다맣밙밚밤따따밙반따밦다다맣밙밚밤따따밥다맣밙밚밤따따밙반따반밧나다다맣밙밚밤따따밙반따다맣받밙반따따받다맣희
'''
print("This program wan't written in Python, it was built for 아희!")

Aheui code generated by https://apteryx.moe/straheui/.

Aheui code is in comment of python, while aheui dismiss every non-Korean characters.

\$\endgroup\$
-1
\$\begingroup\$

brainfuck/C 749 bytes

//-[--->+<]>-.[---->+++++<]>-.+.++++++++++.+[---->+<]>+++.[-->+++++++<]>.++.---.--------.+++++++++++.+++[->+++<]>++.++++++++++++.[->+++++<]>-.--[->++++<]>-.-[->+++<]>-.--[--->+<]>--.-----.[++>---<]>++.[->+++<]>-.[---->+<]>+++.--[->++++<]>-.-----.---------.+++++++++++..+++[->+++<]>.+++++++++.-[->+++++<]>-.-[--->++<]>-.+++++.-[->+++++<]>-.[->+++<]>++.[--->+<]>----.+++[->+++<]>++.++++++++.+++++.--------.-[--->+<]>--.+[->+++<]>+.++++++++.-[++>---<]>+.-[--->++<]>-.+++++++++++.[---->+<]>+++.--[->++++<]>-.-[->+++<]>-.--[--->+<]>--.+[---->+<]>+++.[->+++<]>++.[--->+<]>-.------------.+++.++++++++.[---->+<]>+++.++[->+++<]>.+++++++++.+++.[-->+++++<]>+++.+[->++<]>+.-[-->+<]>.
main(){puts("This program wasn't written in C it was built for brainfuck!");}
\$\endgroup\$
  • 2
    \$\begingroup\$ You need the comma in the output string. \$\endgroup\$ – Mego Oct 28 '16 at 2:10

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.