Levenshtein distance bias

PHP, 137 135

Requires error reporting off and PHP 5.4+. Input is GET variable a.

<?=explode(~ß,~­ß¼Üß¼ÔÔßµž‰žß¬œž“žß¾Š‹¶‹ß¾‘“›¼ß«¶Ò½¾¬¶¼ß½Š“š˜Š’ßµž‰žŒœ–‹ß²ž‹—š’ž‹–œžß¾œ‹–‘¬œ–‹)[levenshtein(PHP,$_GET[a])-1];

Hexdump:

00000000: 3C 3F 3D 65 78 70 6C 6F - 64 65 28 7E DF 2C 7E AD |<?=explode(~ ,~ |
00000010: DF BC DC DF BC D4 D4 DF - B5 9E 89 9E DF AC 9C 9E |                |
00000020: 93 9E DF BE 8A 8B 90 B6 - 8B DF BE 8D 91 90 93 9B |                |
00000030: BC DF AB B6 D2 BD BE AC - B6 BC DF BD 8A 9D 9D 93 |                |
00000040: 9A 98 8A 92 DF B5 9E 89 - 9E 8C 9C 8D 96 8F 8B DF |                |
00000050: B2 9E 8B 97 9A 92 9E 8B - 96 9C 9E DF BE 9C 8B 96 |                |
00000060: 90 91 AC 9C 8D 96 8F 8B - 29 5B 6C 65 76 65 6E 73 |        )[levens|
00000070: 68 74 65 69 6E 28 50 48 - 50 2C 24 5F 47 45 54 5B |htein(PHP,$_GET[|
00000080: 61 5D 29 2D 31 5D 3B    -                         |a])-1];|
00000087;

Readable version:

<?=explode(' ','R C# C++ Java Scala AutoIt ArnoldC TI-BASIC Bubblegum Javascript Mathematica ActionScript')[levenshtein(PHP,$_GET[a])-1];

C, 183

main(s){char* a[12]={"R","C#","C++","Java","COBOL","Python","Clipper","VBScript","Smalltalk","Javascript","Mathematica","ActionScript"};printf(a[strlen(gets(&s))-!!strchr(&s,67)-1]);}

Picking a language with a one character name lets you cheat with the distance calculation: any string's distance from "C" is just the length of the string, minus one if it includes "C". I imagine R or J could beat this using the same strategy.


Perl 5, 325 276

Using a bit of recursion to calculate the Levenshtein distance.

@X=(P,e,r,l);$y=@Y=split//,pop;sub L{my($n,$m)=@_;return$m,if!$n;return$n,if!$m;my$c=$X[$n]eq$Y[$m]?0:1;(sort{$a<=>$b}(L($m-1,$n)+1,L($m,$n-1)+1,L($m-1,$n-1)+$c))[0]}print qw(C C# C++ Java COBOL Python Clipper VBScript Smalltalk Javascript Mathematica ActionScript)[L(4,$y)-1]

My original version had some issues with the longer inputs.
Till I realised that the Perl sort function sorts alphabetically.

Using substrings instead of arrays turns out to make it slightly longer.

@L=qw(C C# C++ Java COBOL Python Clipper VBScript Smalltalk Javascript Mathematica ActionScript);sub l{my($s,$t)=@_;return length($t)if!$s;return length($s)if!$t;my($u,$v)=(substr($s,1),substr($t,1));substr($s,0,1)eq substr($t,0,1)?l($u,$v):(sort{$a<=>$b}(l($u,$v),l($s,$v),l($u,$t)))[0]+1}print$L[l('Perl',pop)-1]

Test

$ perl levenshtein.pl Php
C++