代码高尔夫:蜘蛛网
挑战
按字符数计算最短的代码,输出蜘蛛网,其环数等于用户的输入。
蜘蛛网是通过重建中心环来启动的:
\_|_/
_/ \_
\___/
/ | \
然后添加等于用户输入数量的环。环是由 \ / |
和 _
组成的“蜘蛛圈”的另一层,并包裹着中心圆。
输入始终保证为单个正整数。
测试用例
Input
1
Output
\__|__/
/\_|_/\
_/_/ \_\_
\ \___/ /
\/_|_\/
/ | \
Input
4
Output
\_____|_____/
/\____|____/\
/ /\___|___/\ \
/ / /\__|__/\ \ \
/ / / /\_|_/\ \ \ \
_/_/_/_/_/ \_\_\_\_\_
\ \ \ \ \___/ / / / /
\ \ \ \/_|_\/ / / /
\ \ \/__|__\/ / /
\ \/___|___\/ /
\/____|____\/
/ | \
Input:
7
Output:
\________|________/
/\_______|_______/\
/ /\______|______/\ \
/ / /\_____|_____/\ \ \
/ / / /\____|____/\ \ \ \
/ / / / /\___|___/\ \ \ \ \
/ / / / / /\__|__/\ \ \ \ \ \
/ / / / / / /\_|_/\ \ \ \ \ \ \
_/_/_/_/_/_/_/_/ \_\_\_\_\_\_\_\_
\ \ \ \ \ \ \ \___/ / / / / / / /
\ \ \ \ \ \ \/_|_\/ / / / / / /
\ \ \ \ \ \/__|__\/ / / / / /
\ \ \ \ \/___|___\/ / / / /
\ \ \ \/____|____\/ / / /
\ \ \/_____|_____\/ / /
\ \/______|______\/ /
\/_______|_______\/
/ | \
代码计数包括输入/输出(即完整程序)。
The challenge
The shortest code by character count to output a spider web with rings equal to user's input.
A spider web is started by reconstructing the center ring:
\_|_/
_/ \_
\___/
/ | \
Then adding rings equal to the amount entered by the user. A ring is another level of a "spider circles" made from \ / |
and _
, and wraps the center circle.
Input is always guaranteed to be a single positive integer.
Test cases
Input
1
Output
\__|__/
/\_|_/\
_/_/ \_\_
\ \___/ /
\/_|_\/
/ | \
Input
4
Output
\_____|_____/
/\____|____/\
/ /\___|___/\ \
/ / /\__|__/\ \ \
/ / / /\_|_/\ \ \ \
_/_/_/_/_/ \_\_\_\_\_
\ \ \ \ \___/ / / / /
\ \ \ \/_|_\/ / / /
\ \ \/__|__\/ / /
\ \/___|___\/ /
\/____|____\/
/ | \
Input:
7
Output:
\________|________/
/\_______|_______/\
/ /\______|______/\ \
/ / /\_____|_____/\ \ \
/ / / /\____|____/\ \ \ \
/ / / / /\___|___/\ \ \ \ \
/ / / / / /\__|__/\ \ \ \ \ \
/ / / / / / /\_|_/\ \ \ \ \ \ \
_/_/_/_/_/_/_/_/ \_\_\_\_\_\_\_\_
\ \ \ \ \ \ \ \___/ / / / / / / /
\ \ \ \ \ \ \/_|_\/ / / / / / /
\ \ \ \ \ \/__|__\/ / / / / /
\ \ \ \ \/___|___\/ / / / /
\ \ \ \/____|____\/ / / /
\ \ \/_____|_____\/ / /
\ \/______|______\/ /
\/_______|_______\/
/ | \
Code count includes input/output (i.e full program).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
Perl,164 个字符
195184171167164第一个语句打印出蜘蛛网的上半部分。第二条语句使用音译操作来创建上半部分的反射。
下一篇的重量接近 314 个字符(有效代码),但更符合本季的精神。
向 http://www.ascii-art.de/ascii/s 致敬/spider.txt
我手动构建了蜘蛛形状的代码,但请参阅Acme::AsciiArtinator CPAN 上的模块,以帮助自动化(或至少半自动化)任务。
Perl, 164 chars
195184171167164First statement prints out the top half of the spider web. Second statement uses transliteration operations to create a reflection of the top half.
This next one weighs in closer to 314 chars (of productive code), but is more in the spirit of the season.
Hat tip to http://www.ascii-art.de/ascii/s/spider.txt
I constructed the spider shaped code by hand, but see the Acme::AsciiArtinator module on CPAN for help with automating (or at least semi-automating) the task.
Golfscript - 124 个字符
所有空白都很重要!如果您不小心在末尾添加了换行符,输出末尾将会有一个额外的
_
Golfscript - 129 chars
Golfscript - 133 chars
Golfscript - 124 chars
All whitespace is significant! If you accidently add a newline to the end there will be an extra
_
at the end of the outputGolfscript - 129 chars
Golfscript - 133 chars
Python - 212 个字符
Python - 212 chars
Perl:161 个字符
请注意,此代码包括源代码中的起始 Web。 (末尾的双反斜杠令人遗憾。早期版本没有这个。)
$_
中的空格很重要(当然),但其余的都不是。如果您有一个改进此问题的小建议,请随时编辑我的代码。例如,Kinopiko 很好地削减了 6 个字符!根据你计算命令行开关的方式,这可能会更短(如果我能正确计算的话,按照通常的 Perl 高尔夫规则是 154):
Perl: 161 characters
Note that this code includes the starting web in the source. (The doubled backslash at the end is a shame. An earlier version didn't have that.)
The whitespace within
$_
is significant (of course), but none of the rest is. If you have a minor suggestion that improves this, please feel free to just edit my code. For example, Kinopiko has nicely shaved off 6 characters!Depending on how you count command-line switches, this might be shorter (154 by usual Perl golf rules if I can count correctly):
Vb.net、Windows 控制台、推断、严格、显式开启。
Microsoft Word 表示 442 个字符,不含空格。
也许可以进一步减少,但这是我的最后一次更新(尝试 #2)
Vb.net, windows console, Infer, Strict, Explicit ON.
Microsoft word is saying 442 characters without space
It might be possible to reduce it more but this is my last update(try #2)
Python:240 个字符
这里没什么太棘手的;仅逐行打印 -
298 280 271 266 265 261 260 254240 个字符(忽略最后 2 个换行符)Python: 240 Characters
Nothing too tricky here; just printing line by line -
298 280 271 266 265 261 260 254240 characters (ignore the last 2 line breaks)卢阿,290
Lua, 290
Ruby1.9 - 181 个字符
Ruby1.8 - 185 个字符
的一些改进
JRL Ruby - 207 个字符
Ruby 似乎对“\”有一些特殊的规则
Ruby1.9 - 181 chars
Ruby1.8 - 185 chars
Some improvements from JRL
Ruby - 207 chars
Ruby seems to have some peculiar rules about the "\"
Ruby1.8, 179
使用
ruby -n
运行在下面的第一次尝试中,仅生成一个象限(我选择左下角),然后镜像两次似乎是个好主意获得整个网络。但是 gnibbler 得到了更好的结果,生成两个象限(上半部分),然后生成而不是修补内部区域。所以我修改了我的,最初也生成了另一个下象限,仅镜像一次,并且还将最里面的行留在镜像之外,这与其他条目会聚。
<罢工>红宝石,241
Ruby1.8, 179
Run with
ruby -n
In the first attempt below it seemed like a good idea to just generate one quadrant (I chose lower left), and then mirror twice to get the whole web. But gnibbler got better results generating both quadrants (of the top half) and then generating rather than patching up the inner area. So I revised mine to initially generate the other lower quadrant also, mirror only once, and also to leave the innermost row out of the mirror, which kind of converges with the other entry.
Ruby, 241C,573 个字符
显然,它甚至不在运行中,考虑到字符数。 573 数字只是我的 Windows 计算机上的文件大小,因此可能可以计算几个 ctrl-M。另一方面,也许 573 低估了它,因为我为了节省空间而抛弃了所有#include,招致了编译器的愤怒,该死的警告!
但是,嘿,这是我第一次尝试其中一个,尝试以更紧凑的方式重新表达它无疑是一个很好的实践。
C, 573 chars
Obviously it isn't even in the running w/regard to the character count. The 573 number is just the file size on my windows machine, so that probably counts a few ctrl-M's. On the other hand, maybe 573 is under-counting it, since I incurred the wrath of the compiler by jettisoning all the #include's to save space, warnings be damned!
But hey, this is my first time attempting one of these, and it will undoubtedly be good practice to try to re-express it in something more compact.
Python,
340 - 309 - 269- 250 个字符我认为仍有改进的空间。
-
Python(替代版本),
250- 246 个字符Python,
340 - 309 - 269- 250 charactersStill room for improvement I think.
-
Python (alternative version),
250- 246 charactersPython 和 Ruby 差不多*
我宁愿继续上面简要提到 Python 与 Ruby 的评论线程,但我需要格式化才能做到这一点。 Smashery 当然很优雅,但不必担心:事实证明,从某种角度来看,Python 和 Ruby 的竞争非常接近。我回过头来,在我参加的八场代码高尔夫中将 Python 与 Ruby 进行了比较。
所以这个问题肯定还没有解决,而且最近当 gnibbler 开始从双方的角度进入时,这个问题变得更加有趣。 :-)
*
我只计算了功能齐全的条目。Python and Ruby just about even*
I would rather have continued the comment thread above that briefly mentioned Python vs Ruby, but I need formatting to do this. Smashery is certainly classy but doesn't need to worry: it turns out that Python and Ruby are in a pretty close race by one measure. I went back and compared Python to Ruby in the eight code-golf's that I have entered.
So the issue definitely isn't settled and got more interesting recently when gnibbler started entering on both sides. :-)
*
I only counted fully functional entries.dc - 262
dc (OpenBSD) 中的“直接”解决方案。虽然不是竞争者,但总是很有趣。 的换行符
“可读性”示例输出
dc - 262
A "straightforward" solution in
dc
(OpenBSD). Not a contender, but it is always fun. Line breaks for "readability"sample output
Perl
通过内联子例程缩短了 264 个字符。
扩展以提高可读性。
这是我最小化之前的代码:
Perl 264 chars
shortened by in-lining the subroutines.
Expanded to improve readability.
This is the code before I minimized it:
Haskell 条目的重量为 469 个字符。我确信还有很大的改进空间。
祝你阅读它好运:)
这是一个更具可读性的版本。虽然自此版本以来发生了一些变化
Haskell entry weighing in at 469 characters. I'm sure there is a lot of room for improvement.
good luck trying to read it :)
here is a more readable version. Although there have been some changes since this version