Bash, C, C++, Obj-C, Obj-C++, Perl, PHP, Ruby, 183 chars
score ~ 0.1236
For the C and C-like codes I owe a debt to @baby-rabbit. The others are inspired by the recognition that many languages have an eval
statement that will grudgingly accept invalid syntax.
Outputs the language name to standard output. Sometimes generates a lot of error messages on standard error, so suppress them with 2>/dev/null
as you run them.
#if 0
'PHP<?/*';eval "print\$=?'Perl':'Ruby';echo Bash";exit
__END__
#endif
#include <stdio.h>
main(){puts(
#ifdef __OBJC__
"obj-"
#endif
"C"
#ifdef __cplusplus
"++"
#endif
);}//*/?>'
The php solution outputs 'PHP'
(including the single quotes), which may be bending the rules a little bit.
Last edit: shaved 12 chars from insight that $=
is false
in Ruby, 60
in Perl, and print$=?...
is almost surely an error in Bash. Shaved 7 more from insight the Perl/Ruby/Bash test can now go into a single eval statement.
If the rules can tolerate more bending, I present this 8 language, 43 character solution (score 0.5262)
print("phperluarscriptrubypythoncatebg13");
for which the output includes the name of the interpreter for php
, perl
, lua
, rscript
, ruby
, python
, cat
, and rot13
.
Would using a solution like using
print()
for Perl, Python, PHP, Processing and R be considered trivial or is it acceptable? – Rob – 2012-09-10T23:34:51.7273A can't see a problem with it. The difficulty would be switching print's input per language. – mjgpy3 – 2012-09-11T02:57:56.867
I should have given a higher weight to the number of languages... it seems choosing to have exactly 5 is the most optimal. I wonder how many languages would have been in the winner entry if the score were
n * n / length
– vsz – 2012-09-11T18:47:41.107I was going to suggest that too. Based on the answers provided, I don't believe I interpreted your question properly. – Rob – 2012-09-11T23:10:17.603