Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

The challenge
You are given a function compile(s: String): Integer which returns 0 if s could be compiled and anything other than 0 if it failed (the resulting code is not important).

The thing is that you don't know for what language the function is made for, so it is up to you to figure that out by giving the function small snippets of code and observing what it returns. The good part is that a team of scientists have tried before you and have narrowed down the list to C, C++, C#, Java and JavaScript (these are the only widespread languages which have similar syntax I could think of).

Rules

  • General code-golf rules apply.
  • There is no strict output format; as long as it's readable and understandable it's ok.
  • If these 5 languages are not enough for you then you can add your own favourites and you'll be rewarded with extra stars (★) (that do nothing).

Note1: the compiler is up to date with the latest standard of whatever language it's designed for.
Note2: I know JavaScript does not compile, but it goes though some parsing process; if it's valid JavaScript code then it passes

share|improve this question
1  
What are we to provide? The set of strings that can be used to tell the compilers apart, minimizing the total length of the strings? –  Geoff Reedy Jan 11 '13 at 3:50
    
@GeoffReedy I was looking just for the code to determine the language, but the explanations you provided are more than welcome –  madflame991 Jan 11 '13 at 7:23

1 Answer 1

Total snippet length 8; test program 83 (C)

Distinguish JavaScript and C from C++, Java, and C#

a;

Distinguish JavaScript from C

0

Distinguish C++ from C# and Java

#

Distinguish C# and Java

[:a]

C code to determine the language

#define C(x)compile(#x)?
main(){puts(C(a;)C(#)C([:a])"J":"C#":"C+":C(0)"C":"JS");}

Snippets tested with

C: gcc 4.7.2 with -std=c99
C++: g++ 4.7.2 with -std=c++11
Java: javac 1.7.0_09
JavaScript: JavaScript-C 1.8.5
C#: Mono C# compiler version 2.10.8.0

share|improve this answer
    
#define C(x)compile(#x)? saves a space and lots of quotes. –  ugoren Jan 11 '13 at 13:46
    
@ugoren thanks, I actually thought I tried the stringification thing but it didn't work for some reason; I must have had some other problem too, cause it works fine now –  Geoff Reedy Jan 11 '13 at 13:58
    
But you somehow got it mixed up - C(#x) won't work. –  ugoren Jan 11 '13 at 19:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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