代码高尔夫:谢尔宾斯基三角
挑战
按字符数输出最短的代码,以输出由以下 ASCII 三角形组成的 N 次迭代的谢尔宾斯基三角形的 ASCII 表示:
/\
/__\
输入是单个正数。
测试用例
Input:
2
Output:
/\
/__\
/\ /\
/__\/__\
Input:
3
Output:
/\
/__\
/\ /\
/__\/__\
/\ /\
/__\ /__\
/\ /\ /\ /\
/__\/__\/__\/__\
Input:
5
Output:
/\
/__\
/\ /\
/__\/__\
/\ /\
/__\ /__\
/\ /\ /\ /\
/__\/__\/__\/__\
/\ /\
/__\ /__\
/\ /\ /\ /\
/__\/__\ /__\/__\
/\ /\ /\ /\
/__\ /__\ /__\ /__\
/\ /\ /\ /\ /\ /\ /\ /\
/__\/__\/__\/__\/__\/__\/__\/__\
/\ /\
/__\ /__\
/\ /\ /\ /\
/__\/__\ /__\/__\
/\ /\ /\ /\
/__\ /__\ /__\ /__\
/\ /\ /\ /\ /\ /\ /\ /\
/__\/__\/__\/__\ /__\/__\/__\/__\
/\ /\ /\ /\
/__\ /__\ /__\ /__\
/\ /\ /\ /\ /\ /\ /\ /\
/__\/__\ /__\/__\ /__\/__\ /__\/__\
/\ /\ /\ /\ /\ /\ /\ /\
/__\ /__\ /__\ /__\ /__\ /__\ /__\ /__\
/\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\
/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\
代码计数包括输入/输出(即完整程序)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
J
46 个字符,从标准输入读取。
\n
总是分隔句子,这使得它不可能放入 S3 中(只有 54 个字符可供使用)。 S4 162 有点大,所以我填充了它以适合。偶然的是,/\
是一个合法的副词。 ☺J
46 characters, reading from stdin.
\n
always delimits sentences, which made it impossible to fit inside S3 (only 54 characters to play with). S4 is a bit big at 162, so I padded it to fit. Serendipitously,/\
is a legal adverb. ☺抱歉我来晚了。这是基于 A. Rex 的 Perl 解决方案:
Sorry I'm late. This is based on A. Rex's Perl solution:
Golfscript - 46
Golfscript - 47
Golfscript - 48
Golfscript - 51
与我较短的 python (和 ruby )答案相同的算法
Golfscript - 78 这个
与我的较长 python 解决方案相同的算法
有重要的换行符
Golfscript - 46
Golfscript - 47
Golfscript - 48
Golfscript - 51
Same algorithm as my shorter python ( and ruby ) answer
Golfscript - 78
Same algorithm as my longer python solution
This one has significant newlines
Go,273 个字符
空格都很重要。
使用
gofmt sierpinski-3.go 取消最小化 | perl -p -e's/\t/ /g'
:我得到了关于围棋高尔夫的一个很好的提示 这里。
Go, 273 characters
Whitespace is all significant.
Unminized with
gofmt sierpinski-3.go | perl -p -e's/\t/ /g'
:I got a good hint for Go golf here.
Python - 102
Python - 105
Python - 109
Python2.6 - 120
Python - 102
Python - 105
Python - 109
Python2.6 - 120
Perl,82 笔画
此版本不再打印尾随换行符。只有第一个换行符是必要的:
如果允许命令行切换,那么根据传统的 Perl 高尔夫评分,这是 77+3 杆(第一个换行符是字面意思):
如果您发现改进,请随时编辑我的答案。
Perl, 82 strokes
This version no longer prints a trailing newline. Only the first newline is necessary:
If command-line switches are allowed, then by traditional Perl golf scoring, this is 77+3 strokes (the first newline is literal):
Please feel free to edit my answer if you find an improvement.
Haskell,
153149137125118112个字符:使用尾递归:
更早版本,@118 个字符:
使用(刚刚被弃用!)n+k 模式 保存了 4 个字符。
我喜欢它的可读性,即使是压缩形式。
编辑:旧主
Haskell,
153149137125118112 characters:Using tail recursion:
earlier version, @118 characters:
Using the (justly deprecated!) n+k pattern saved 4 characters.
I like how it comes out halfway readable even in compressed form.
edit:old main
Perl
删除换行符后为 94 个字符。
Perl
94 characters when newlines are removed.
红宝石 — 85
101 chars —
/\
-modified solution from Rosetta CodeRuby — 85
101 chars —
/\
-modified solution from Rosetta CodePython,135 个字符
Python, 135 chars
MATLAB - 64 个字符(脚本版本)
这假设您已在工作区中定义了变量
N
:MATLAB - 78 个字符(m 文件函数版本)
将
N
传递为函数s
的参数:MATLAB - 64 characters (script version)
This assumes that you have the variable
N
already defined in your workspace:MATLAB - 78 characters (m-file function version)
Pass
N
as an argument to the functions
:C
与 Perl 答案,但重量较重,有 131 个必要字符。
我以为
write(1,…)
是 UNIX API,但这似乎在 Windows 上也可以正常编译和运行。如果将
char
替换为int
,它会保存一个字符并且仍然有效,但其合法性值得怀疑。C
Same algorithm as the Perl answer, but weighing in heavier, at 131 necessary characters.
I thought
write(1,…)
was UNIX API, but this seems to compile and run fine on Windows too.If you replace
char
byint
, it saves one character and still works, but it's of questionable legality.徽标(不完全符合要求):47 个字符
我仅使用 http://www.calormen.com 对此进行了测试/Logo/ 所以我不知道它是否是便携式的。它不符合要求,但徽标肯定是这里合适的语言吗? :) 我喜欢在编写徽标时比 Golfscript 和 J 少一个字符。
Logo (not exactly following the requirements): 47 characters
I tested this only with http://www.calormen.com/Logo/ so I don't know if it's portable. It doesn't follow the requirements, but surely logo must be the appropriate language here? :) I love that at the time of writing logo is one character short of equalling golfscript and J.
Lua,139 个字符
Lua, 139 characters
诺夫,542
$ nroff -rn=5 文件.n
Nroff, 542
$ nroff -rn=5 file.n
F#,225 个字符
F#, 225 chars
Clojure:174 个字符
从上面其他人那里窃取的算法。
其中 38 个字符是括号。 :(
Clojure: 174 characters
Algorithm stolen from others above.
38 of those characters are parentheses. : (
Python,120 个字符(递归解决方案)
我开始在 @fserb 留下的地方加上绿色......
Python, 120 characters (recursive solution)
I started putting on the green where @fserb left off...
GolfScript (
4544 个字符)与 gnibbler 的解决方案类似。我最初的尝试已经很相似了,然后我参考了他的一些想法。
GolfScript (
4544 chars)Similar to gnibbler's solution. My initial attempt was already quite similar, and then I looked at his and borrowed some ideas.
Python,186 个字符(UNIX 行终止)
Python, 186 chars (UNIX line termination)
Prolog,811 个字符
Prolog, 811 Chars