9
\$\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\$
0

13 Answers 13

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\$
5
  • \$\begingroup\$ Is this a function? I don't do JS (ES6). \$\endgroup\$
    – Joseph
    Jun 23, 2017 at 0:06
  • \$\begingroup\$ I literally had the exact same solution, except using double quotes instead of single quotes \$\endgroup\$ Jun 23, 2017 at 0:06
  • \$\begingroup\$ Nvm. Great start! \$\endgroup\$
    – Joseph
    Jun 23, 2017 at 0:07
  • \$\begingroup\$ @juniorRubyist Its an anonymous function that can be assigned to any variable(in this case 'unpig'). \$\endgroup\$ Jun 23, 2017 at 3:13
  • \$\begingroup\$ Seems pretty unbeatable. \$\endgroup\$ Jun 23, 2017 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\$
3
  • \$\begingroup\$ Must be a function. Invalid. \$\endgroup\$ Jun 23, 2017 at 16:35
  • \$\begingroup\$ @CalculatorFeline fixed \$\endgroup\$ Jun 23, 2017 at 16:44
  • \$\begingroup\$ Actually, 22 minutes ago, your 46 byte solution became valid. \$\endgroup\$ Jun 23, 2017 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
  • 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, 2017 at 16:53
2
\$\begingroup\$

05AB1E, 15 bytes

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

Try it online!

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

\$\endgroup\$
1
  • \$\begingroup\$ Best golfing! You win. \$\endgroup\$
    – Joseph
    Jun 26, 2017 at 2:20
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
  • 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\$ Jun 23, 2017 at 3:03
1
\$\begingroup\$

Lexurgy, 29 bytes

p:
([]+)$1 \- []$2 ay=>$2 $1
\$\endgroup\$
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
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\$
0
\$\begingroup\$

Japt, 18 17 bytes

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

r"(.*)-(.)ay$"ÏiZ

Try it

\$\endgroup\$
1
  • \$\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\$ Jun 25, 2017 at 4:15
0
\$\begingroup\$

05AB1E, 15 14 bytes

I know there already is a 15-bytes 05AB1E answer. I haven't found anything shorter yet, but since I found two 15-byters with a different approach, I figured I'd post them as well EDIT: Found a 14-byter. But I'll still mention the two 15-byters either way:

œ6è…-ayÅ¿i¨¨Á¨

Very slow for larger inputs.

Try it online or verify all test cases. (The test suite uses S6.IJ instead of œ6è, since it's A LOT faster, but basically does the same. Unfortunately, builtin .I doesn't work on strings - apparently.)

4(è«…ay-Å¿i¨¨Á¨

Try it online or verify all test cases.

`«ŠŠ«…ay-QiJÁëI

Try it online or verify all test cases.

Explanation:

œ               # Get all permutations of the (implicit) input-string
 6è             # Only leave the 7th one (0-based 6th)
   …-ayÅ¿i      # If this permutation ends with "-ay":
          ¨¨    #  Remove the last two characters from the (implicit) input-string ("ay")
            Á   #  Rotate it once towards the right
             ¨  #  Rotate the last character ("-")
                #  (after which it is output implicitly as result)
                # (implicit else)
                #  (implicitly output the implicit input unchanged)

4(è             # Get the 0-based -4'th character of the (implicit) input
   «            # Append it to the (implicit) input
    …ay-Å¿i     # If this string ends with "ay-"
           ¨¨Á¨ #  Same as above

`               # Push all characters of the (implicit) input separately to the stack
 «              # Merge the last two together
  ŠŠ            # Triple-swap the last three items twice (a,b,c to b,c,a)
    «           # Merge the top two together
     …ay-Qi     # If this is equal to "ay-":
           J    #  Join the other characters on the stack together
            Á   #  And rotate it once towards the right
          ë     # Else:
           I    #  Push the input
                # (after which the top of the stack is output implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

Pip, 18 bytes

a~`-(.)ay$`?$1.$`a

Try It Online!

Explanation

a~`-(.)ay$`?$1.$`a
a                   First command-line argument
 ~                  Find first match
  `-(.)ay$`         of this regex (capture the letter in group 1)
           ?        If a match was found:
            $1        Contents of group 1
              .       Concatenated with
               $`     Portion of the string before the match
                    Else:
                 a    The string unchanged
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.