开发者

Code Golf: Pig Latin

开发者 https://www.devze.com 2023-01-06 03:07 出处:网络
Locked. This question and its answers are locked because the question is off-topic but has historical signific开发者_运维知识库ance. It is not currently accepting new answers or interactions.
Locked. This question and its answers are locked because the question is off-topic but has historical signific开发者_运维知识库ance. It is not currently accepting new answers or interactions.

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.


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


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


GolfScript - 60 53 52 51 49 46 chars

)](' '/{1/(."AEIOUaeiou"-!{\119}*"ay "}%));+\+


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.


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.


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" 
    })


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!"?


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


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.


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


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.


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.


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.


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]


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消