10
\$\begingroup\$

Write a program in a language with name A that, given a string S, outputs the name of a different programming language B. The length of B must be equal to the Levenshtein distance (abbr. "LD") between A and S. To be considered a programming language, B must have an esolangs page or a Wikipedia page.

Example:

(Suppose the program is coded in Python)
Input: Jython
Output: R
Input: Pyt4oq
Output: Go
Input: Rs7hon
Output: C++

The LD between Python and Jython is one, so the output R

You only need to cover up to an LD of up to 12. You can assume that an LD of 0 will never occur.

This is , so the shortest program in bytes wins.

(This is my first challenge! Feedback is appreciated)


Leaderboard

var QUESTION_ID=58974,OVERRIDE_USER=20260;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
  • \$\begingroup\$ Why 12? Are there not languages with larger names? \$\endgroup\$ – Conor O'Brien Sep 29 '15 at 2:26
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ It just seemed liek a reasonabl upperbnd. \$\endgroup\$ – Seadrus Sep 29 '15 at 2:27
  • \$\begingroup\$ @ThomasKwa I believe R is a programming language... it could have easily been C, D, etc. \$\endgroup\$ – Seadrus Sep 29 '15 at 2:35
  • 1
    \$\begingroup\$ so, you mean a name with a length equal to the Levenshtein distance? What counts as a programming language? Must it have an esolangs/Wikipedia page? \$\endgroup\$ – lirtosiast Sep 29 '15 at 2:38
  • 6
    \$\begingroup\$ The LD of "Patton" and "Python" is 2, not 3. \$\endgroup\$ – Leif Willerts Sep 29 '15 at 11:10
3
\$\begingroup\$

O, 107 bytes

Using the tip from @histocrat to make the distance calculation simpler. Also used languages with The same starting letter to remove a few characters

"pyrodecimal""hakespeare""tackstack""nowflake""nowball""nowman""onata""ADOL""taq""oT""R"""ie\'O<-1-{;}d'Soo

Try it here

\$\endgroup\$
6
\$\begingroup\$

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];
\$\endgroup\$
6
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ How come gets(&s) doesn't crash? \$\endgroup\$ – anatolyg Sep 30 '15 at 9:13
  • \$\begingroup\$ I'm not sure, it works on Anarchy Golf and ideone but is clearly not really cricket. I imagine it's using the unused memory allocated for ARGV. \$\endgroup\$ – histocrat Sep 30 '15 at 13:12
5
\$\begingroup\$

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++
\$\endgroup\$
3
\$\begingroup\$

J, 115 bytes

{&((}.&'YABC'&.>|.i.4),(g'SMITHb'),'Clojure';(g'ComeFrom2'),((g=:}:;])'StackStacks'),<'CoffeeScript')@<:@(#-'J'&e.)

This is using histocrat's calculation trick (using a 1-letter language), and generates the following list of languages:

┌─┬──┬───┬────┬─────┬──────┬───────┬────────┬─────────┬──────────┬───────────┬────────────┐
│C│BC│ABC│YABC│SMITH│SMITHb│Clojure│ComeFrom│ComeFrom2│StackStack│StackStacks│CoffeeScript│
└─┴──┴───┴────┴─────┴──────┴───────┴────────┴─────────┴──────────┴───────────┴────────────┘

e.g.:

   {&((}.&'YABC'&.>|.i.4),(g'SMITHb'),'Clojure';(g'ComeFrom2'),((g=:}:;])'StackStacks'),<'CoffeeScript')@<:@(#-'J'&e.) 'C++'
┌───┐
│ABC│
└───┘
   {&((}.&'YABC'&.>|.i.4),(g'SMITHb'),'Clojure';(g'ComeFrom2'),((g=:}:;])'StackStacks'),<'CoffeeScript')@<:@(#-'J'&e.) 'ActionScript'
┌────────────┐
│CoffeeScript│
└────────────┘
   f=:{&((}.&'YABC'&.>|.i.4),(g'SMITHb'),'Clojure';(g'ComeFrom2'),((g=:}:;])'StackStacks'),<'CoffeeScript')@<:@(#-'J'&e.)
   f 'Jython'
┌─────┐
│SMITH│
└─────┘
   f 'Python'
┌──────┐
│SMITHb│
└──────┘
\$\endgroup\$

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.