代码高尔夫:Pig Latin

发布于 2024-09-08 03:02:50 字数 598 浏览 4 评论 0原文

挑战:

输入任意长度的句子,并将该句子中的所有单词转换为猪拉丁语。如果您不知道 Pig Latin 是什么,请阅读维基百科:Pig Latin

规范:

  1. 假设所有单词均以空格分隔,所有句子均以感叹号、问号或句号结尾。

  2. 请勿使用维基百科中提及的元音变体。

  3. 对于诸如 bread 和 quiz 之类的词,使用 readbay、uizqay 代替 eadbray 和 izquay 是完全可以接受的。

  4. 函数或方法是完全可以接受的。换句话说,您不需要接受用户输入,但必须显示用户输出。

  5. 假设没有输入包含复合词。

示例:

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

如何获胜:

获胜者是能够编写一个程序以最少的字符完成挑战的人。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

拿命拼未来 2024-09-15 03:02:50

sed - 53/55 45/47 个字符

使用 -r 选项 (2+43):

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

不使用 -r 选项 (47):

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

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
等风来 2024-09-15 03:02:50

C# 257 96 个字符

可读版本:

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

精简版

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

输入:

LINQ 帮助我写出好的高尔夫答案

输出:

INQLay elpshay emay riteway oodgay olfgay Answersway

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

相思故 2024-09-15 03:02:50

GolfScript - 60 53 52 51 49 46 个字符

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

GolfScript - 60 53 52 51 49 46 chars

)](' '/{1/(."AEIOUaeiou"-!{\119}*"ay "}%));+\+
请远离我 2024-09-15 03:02:50

Ruby 1.9+:63 62个字符

只是一个快速答案,可能可以缩短更多

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

它处理qu(question => estionquay)的情况,并用 double 打印引用。还需要 3 个字节来摆脱它们(我说没有对此进行说明)

编辑 1:如果使用 Ruby 1.9 保存一个字符 (?w),让我们使用它。

+
amp;:e+?w}ay"}

它处理qu(question => estionquay)的情况,并用 double 打印引用。还需要 3 个字节来摆脱它们(我说没有对此进行说明)

编辑 1:如果使用 Ruby 1.9 保存一个字符 (?w),让我们使用它。

Ruby 1.9+: 63 62 chars

Just a quick answer, probably can be shortened more

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

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.

+
amp;: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.

ぺ禁宫浮华殁 2024-09-15 03:02:50

Perl 8756、47 个字符

适用于标点符号。

感谢莫布鲁莱。

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

用途:

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

输出:

Iway, amway; away: appyhay! anmay.

Perl 87, 56, 47 chars

works with punctuation.

Thanks to mobrule.

s/\b[aeiou]\w*/w
amp;/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.
请止步禁区 2024-09-15 03:02:50

Groovy,117 100 91 85 83 79 个字符

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

可读版本:

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

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" 
    })
烟雨扶苏 2024-09-15 03:02:50

Haskell: 244 199 222 214 个字符

解决方案根据原始大写为转换后的单词提供合理的大写。现在可以正确处理前辅音簇。注意:最后一行末尾不包含换行符。

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)

测试输入:

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

测试输出:

Idday ethay appingstray anmay aysay: "Iway amway Octorday Xay!"?

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!"?
黎歌 2024-09-15 03:02:50

VB.NET:106 个字符

假设“s”是输入,并且还导入 System.Text.RegularExpressions(有趣的是,由于需要 @ 字符串文字前缀和尾随分号,该 VB.NET 版本比 C# 版本多了 3 个字符。)

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

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")
烟燃烟灭 2024-09-15 03:02:50

Python 3 - 107 106 个字符

不保留大写,如评论中所允许的。但标点符号被保留。添加空格和换行只是为了提高可读性(因此 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()))

如果我们不处理“qu”单词,则可以删除 3 个字符 (qu|)。

用法示例:

$ python3.1 x.py
The "quick brown fox" jumps over: the lazy dog.
eThay "ickquay ownbray oxfay" umpsjay overway: ethay azylay ogday.

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.
一曲爱恨情仇 2024-09-15 03:02:50

Python 3 - 100 103 106 个字符

(与 KennyTM 类似;正则表达式在此处有所不同。)

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

注意:由于要考虑正则表达式的修改,字符数从 100 变为 103为“曲”。

注 2:当“y”用于元音时,103 个字符的版本会失败。布莱赫。 (另一方面,当“y”用于元音时,KennyTM 的 106 个字符版本也会失败,所以无论如何。)

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

难以启齿的温柔 2024-09-15 03:02:50

Boo (.NET):91 个字符

与 VB.NET 答案相同的概念,仅使用 Boo 来节省一些击键。

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

哎呀...我刚刚注意到这不能处理结尾标点符号。或者任何标点符号。哦,好吧 - 许多其他解决方案也没有。

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.

┊风居住的梦幻卍 2024-09-15 03:02:50

Lua,109 个字符

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

输入:

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

输出:

oTay ebay onesthay, Iway ouldway aysay "oNay!" otay atthay uestionqay.

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.
那请放手 2024-09-15 03:02:50

Perl,70 个字符

为了让事情顺利进行:

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

我相信它可以在某些地方得到改进。

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.

叹梦 2024-09-15 03:02:50

Python - 107 个字符

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]

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]
旧人哭 2024-09-15 03:02:50

PHP 102 字节

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

使用 preg 的 PHP 80 字节

<?=preg_filter('#\b(([aioue]\w*)|(\w)(\w*))\b#ie','"$2"?$2way:$4$3ay',SENTENCE);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文