Code Golf: Pig Latin
Asked Answered
O

15

23

Challenge:

Take a sentence of input of any length and convert all the words in that sentence to pig latin. If you do not know what pig latin is please read Wikipedia: Pig Latin.

Specifications:

  1. Assume all words are separated by spaces and all sentences either end with a exclamation, question mark or period.

  2. Do not use the variant for vowels mentioned in Wikipedia.

  3. For words such as bread and quiz it is perfectly acceptable for them to be readbay, uizqay instead of and eadbray and izquay.

  4. Functions or methods are perfectly acceptable. In other words you do not need to take in user input, but you must display user output.

  5. Assume no input contains a compound word.

Example:

Input: I am a happy man.
Output: Iway amway away appyhay anmay.

How to win:

The winner is the person who can write a program that will do the challenge with the least amount of characters.

Orbicular answered 6/7, 2010 at 23:25 Comment(15)
@Byron Whitlock: The satisfaction of winning. And a green checkmark by your answer if you win. :POrbicular
Should we properly capitalize a capitalized word? Can we ignore cases where entire words are capitalized?Sanitation
@trinithis: It does not matter. Either way is fine.Orbicular
Ok, who voted to close this for being off-topic? And can that person provide reasoning why he/she decided to, please?Orbicular
@thyrgle: Don't sweat it; it's just a code golf hater. The close vote will age away after a few days.Alegre
What? without the requirement to maintain capitalization, this is way too easy!Ghirlandaio
Is it a requirement to retain all punctuation and spacing, both at the end and in the middle? For example, shouldn't He said "Hey, man!" aloud. become Ehay aidsay "Eyhay, anmay!" aloudway. ? I notice that most solutions do not, and many don't preserve the final punctuation.Ghirlandaio
@MtnViewMark: Yeah you need to maintain punctuation.Orbicular
@thy: Should the program include full input/output?Prejudge
@KennyTM: As in "User input:" and "Output:"? No. But, if you are asking about something else I do not understand what you are trying to get at.Orbicular
@thyrgle: Things like cin >> and cout <<. Some solutions e.g. the C#'s one is just a pure function, that can cut down a lot of characters.Prejudge
@thyrgle: Probably you should comment on whether quiz -> uizqay is acceptable, and whether bread -> readbay is acceptable. (Writing a spec is hard :) ).Prejudge
@KennyTM: Functions or methods are fine, no need for input such as cin >>. No need to take user input. Hmmm... And as for those words you have brought up: do not worry about managing for words of those kind, they are acceptable as you have presented them, and as for compound words do not worry about them either.Orbicular
What about the word honest? Is the Wikipedia rule about silent consonants required? Should honest be honestway or onesthay?Miser
@gwell: Don't worry about words like those because they are language oddities.Orbicular
W
12

sed - 53/55 45/47 chars

With the -r option (2+43):

s/\b[aeiou]\w*/w&/gi;s/\b(\w)(\w*)/\2\1ay/g

Without the -r option (47):

s/\b[aeiou]\w*/w&/gi;s/\b\(\w\)\(\w*\)/\2\1ay/g
Womankind answered 6/7, 2010 at 23:25 Comment(0)
C
11

C# 257 96 characters

Readable Version:

string.Join(" ",
    args.Select(y =>
        ("aeiouAEIOU".Contains(y[0])
        ? y + "way"
        : y.Substring(1) + y[0] + "ay")
    )
);

Condensed

string.Join(" ",args.Select(y=>("aeiouAEIOU".Contains(y[0])?y+"way":y.Substring(1)+y[0]+"ay")));

Input:

LINQ helps me write good golf answers

Output:

INQLay elpshay emay riteway oodgay olfgay answersway

Chlodwig answered 6/7, 2010 at 23:25 Comment(6)
Shaved off about 2/3 of the solution by using LINQ.Chlodwig
Woah, I've never seen such a short golf in C#. +1Dituri
@Maulrus: That's because this one misses everything what makes it into a program. Namely you need to put class A{static void Main(string[]a){ before all that (and replace "args" by "a", of course. Why they used multi-letter variable names is beyond me, actually.Legislature
@Johannes: Ohhhh, that makes sense. If it's not a full program, I'm withdrawing my vote.Dituri
@mickey the OP's 3rd rule states that ignoring syllables, and breaking simply on first letter, is acceptable (although I agree with you, it should be on syllable).Dwightdwindle
@Chadwick, Johannes: Syllable vs letter. Yeah, it just doesn't sound right to my ear. Not like real, properly educated Pig Latin, more like "Pigeon Pig Latin"...;)Byte
W
9

GolfScript - 60 53 52 51 49 46 chars

)](' '/{1/(."AEIOUaeiou"-!{\119}*"ay "}%));+\+
Womankind answered 6/7, 2010 at 23:25 Comment(2)
-1. The sample input gives Iway amway away appyhay an.may — the man. is converted incorrectly.Prejudge
@KennyTM The specifications the input ends with a "!" "?" or ".". It doesn't say they can end with ".\n"Womankind
R
7

Ruby 1.9+: 63 62 chars

Just a quick answer, probably can be shortened more

p gets.gsub(/\w+/){|e|"#{e=~/^(qu|[^aeiou]+)/i?$'+$&:e+?w}ay"}

it handles the case of the qu (question => estionquay), and prints with double qoutes. 3 more bytes for getting rid of them (I say no specification about this)

Edit 1: If using Ruby 1.9 saves a character (?w), let's use it.

Ruinous answered 6/7, 2010 at 23:25 Comment(0)
D
6

Perl 87, 56, 47 chars

works with punctuation.

Thanks to mobrule.

s/\b[aeiou]\w*/w$&/gi;s/\b(\w)(\w*)/\2\1ay/g

Usage :

echo 'I, am; a: happy! man.' | perl -p piglatin.pl

Output :

Iway, amway; away: appyhay! anmay.
Droshky answered 6/7, 2010 at 23:25 Comment(3)
Nice piggy back solution! Good show. I love seeing Perl do its thing.Disposed
Run this as perl -p ... and you can drop 12 more characters (minus a 3 char penalty for the extra command-line switch)Subclavius
By the "rules" of code-golf you need to give yourself a 3 character penalty to use the -p switch.Subclavius
S
4

Haskell: 244 199 222 214 chars

Solution gives reasonable capitalization to transformed words based on original capitalization. Now properly handles leading consonant clusters. Note: no newline included at end of last line.

import Data.Char
import Data.List
q(x:y,z)|w x=x%(z++toLower x:y++"ay")|0<1=x:y++z
q(_,z)=z++"way"
x%(y:z)|isUpper x=toUpper y:z|0<1=y:z
w=isAlpha
main=interact$(>>=q.break(`elem`"aeiouAEIOU")).groupBy((.w).(==).w)

Test Input:

Did the strapping man say: "I am Doctor X!"?

Test Output:

Idday ethay appingstray anmay aysay: "Iway amway Octorday Xay!"?
Sanitation answered 6/7, 2010 at 23:25 Comment(0)
A
4

Groovy, 117 100 91 85 83 79 chars

print args[0].replaceAll(/(?i)\b(\w*?)([aeiou]\w*)/,{a,b,c->c+(b?b:'w')+"ay"})

Readable version:

print args[0]
.replaceAll(
    /(?i)\b(\w*?)([aeiou]\w*)/ ,
    {
        a, b, c ->
        c + ( b ? b : 'w' ) + "ay" 
    })
Asteroid answered 6/7, 2010 at 23:25 Comment(0)
R
3

Python 3 - 100 103 106 chars

(similar to KennyTM's; the regex makes the difference here.)

import re;print(re.sub('(?i)(y|qu|\w*?)([aeiouy]\w*)',lambda m:m.group(2)+(m.group(1)or'w')+'ay',input()))

Note: went from 100 to 103 characters because of modification of the regex to account for "qu".

Note 2: Turns out the 103-char version fails when "y" is used for a vowel sound. Bleh. (On the other hand, KennyTM's 106-char version also fails when "y" is used for a vowel sound, so whatever.)

Richert answered 6/7, 2010 at 23:25 Comment(0)
C
3

Python 3 — 107 106 chars

Not preserving capitalization, as allowed in the comment. But punctuations are preserved. Whitespaces and linebreaks are added for readability only (hence the ; after import re).

import re;
print(re.sub('(?i)\\b(qu|[^aeiou\W]*)(\w*)',
             lambda m:m.group(2)+(m.group(1)or'w')+'ay',
             input()))

3 chars can be removed (qu|) if we don't handle the "qu" words.

Example usage:

$ python3.1 x.py
The "quick brown fox" jumps over: the lazy dog.
eThay "ickquay ownbray oxfay" umpsjay overway: ethay azylay ogday.
Clem answered 6/7, 2010 at 23:25 Comment(0)
D
3

VB.NET: 106 chars

Assumes "s" is the input, and also Imports System.Text.RegularExpressions. (Interestingly, due to the need for the @ string literal prefix and the trailing semi-colon, this VB.NET version beats the C# equivalent by 3 chars.)

Return Regex.Replace(Regex.Replace(s, "(?i)\b([aeiou]\S*)", "$1way"), "(?i)\b([^aeiou\s])(\S*)", "$2$1ay")
Disposed answered 6/7, 2010 at 23:25 Comment(6)
Compliment and put down all in one sentence :)Besiege
It still needs a program around itLegislature
@Johannes - not necessarily. It could always be run though something like this (tirania.org/blog/archive/2008/Sep-08.html) or any of these (#2059215). No reason to over-inflate the existing code-golf bias towards dynamic languages. They can stand on their own merits :)Disposed
@matt: In that case I can shorten many of my PowerShell golfings by assuming that input is given by the function i instead of using the iterator $input which shortens things also quite a bit (at least it can make a difference between beating Ruby or not).Legislature
Also, if it's in a REPL you can remove the ReturnHaswell
I don't see the Python solutions assuming import re - so why would you have the right to assume this Imports System.Text.RegularExpressions? Only fully functional solutions should be applying for code golf. Your program doesn't even read input...Kinney
M
2

Lua, 109 characters

print((io.read():gsub("(%A*)([^AEIOUaeiou]*)(%a+)",function(a,b,c)return a..c..b..(#b<1 and"way"or"ay")end)))

Input:

To be honest, I would say "No!" to that question.

Output:

oTay ebay onesthay, Iway ouldway aysay "oNay!" otay atthay uestionqay.
Miser answered 6/7, 2010 at 23:25 Comment(0)
D
2

Boo (.NET): 91 chars

Same concept as VB.NET answer, only using Boo to save a few keystrokes.

print /(?i)\b([^aeiou\s])(\S*)/.Replace(/(?i)\b([aeiou]\S*)/.Replace(s, "$1way"), "$2$1ay")

Oops... I just noticed that this doesn't handle the ending punctuation. Or really any punctuation. Oh well - neither do many of the other solutions.

Disposed answered 6/7, 2010 at 23:25 Comment(0)
B
1

PHP 102 bytes

<?foreach(split(~ß,SENTENCE)as$a)echo($b++?~ß:'').(strpos(' aeuio',$a[0])?$a.w:substr($a,1).$a[0]).ay;

PHP with use of preg 80 bytes

<?=preg_filter('#\b(([aioue]\w*)|(\w)(\w*))\b#ie','"$2"?$2way:$4$3ay',SENTENCE);
Belligerency answered 6/7, 2010 at 23:25 Comment(0)
A
1

Python - 107 chars

i=raw_input()
print" ".join(w+"way"if w[0]in"aeiouyAEIOUY"else w[1:]+w[0]+"ay"for w in i[:-1].split())+i[-1]
Arduous answered 6/7, 2010 at 23:25 Comment(1)
There are many words that use an initial Y as a consonant, but very few that use an initial Y as a vowel. This version works well with Yggdrasil and Yttrium, but seems to ignore the more common yes, yodel, and young. It gives yesway, yodelway, and youngway.Miser
E
1

Perl, 70 characters

To get the ball rolling:

while(<>){for(split){s/^([^aeiou]+)(.*)/$2$1ay / or $_.='way ';print}}

I'm sure it can be improved somewhere.

Eructate answered 6/7, 2010 at 23:25 Comment(2)
You could chop off a few by doing perl -pe '...' rather than the while/print bits yourself in the code.Withdrew
With the sample input an incorrect output Iay amway away appyhay an.may is generated. It should be Iway (include case-insensitive flag) and anmay. (ignore the non-word chars).Prejudge

© 2022 - 2024 — McMap. All rights reserved.