8
\$\begingroup\$

Pig-latin is a made-up language that is translated into by moving the first letter of a word to the end and adding ay to the end. For example: hello translates into pig-latin as ellohay. Therefore, translating pig-latin back into English is dropping the ay at the end and moving the last letter to the start (like oodbyegay to goodbye). When writing it out, some people will put a - at the end before moving the first letter for clarity, so it reads (for example) ello-hay instead of ellohay for hello. We will use this hyphenated version for this challenge.

Objective

Undo a pig-latin string.

Instructions

Write a program/function that takes one argument, a single word in the form of a string. If that word is in pig-latin, the program/function must output (i.e. return or print it) the non-pig-latin word. If it isn't pig-latin, output the word as-is.

Pig-latin for this challenge will be in the following format:

ello-hay

If the word ends in -*ay, it is pig-latin, where the * is a single wildcard character.

Non-letter characters should be treated as a normal letter. For multiple dashes, ignore all except the last.

Examples

Using fictitious unpig() Python function for demo.

unpig("ello-hay")    #=> hello
unpig("hello-world") #=> hello-world
unpig("hello")       #=> hello
unpig("pple-aay")    #=> apple
unpig("apple")       #=> apple
unpig("ig-Stay")     #=> ig-Stay (+1 to ETHproductions for noticing.)
unpig("ello----hay") #=> hello---
unpig("la$s-gay")    #=> gla$s
unpig("ig-pay-yay")  #=> yig-pay

Rules

  1. Standard loopholes apply
  2. This is , so shortest code wins.
  3. Built-in functions (specifically for this purpose) are not permitted.
  4. Use header # Language, XX bytes

Leaderboard

var QUESTION_ID=127963,OVERRIDE_USER=46066;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\$

10 Answers 10

5
\$\begingroup\$

JavaScript (ES6), 34 bytes

s=>s.replace(/(.*)-(.)ay$/,'$2$1')

Test cases

let unpig =

s=>s.replace(/(.*)-(.)ay$/,'$2$1')

console.log(unpig("ello-hay"))    // => hello
console.log(unpig("hello-world")) // => hello-world
console.log(unpig("hello"))       // => hello
console.log(unpig("pple-aay"))    // => apple
console.log(unpig("apple"))       // => apple
console.log(unpig("ig-Stay"))     // => ig-Stay

\$\endgroup\$
  • \$\begingroup\$ Is this a function? I don't do JS (ES6). \$\endgroup\$ – juniorRubyist Jun 23 '17 at 0:06
  • \$\begingroup\$ I literally had the exact same solution, except using double quotes instead of single quotes \$\endgroup\$ – ETHproductions Jun 23 '17 at 0:06
  • \$\begingroup\$ Nvm. Great start! \$\endgroup\$ – juniorRubyist Jun 23 '17 at 0:07
  • \$\begingroup\$ @juniorRubyist Its an anonymous function that can be assigned to any variable(in this case 'unpig'). \$\endgroup\$ – officialaimm Jun 23 '17 at 3:13
  • \$\begingroup\$ Seems pretty unbeatable. \$\endgroup\$ – Steve Bennett Jun 23 '17 at 4:05
3
\$\begingroup\$

PHP, 61 bytes

function f($v){echo preg_replace("#(.*)-(.)ay$#","$2$1",$v);}

Try it online!

PHP, 46 bytes

without a function

<?=preg_replace("#(.*)-(.)ay$#","$2$1",$argn);

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Must be a function. Invalid. \$\endgroup\$ – CalculatorFeline Jun 23 '17 at 16:35
  • \$\begingroup\$ @CalculatorFeline fixed \$\endgroup\$ – Jörg Hülsermann Jun 23 '17 at 16:44
  • \$\begingroup\$ Actually, 22 minutes ago, your 46 byte solution became valid. \$\endgroup\$ – CalculatorFeline Jun 23 '17 at 16:53
3
\$\begingroup\$

Python 2, 48 46 bytes

-2 bytes thanks to xnor.

These are anonymous lambda functions that can be assigned to, say unpig.

lambda s:[s,s[-3]+s[:-4]][s[-4]+s[-2:]=="-ay"]

Try it online!

Regex solution, 49 bytes

lambda s:re.sub('(.*)-(.)ay',r'\2\1',s)
import re

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ You don't need to use and/or when short-circuiting isn't required. Selecting from a list works and is shorter. \$\endgroup\$ – xnor Jun 23 '17 at 16:53
2
\$\begingroup\$

Python 2.7, 68 55 52 bytes

def up(s):print(s,s[-3]+s[:-4])[s[-4]+s[-2:]=="-ay"]

Uses a ternary-like operator.

-13 bytes thanks to officialaimm and an update in rules.

-3 Changed ternary operator. See https://stackoverflow.com/a/470376/3210045.

\$\endgroup\$
  • 1
    \$\begingroup\$ s[0:len(s)-4] can be replaced by just s[:-4]. And few spaces can be avoided by squeezing ]if and "else \$\endgroup\$ – officialaimm Jun 23 '17 at 3:03
1
\$\begingroup\$

05AB1E, 15 bytes

D'-¡θ¦„ayQi¨¨Á¨

Try it online!

θ is replaced by ®è on TIO since it hasn't been pulled there yet.

\$\endgroup\$
  • \$\begingroup\$ Best golfing! You win. \$\endgroup\$ – juniorRubyist Jun 26 '17 at 2:20
0
\$\begingroup\$

Japt, 18 bytes

Just the same RegEx replacement as everyone else, first posted by Arnauld

r"(.*)-(.)ay""$2$1

Test it

\$\endgroup\$
  • \$\begingroup\$ I had q$/-(.)ay/$ w q for 15 but unfortunately that fails on inputs containing multiple copies of -<char>ay (it corrects them all rather than just the last). Oh well... \$\endgroup\$ – ETHproductions Jun 25 '17 at 4:15
0
\$\begingroup\$

Jelly, 22 17 bytes

ṖṖṙ-Ṗµṣ”-ṪḊ⁼⁾ayµ¡

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 22 bytes

{S/(.+)\-(.)ay$/$1$0/}

Basic regular expression solution.

\$\endgroup\$
0
\$\begingroup\$

Python 3, 59 60 bytes

  • 1 byte added thanks to @Erik the Outgolfer: $ to ensure ig-payyay outputs the same and not yig-pa
lambda a:a[-3]+a[:-4]if re.search("-.ay$",a)else a
import re

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java (OpenJDK 8), 146 bytes

s->{Matcher m=Pattern.compile("(.*)-(.)ay").matcher(s);String g="";while(m.find())g=m.group(1);return (g!=""?s.charAt(s.lastIndexOf('-')+1)+g:s);}

Try it online!

\$\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.