views:

1311

answers:

14

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.

+1  A: 

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.

Anon.
You could chop off a few by doing `perl -pe '...'` rather than the while/print bits yourself in the code.
Daenyth
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).
KennyTM
+5  A: 

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" 
    })
seanizer
+1  A: 

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]
Bwmat
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`.
gwell
+9  A: 

GolfScript - 60 53 52 51 49 46 chars

)](' '/{1/(."AEIOUaeiou"-!{\119}*"ay "}%));+\+
Nabb
-1. The sample input gives `Iway amway away appyhay an.may` — the `man.` is converted incorrectly.
KennyTM
@KennyTM The specifications the input ends with a "!" "?" or ".". It doesn't say they can end with ".\n"
Nabb
+4  A: 

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!"?
trinithis
+10  A: 

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

Anthony Pegram
Shaved off about 2/3 of the solution by using LINQ.
Bauer
Woah, I've never seen such a short golf in C#. +1
Maulrus
@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.
Joey
@Johannes: Ohhhh, that makes sense. If it's not a full program, I'm withdrawing my vote.
Maulrus
I think "riteway" should be "itewray". Pig Latin as I learned it shifts the leading consonant sound, not the single letter. (I have not checked what Wikipedia has to say about it)
mickeyf
"question" would probably also written wrongly.
Joey
@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).
Chadwick
@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"...;)
mickeyf
+7  A: 

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.

Chubas
+12  A: 

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
Nabb
+4  A: 

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")
mattmc3
Not bad for VB.NET. Well done.
Bauer
Compliment and put down all in one sentence :)
Spence
It still needs a program around it
Joey
@Johannes - not necessarily. It could always be run though something like this (http://tirania.org/blog/archive/2008/Sep-08.html) or any of these (http://stackoverflow.com/questions/2058715/is-there-an-interactive-interpreter-for-c). No reason to over-inflate the existing code-golf bias towards dynamic languages. They can stand on their own merits :)
mattmc3
@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).
Joey
Also, if it's in a REPL you can remove the `Return`
Amnon
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...
ChristopheD
+3  A: 

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.

mattmc3
+4  A: 

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.
KennyTM
+7  A: 

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.
M42
Nice piggy back solution! Good show. I love seeing Perl do its thing.
mattmc3
Run this as `perl -p ...` and you can drop 12 more characters (minus a 3 char penalty for the extra command-line switch)
mobrule
@mobrule: thanks a lot
M42
By the "rules" of code-golf you need to give yourself a 3 character penalty to use the `-p` switch.
mobrule
+4  A: 

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.)

JAB
+2  A: 

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.
gwell