高尔夫代码:格雷码
挑战
按字符数计算的最短程序,输出 n 位格雷码。 n
是一个小于 1000
100000
的任意数字(根据用户建议),取自标准输入。格雷码将在标准输出中打印,如示例所示。
注意:我不希望程序在合理的时间内打印格雷码(n=100000
有点矫枉过正);我确实希望它能够开始打印。
示例
输入:
4
预期输出:
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
The Challenge
The shortest program by character count that outputs the n-bit Gray Code. n
will be an arbitrary number smaller than 1000
100000
(due to user suggestions) that is taken from standard input. The gray code will be printed in standard output, like in the example.
Note: I don't expect the program to print the gray code in a reasonable time (n=100000
is overkill); I do expect it to start printing though.
Example
Input:
4
Expected Output:
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
Python - 53 个字符
这个 54 个字符版本克服了 Python2 中的范围限制,因此 n=100000 有效!
69 个字符
75 个字符
Python - 53 chars
This 54 char version overcomes the limitation of range in Python2 so n=100000 works!
69 chars
75 chars
APL(29 个字符)
使用函数 F as(
⌽
是“旋转”字符)这会生成 5 位格雷码 (
⍴
现在是“rho”字符)数字“5”可以更改或成为变量。
(对不可打印的 APL 字符感到抱歉。所以不允许我作为新用户发布图像)
APL (29 chars)
With the function F as (
⌽
is the 'rotate' char)This produces the Gray Code with 5 digits (
⍴
is now the 'rho' char)The number '5' can be changed or be a variable.
(Sorry about the non-printable APL chars. SO won't let me post images as a new user)
不可能!语言(54
58字符)测试运行:(
实际上我不'不知道是否允许使用个人语言,因为不可能!仍在开发中,但我还是想发布它..)
Impossible! language (54
58chars)Test run:
(actually I don't know if personal languages are allowed, since Impossible! is still under development, but I wanted to post it anyway..)
Golfscript - 27 个字符
从 stdin 读取,写入 stdout
示例运行
Golfscript - 27 chars
Reads from stdin, writes to stdout
Sample run
Ruby - 49 个字符
这适用于 n=100000 没有问题
Ruby - 49 chars
This works for n=100000 with no problem
C++,168 个字符,不包括空格:
C++, 168 characters, not including whitespaces:
Haskell,82 个字符:
无积分风格,赢得胜利! (或至少少 4 次)。感谢 FUZxxl。
上一篇: 86 个字符:
用交互剪切两笔画,用单行剪切一笔。
旧版本:89 个字符:
请注意,惰性可以让您免费获得即时输出。
Haskell, 82 characters:
Point-free style for teh win! (or at least 4 fewer strokes). Kudos to FUZxxl.
previous: 86 characters:
Cut two strokes with interact, one with unlines.
older: 89 characters:
Note that the laziness gets you your immediate output for free.
Mathematica 50 Chars
感谢 A. Rex 的建议!
之前的尝试
这是我在 Mathematica 中的尝试(140 个字符)。我知道它不是最短的,但我认为如果您熟悉函数式编程,它是最容易理解的(尽管这可能是我的语言偏见表现出来)。 addbit 函数采用 n 位格雷码,并使用维基百科页面中的逻辑返回 n+1 位格雷码。 makegray 代码函数以嵌套方式将 addbit 函数应用于 1 位格雷码,{{ 0}、{1}},直到创建 n 位版本。 charactercode 函数仅打印 addbit 函数输出中不带大括号和逗号的数字。
Mathematica 50 Chars
Thanks to A. Rex for suggestions!
Previous attempts
Here is my attempt in Mathematica (140 characters). I know that it isn't the shortest, but I think it is the easiest to follow if you are familiar with functional programming (though that could be my language bias showing). The addbit function takes an n-bit gray code and returns an n+1 bit gray code using the logic from the wikipedia page.. The make gray code function applies the addbit function in a nested manner to a 1 bit gray code, {{0}, {1}}, until an n-bit version is created. The charactercode function prints just the numbers without the braces and commas that are in the output of the addbit function.
直接 Python 实现构建n位格雷码维基百科上的 :
(233 个字符)
测试:
Straightforward Python implementation of what's described in Constructing an n-bit Gray code on Wikipedia:
(233 characters)
Test:
C,203 个字符
这是 C 语言的祭品:
C, 203 Characters
Here's a sacrificial offering, in C:
C#,
149143 个字符C# 并不是代码高尔夫的最佳语言,但我想我还是会尝试一下。
可读版本:
C#,
149143 charactersC# isn't the best language for code golf, but I thought I'd go at it anyway.
Readable version:
这是我的 Fantom 祭品
(177 个字符)
或扩展版本:
And here is my Fantom sacrificial offering
(177 char)
Or the expanded version:
F#,152 个字符
F#, 152 characters
F#
180175字符过多今天早上我做了另一个版本,简化了递归版本,但可惜的是,由于递归,它不会执行100000。
递归解决方案:
完成后,我为“100000”要求创建了一个工作版本 - 它太长了,无法与此处显示的其他解决方案竞争,而且我可能多次重新发明轮子,但与我在这里看到的许多解决方案不同,它将使用非常非常多的位数,嘿,这对于 F# 菜鸟来说是一次很好的学习体验 - 我没有费心去缩短它,因为它太长了无论如何;-)
迭代解决方案:(使用 100000+)
F#
180175too many charactersThis morning I did another version, simplifying the recursive version, but alas due to recursion it wouldn't do the 100000.
Recursive solution:
After that was done I created a working version for the "100000" requirement - it's too long to compete with the other solutions shown here and I probably re-invented the wheel several times over, but unlike many of the solutions I have seen here it will work with a very,very large number of bits and hey it was a good learning experience for an F# noob - I didn't bother to shorten it, since it's way too long anyway ;-)
Iterative solution: (working with 100000+)
Lua,156 个字符
这是我在 Lua 中的尝试,尽可能接近它。
LuaJIT(或带 lua-bitop 的 lua):156 字节
Lua 5.2:154 字节
Lua, 156 chars
This is my throw at it in Lua, as close as I can get it.
LuaJIT (or lua with lua-bitop): 156 bytes
Lua 5.2: 154 bytes
在无剪切的 Prolog 中(如果删除“<<”后的空格,则为 138 个字节;提交编辑器会截断最后一行而不包含它):
In cut-free Prolog (138 bytes if you remove the space after '<<'; submission editor truncates the last line without it):
红宝石,50 个字符
Ruby, 50 Chars