代码高尔夫:Pig Latin
挑战:
输入任意长度的句子,并将该句子中的所有单词转换为猪拉丁语。如果您不知道 Pig Latin 是什么,请阅读维基百科:Pig Latin。
规范:
假设所有单词均以空格分隔,所有句子均以感叹号、问号或句号结尾。
请勿使用维基百科中提及的元音变体。
对于诸如 bread 和 quiz 之类的词,使用 readbay、uizqay 代替 eadbray 和 izquay 是完全可以接受的。
函数或方法是完全可以接受的。换句话说,您不需要接受用户输入,但必须显示用户输出。
假设没有输入包含复合词。
示例:
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:
Assume all words are separated by spaces and all sentences either end with a exclamation, question mark or period.
Do not use the variant for vowels mentioned in Wikipedia.
For words such as bread and quiz it is perfectly acceptable for them to be readbay, uizqay instead of and eadbray and izquay.
Functions or methods are perfectly acceptable. In other words you do not need to take in user input, but you must display user output.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
sed -
53/5545/47 个字符使用 -r 选项 (2+43):
不使用 -r 选项 (47):
sed -
53/5545/47 charsWith the -r option (2+43):
Without the -r option (47):
C#
25796 个字符可读版本:
精简版
输入:
输出:
C#
25796 charactersReadable Version:
Condensed
Input:
Output:
GolfScript -
605352514946 个字符GolfScript -
605352514946 charsRuby 1.9+:
6362个字符只是一个快速答案,可能可以缩短更多
它处理
qu
(question => estionquay)的情况,并用 double 打印引用。还需要 3 个字节来摆脱它们(我说没有对此进行说明)编辑 1:如果使用 Ruby 1.9 保存一个字符 (
?w
),让我们使用它。Ruby 1.9+:
6362 charsJust a quick answer, probably can be shortened more
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 个字符适用于标点符号。
感谢莫布鲁莱。
用途:
输出:
Perl
87,56, 47 charsworks with punctuation.
Thanks to mobrule.
Usage :
Output :
Groovy,
11710091858379 个字符可读版本:
Groovy,
11710091858379 charsReadable version:
Haskell:
244199222214 个字符解决方案根据原始大写为转换后的单词提供合理的大写。现在可以正确处理前辅音簇。注意:最后一行末尾不包含换行符。
测试输入:
测试输出:
Haskell:
244199222214 charsSolution 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.
Test Input:
Test Output:
VB.NET:106 个字符
假设“s”是输入,并且还
导入 System.Text.RegularExpressions
。 (有趣的是,由于需要 @ 字符串文字前缀和尾随分号,该 VB.NET 版本比 C# 版本多了 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.)Python 3 -
107106 个字符不保留大写,如评论中所允许的。但标点符号被保留。添加空格和换行只是为了提高可读性(因此
import re
之后的;
)。如果我们不处理“qu”单词,则可以删除 3 个字符 (
qu|
)。用法示例:
Python 3 —
107106 charsNot preserving capitalization, as allowed in the comment. But punctuations are preserved. Whitespaces and linebreaks are added for readability only (hence the
;
afterimport re
).3 chars can be removed (
qu|
) if we don't handle the "qu" words.Example usage:
Python 3 -
100103106 个字符(与 KennyTM 类似;正则表达式在此处有所不同。)
注意:由于要考虑正则表达式的修改,字符数从 100 变为 103为“曲”。
注 2:当“y”用于元音时,103 个字符的版本会失败。布莱赫。 (另一方面,当“y”用于元音时,KennyTM 的 106 个字符版本也会失败,所以无论如何。)
Python 3 -
100103106 chars(similar to KennyTM's; the regex makes the difference here.)
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 个字符
与 VB.NET 答案相同的概念,仅使用 Boo 来节省一些击键。
哎呀...我刚刚注意到这不能处理结尾标点符号。或者任何标点符号。哦,好吧 - 许多其他解决方案也没有。
Boo (.NET): 91 chars
Same concept as VB.NET answer, only using Boo to save a few keystrokes.
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 个字符
输入:
输出:
Lua, 109 characters
Input:
Output:
Perl,70 个字符
为了让事情顺利进行:
我相信它可以在某些地方得到改进。
Perl, 70 characters
To get the ball rolling:
I'm sure it can be improved somewhere.
Python - 107 个字符
Python - 107 chars
PHP 102 字节
使用 preg 的 PHP 80 字节
PHP 102 bytes
PHP with use of preg 80 bytes