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.

Community
  • 1
  • 1
  • 4
    @Byron Whitlock: The satisfaction of winning. And a green checkmark by your answer if you win. :P –  Jul 06 '10 at 23:34
  • Should we properly capitalize a capitalized word? Can we ignore cases where entire words are capitalized? – Thomas Eding Jul 07 '10 at 02:25
  • @trinithis: It does not matter. Either way is fine. –  Jul 07 '10 at 02:33
  • Ok, who voted to close this for being off-topic? And can that person provide reasoning why he/she decided to, please? –  Jul 07 '10 at 03:34
  • @thyrgle: Don't sweat it; it's just a code golf hater. The close vote will age away after a few days. – Robert Harvey Jul 07 '10 at 03:59
  • What? without the requirement to maintain capitalization, this is way too easy! – MtnViewMark Jul 07 '10 at 04:15
  • 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. – MtnViewMark Jul 07 '10 at 05:10
  • 1
    @MtnViewMark: Yeah you need to maintain punctuation. –  Jul 07 '10 at 05:12
  • @thy: Should the program include full input/output? – kennytm Jul 07 '10 at 08:43
  • @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. –  Jul 07 '10 at 18:58
  • @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. – kennytm Jul 07 '10 at 19:35
  • @thyrgle: Probably you should comment on whether `quiz` -> `uizqay` is acceptable, and whether `bread` -> `readbay` is acceptable. (Writing a spec is hard :) ). – kennytm Jul 07 '10 at 19:37
  • @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. –  Jul 07 '10 at 20:03
  • What about the word `honest`? Is the Wikipedia rule about silent consonants required? Should `honest` be `honestway` or `onesthay`? – gwell Jul 08 '10 at 00:09
  • @gwell: Don't worry about words like those because they are language oddities. –  Jul 08 '10 at 01:37

15 Answers15

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
Nabb
  • 3,434
  • 3
  • 22
  • 32
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

gpmcadam
  • 6,346
  • 2
  • 33
  • 37
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • 3
    Shaved off about 2/3 of the solution by using LINQ. – gpmcadam Jul 07 '10 at 03:54
  • 1
    Woah, I've never seen such a short golf in C#. +1 – Maulrus Jul 07 '10 at 04:15
  • 14
    @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 Jul 07 '10 at 09:30
  • 2
    @Johannes: Ohhhh, that makes sense. If it's not a full program, I'm withdrawing my vote. – Maulrus Jul 07 '10 at 10:06
  • @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 Jul 07 '10 at 21:07
  • @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"...;) – MickeyfAgain_BeforeExitOfSO Jul 08 '10 at 14:09
9

GolfScript - 60 53 52 51 49 46 chars

)](' '/{1/(."AEIOUaeiou"-!{\119}*"ay "}%));+\+
Nabb
  • 3,434
  • 3
  • 22
  • 32
  • 1
    -1. The sample input gives `Iway amway away appyhay an.may` — the `man.` is converted incorrectly. – kennytm Jul 07 '10 at 08:41
  • 7
    @KennyTM The specifications the input ends with a "!" "?" or ".". It doesn't say they can end with ".\n" – Nabb Jul 07 '10 at 12:00
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.

Chubas
  • 17,823
  • 4
  • 48
  • 48
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.
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Nice piggy back solution! Good show. I love seeing Perl do its thing. – mattmc3 Jul 08 '10 at 11:21
  • Run this as `perl -p ...` and you can drop 12 more characters (minus a 3 char penalty for the extra command-line switch) – mob Jul 08 '10 at 14:32
  • By the "rules" of code-golf you need to give yourself a 3 character penalty to use the `-p` switch. – mob Jul 08 '10 at 15:36
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" 
    })
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
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!"?
Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
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")
mattmc3
  • 17,595
  • 7
  • 83
  • 103
  • 2
    Compliment and put down all in one sentence :) – Spence Jul 07 '10 at 08:05
  • 1
    It still needs a program around it – Joey Jul 07 '10 at 10:26
  • 2
    @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 Jul 07 '10 at 11:53
  • 1
    @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 Jul 07 '10 at 13:17
  • Also, if it's in a REPL you can remove the `Return` – Amnon Jul 07 '10 at 15:11
  • 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 Jul 08 '10 at 05:28
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.
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
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.)

JAB
  • 20,783
  • 6
  • 71
  • 80
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.

mattmc3
  • 17,595
  • 7
  • 83
  • 103
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.
Community
  • 1
  • 1
gwell
  • 2,695
  • 20
  • 20
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.

Nas Banov
  • 28,347
  • 6
  • 48
  • 67
Anon.
  • 58,739
  • 8
  • 81
  • 86
  • You could chop off a few by doing `perl -pe '...'` rather than the while/print bits yourself in the code. – Daenyth Jul 07 '10 at 03:32
  • 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 Jul 07 '10 at 08:45
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]
Tal Weiss
  • 8,889
  • 8
  • 54
  • 62
Bwmat
  • 4,314
  • 3
  • 27
  • 42
  • 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 Jul 10 '10 at 03:38
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);