代码高尔夫 - 文字扰乱器
请按照我下面给出的示例输入和输出,用尽可能短的源代码来回答将任意明文转换为其相应密文的程序。 CPU 时间最少或内存使用量最少的奖励积分*。
示例 1:
明文:敏捷的棕色狐狸跳过了懒狗。 Supercalifragilisticexpialidocious!
密文: eTh kiquc nobrw xfo smjup rvoe eth yalz .odg !uioiapeislgriarpSueclfaiitcxildcos
示例 2:
明文: 123 1234 12345 123456 1234567 12345678 123456789
C密文:312 4213 53124 642135 7531246 86421357 975312468
规则:
- 标点符号被定义为包含在最接近的单词中。
- 单词的中心被定义为ceiling((strlen(word)+1)/2)。
- 空白被忽略(或折叠)。
- 奇怪的单词首先向右移动。甚至单词也会先向左移动。
您可以将其视为向后读取所有其他字符(从单词末尾开始),然后向前读取其余字符。公司 => XoXpXrXtXoX =>尼亚奥·科普托。
感谢那些指出我的描述中不一致之处的人。这导致你们中的许多人走上了错误的道路,对此我深表歉意。规则#4 应该可以解决问题。
*仅当杰夫·阿特伍德决定这样做时才会授予奖励积分。因为我没有和他确认过,所以可能性很小。对不起。
Please answer with the shortest possible source code for a program that converts an arbitrary plaintext to its corresponding ciphertext, following the sample input and output I have given below. Bonus points* for the least CPU time or the least amount of memory used.
Example 1:
Plaintext: The quick brown fox jumps over the lazy dog. Supercalifragilisticexpialidocious!
Ciphertext: eTh kiquc nobrw xfo smjup rvoe eth yalz .odg !uioiapeislgriarpSueclfaiitcxildcos
Example 2:
Plaintext: 123 1234 12345 123456 1234567 12345678 123456789
Ciphertext: 312 4213 53124 642135 7531246 86421357 975312468
Rules:
- Punctuation is defined to be included with the word it is closest to.
- The center of a word is defined to be ceiling((strlen(word)+1)/2).
- Whitespace is ignored (or collapsed).
- Odd words move to the right first. Even words move to the left first.
You can think of it as reading every other character backwards (starting from the end of the word), followed by the remaining characters forwards. Corporation => XoXpXrXtXoX => niaorCoprto.
Thank you to those who pointed out the inconsistency in my description. This has lead many of you down the wrong path, which I apologize for. Rule #4 should clear things up.
*Bonus points will only be awarded if Jeff Atwood decides to do so. Since I haven't checked with him, the chances are slim. Sorry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Python,50 个字符
对于
i
中的输入:处理自己 IO 的替代版本:
如果包含空格,则总共 66 个字符。 (从技术上讲,如果从命令行运行,则可以省略
print
,因为代码的评估值默认显示为输出。)使用
reduce
的替代版本:59人物。
i
中输入的原始版本(偶数和奇数都先向右):48 个字符,包括空格。
另一个替代版本(虽然稍长)稍微更有效:
(53 个字符)
Python, 50 characters
For input in
i
:Alternate version that handles its own IO:
A total of 66 characters if including whitespace. (Technically, the
print
could be omitted if running from a command line, since the evaluated value of the code is displayed as output by default.)Alternate version using
reduce
:59 characters.
Original version (both even and odd go right first) for an input in
i
:48 characters including whitespace.
Another alternate version which (while slightly longer) is slightly more efficient:
(53 characters)
J,58 个字符
J, 58 characters
Haskell,64 个字符
好吧,如果您添加必要的“
导入列表”。
Haskell, 64 characters
Well, okay, 76 if you add in the requisite "
import List
".Python - 69 个字符
(包括空格和换行符)
这处理所有 I/O。
Python - 69 chars
(including whitespace and linebreaks)
This handles all I/O.
Perl,78 个字符
用于在
$_
中输入。如果不可接受,请在开头添加$_=<>;
或$_=$s;
六个字符。换行符只是为了可读性。Perl, 78 characters
For input in
$_
. If that's not acceptable, add six characters for either$_=<>;
or$_=$s;
at the beginning. The newline is for readability only.C,140 个字符
格式良好:
压缩:
C, 140 characters
Nicely formatted:
Compressed:
Lua
130 个字符函数,147 个字符函数程序
Lua 在代码高尔夫中没有得到足够的喜爱——也许是因为当你有像
function
/end< 这样的长关键字时很难编写一个短程序。 /code>、
if
/then
/end
等。首先,我详细地编写了该函数并附有解释,然后将其重写为一个压缩的独立函数,然后我在命令行指定的单个参数上调用该函数。
我必须使用
标签来格式化代码,因为 Markdown 在格式化 Lua 方面做得很糟糕。
从技术上讲,您可以通过内联函数来获得更小的运行程序,但这种方式更加模块化:)
输出:
我对 Lua 还很陌生,所以我希望看到一个更短的解决方案(如果有)。
For a minimal cipher on all args to stdin, we can do 111 chars:
但这种方法确实会像其他一些解决方案一样输出尾随空格。
Lua
130 char function, 147 char functioning program
Lua doesn't get enough love in code golf -- maybe because it's hard to write a short program when you have long keywords like
function
/end
,if
/then
/end
, etc.First I write the function in a verbose manner with explanations, then I rewrite it as a compressed, standalone function, then I call that function on the single argument specified at the command line.
I had to format the code with
<pre></pre>
tags because Markdown does a horrible job of formatting Lua.Technically you could get a smaller running program by inlining the function, but it's more modular this way :)
Output:
I'm still pretty new at Lua so I'd like to see a shorter solution if there is one.
For a minimal cipher on all args to stdin, we can do 111 chars:
But this approach does output a trailing space like some of the other solutions.
对于
s
中的输入:Python,90 个字符,包括空格。
For an input in
s
:Python, 90 characters including whitespace.
TCL
125个字符
TCL
125 characters
Bash - 133,假设输入在 $w 变量中,
很好
压缩得
,所以它输出一个尾随空格。
Bash - 133, assuming input is in $w variable
Pretty
Compressed
Ok, so it outputs a trailing space.