Some time ago, the following question was asked: GolfScript, CJam, or Pyth? Based on the title only, I thought that it would be a very nice challenge, but unfortunately, it turned out to be a question asking for tips. Here is the challenge I wanted to read:
Who said golfing languages were not used in the Real World? As everybody knows, the ratio of bugs per line of code is the same independently of the programming language being used, so there is a clear opportunity to reduce debugging and maintenance costs with those languages. Your company finally saw the light and decided to use Golfscript, CJam and Pyth to develop its products.
Unfortunately, after a couple of months, your hard drives are cluttered with snippets of code and you don't even know which ones are written in which languages (you even suspect some of your programmers of using Perl).
You must write a tool to detect which of CJam, Golfscript or Pyth is the language in which a program is written. The situation is critical, you can use any language you want, but please keep it short (data storage is expensive: the more bytes we use, the more it costs).
General
- Shortest code win
- Standard loopholes, etc.
- Don't use an online interpreter
- You can write a function or a program
- You can use eval to detect your own language
Input
- Your input is taken from the standard input stream or as a string
- Input contains only ASCII printable characters and linefeeds
- The size of the input is up to 256 bytes long
Output
- Output is printed to output stream or returned as a sequence of strings/symbols
If the input is without doubt a valid X program, print or return X, X ∈ {CJam, Pyth, Golfscript}
Remark: "without a doubt" does not mean that you are allowed to answer with a dumb analyzer that consistently fails to detect any language. For Pyth, I expect Simple Programs to be recognized (but no hardcoding). The same goes for CJam (cheat sheet, examples) and Golfscript (examples). The previous links point to each language's specifications. If you use a fuzzy/bayesian approach, "without a doubt" means with a high-level of confidence (you score 99% with your classification, for example). See below for the actual test suite.
If the input is valid in multiple languages, each detected language should be printed/returned. When printed, there must be a separator between multiple outputs (e.g. space, newline, comma...).
- The order in which languages are tried does not matter
- I do not care about case (CJam, cjam, CJAM, SPAM) [1]
- If none of the above language is detected, print "
Probably Perl
". Thanks to this comment from mbomb007, in the above case you can also output "Seriously
" for a penalty of 4 bytes (the difference between both strings).
[1] Just to be clear, SPAM is invalid
Examples
-
"Crime predicted: -- Calling: 1--555- "30*{_5<{iAa*:mr}&}/
Output (example of multiple return values)
["Golfscript", "Cjam"]
Input
3
Output (example on standard output)
golfscript cjam pyth
Input
if {} aazd
Output
Probably Perl
In the last case, the input program produces an error With All Three online interpreters.
Scoring, winning criterion
Shortest code win. Add 4 bytes if you output "Seriously". Then, apply bonuses.
Failing tests
The following are snippets that must not be recognized as any of the three languages above.
One-liners (i.e. one entry per line)$^X=~/([a-z]+)[^\/]+$/;print$1,$/
<>;map($s-=(-1)**$_/(2*$_-1),1..$_),$s=!print$s,$/for<>
((THIS IS LISP HAIKU) (TRULY THIS IS LISP HAIKU) (THIS IS LISP HAIKU))
(format t"~@(~{~R~^, ~}~).~%~:*~@(~{~:R~^, ~}~)."(loop for i to 99 collect(1+ i)))
print sum(ord(c) for c in 'Happy new year to you!')
Brainfuck
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
++++++++++++++++++++++++++++++++++++++++++++++++.
-----------------.
++++++++.
+++++.
--------.
+++++++++++++++.
------------------.
++++++++.
Perl
@list=( "aaaa",
"aaaaaaaa",
"aaaaaaaaaaaaaaa",
"aaaaaaaaaaaaaaaa",
"aaaaaaaaaaaaaaaaaaaaaaa",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
while (@list) {
for($i=0;$i<6;$i++){
print length($list[$i])." ";
}
print "\n";
}
Fish
#ifndef __linux
#include "x.h"
#define X/*\
a=1 set -e
+++++++++++++++++++++_+++++++++++++....Hello World!?:Q:
#endif
echo "Hello, World!"
int m(){}
Basic test suite
Those are tests which must pass. Belows are one-liners for each language where your program must detect the language it is being written in (I did not check which of those are polyglot).
Pyth
*2Q
FNrZTN
FNrZhTN
FNUhTN
VhTN
FNr1hQN
=N5N
K5K
K1FNr1hQ=K*KN
K1FNr1hQ=K*KNK
K1FNr1hQ=K*KN;K
DhZK*3ZRK
L?b*bytb1yQ
A(Z1)VQHA(H+HG
Golfscript
;'2706 410'~{.@\%.}do;
;''6666,-2%{2+.2/@*\/10.3??2*+}*`50<~\;
'Hello, world!'
1 2 [\]
CJam
"Hello, world"
{`"_~"}_~
"`_~"`_~
T1{_2$+}A*]`
{__'`>\'x>26*2-*-}/
Y38#
N/s:X,8-,{X>9<__{'a<},,\4%{'Z>},,*I={4=}{;}?}/
Silver bonus: byte-count * 0.6
All previous tests must pass, as well as the following one-liners. All those snippets are taken from actual CodeGolf answers.
Pyth
VzJ:zZhZpkJ~Zhy}rJ0-G"aeoui
Vzjdm?@zd}N,dt-lzd\ Uz
jd.iSQs*RtQ,\?":0
rsXOtQmO*-GJ"aeiou"J/Q2*%Q2O"hy"4
VhQIq`N_`NN
s["$$\\varphi=1+"*Q"\cfrac1{1+"\\<\dQ"dots"*Q\}"$$
@c"weak trick fair"d-!JlfhT-M.:us_cG.u+NYtKrH7-52hK.zU52 2>J26
Golfscript
);:|;{0):0;|$:§-1%" - "§" = ""0"4$~§~-+-4>:|n|6174`=!}do"Iterations: "0"."
'-+,/'{)))))}%
4:echo(2+2);
#undef X;A!"$%&'()*+-[,.]/0123456789:<=>?@BCDEFGHIJKLMNOPQRSTUVWYZ\^_`abcghijklmopqrstvwxyz{|}~
{`),32>^.}.~
"126,32>''+".~\-'.~\-"'-
"),@`^^32>#.~".~
...[[]]{{}}&%%++++5i
*++..0011125::::;;;?bbbbcccc{}
"This program wasn't written in "o"GolfScript"", it was built for ""CJam"oo"!"
CJam
"Q"c("ASSW"1$("aRD"(((T1
%\@_@){;_0}*__*)\15
"This program wasn't written in "o"GolfScript"", it was built for ""CJam"oo"!"
"P2"1e3K51_,1>K*$K*~]N*
li__,\mf:i2m1+:*/fb:+
ri:B__(^2/):G/,{_BBG/@-(#G@*G(B2/*+*}/]:+
{1$+S@]_1=4+1$`,-S*"2$~"}21D2$~
[S'-26*N]:Z[S'|:PS24*PN]:RR'(PS5*qi:XD=X0<-X2%2*+:Y[" ^ "_" > ""(O)"" - "__]=S8*Y[" ^ "" "" < ""(O)"" - "__]=S5*P')NRRXD=[SPS7*'oA*S7*PN]:QR?Y[[SPS5*'oSC*'oS5*PN]:T_R[SPS7*'oS8*'oS7*PN]RRR]=QY2=TR?RRZ
li4H#+2bW%32<2b
q~:R100:H*\d:T/i){R-H*HT-/m]}6*_H)<*
"JamesBdo,"YZ+/)BA*c+Y*Y%:BS@SB)))[JW:Z____)ci+*]U*
Gold bonus: previous-score * 0.8
Pyth
Comparejdm@cd)._-FQcj"
is
equal greater less
to than
"Qb
Snowman
M@GCHgc" ___
___
_"bhzgc" (_*_)
_===_
.....
/_\\"bhzs[g" \ "@z4\(g"-.oO"@z2g" ,._"@z1g"-.oO"@z3\)g" / "@z5)s[g" < /"@z4\(gc"
:
] [
> <"b@z6\)g" > \\"@z5)++" ("gc"
:
\" \"
___"bez\)
CJam
Bigrri:Hri:Vri:Q[q~]3/_Qa3*a+_|$W%:Pf{\a#}:AH/:B0ff*
P,,[AHAW%HBz:+_W%V\V]2/
ff{~@@f=/::|1#}0Ua4*t:R;
P0f<
V{H{BI=J=_2$=
0R{"I>! I+V<J>! J+H<"4/+4/z{~~}%:&1$*\)}%);2$-|t
}fJ}fI
[P,{_La#\1$0t1$f-}*;;]
{:TR=2/~\~V\-,>\f{\_3$=@~H\-,>{Tt}/t}~}/
:~Pf=:~
~]S*N
Snowman
q:Q;SS"
_===_,___
....., _
/_\,___
(_*_)"',/0{Q=~(=}:G~N" \ "4G'(".oO-"_2G",._ "1G@3G')" / "5GN"< / "4G'(" : ] [> < "3/6G')"> \ "5GNS'(" : \" \"___ "3/7G')
Golfscript
Lorem IpsumLorem ipsum dolor sit amet, consectetur adipiscing elit. Cras imperdiet est ut sem commodo scelerisque. Sed ut ultricies enim. Nam eget lectus suscipit, gravida turpis a, volutpat tellus. Cras efficitur luctus neque, at semper massa condimentum at posuere.
Digital clock
:*{32' _':$@'14'{?~!=}:&~32}%n*{:x' |':|\'1237'&$x'017'&|x'56'&}%n*{:x|\'134579'&$x'147'&|x'2'&}%
Happy birthday
4,{"Happy Birthday "["To You""Dear GolfScript"]@2==n}%
Farey sequence
~:c[,{){.}c(*}%.c/zip{+}*]zip{~{.@\%.}do;1=},{~<},{~\10c?*\/}${'/'*}%', '*'F'c`+' = {0/1, '+\', 1/1}'