代码高尔夫:蜘蛛网

发布于 2024-08-09 14:05:02 字数 1456 浏览 4 评论 0原文

挑战

按字符数计算最短的代码,输出蜘蛛网,其环数等于用户的输入。

蜘蛛网是通过重建中心环来启动的:

   \_|_/
  _/   \_
   \___/
   / | \

然后添加等于用户输入数量的环。环是由 \ / |_ 组成的“蜘蛛圈”的另一层,并包裹着中心圆。

输入始终保证为单个正整数。

测试用例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

行至春深 2024-08-16 14:05:02

Perl,164 个字符

195 184 171 167 164

print@o=((map{$z=_ x($x=1+$N-$_);$"x$x." /"x$_."\\$z|$z/".'\ 'x$_.$/}0..($N=<>)),
"_/"x++$N."   ".'\_'x$N.$/);
y'/\\'\/',@o||y#_# #,$t++||y#_ # _#,print while$_=pop@o

第一个语句打印出蜘蛛网的上半部分。第二条语句使用音译操作来创建上半部分的反射。

下一篇的重量接近 314 个字符(有效代码),但更符合本季的精神。

           ;               "
         Tr                 Ic
        K|                   |t
       Re                     aT
       ",                     "H
       av                     e
        A:                    -
     )H AL                   LO  W
    ee  N"                   ,"  En
   jo    y_                 Yo    ur
   _      C&&             y";     ##
   &I      ();           $N=      1+
   <>;      $,=  $/;@O  =((     map 
    $" x($   X=$N-$_). ${   f}x$_.$
        B.${U}x$X.$P.${U}x$X.$
    F.${b}x$_,0..$N-1),${g}x$N.(${S}
   x3).${c}x$N);sub I{($F,$B,$U, $P)
  =qw         (/ \\ _ |);;         ${
 S}=        " ";$f=$S.$F;$g=$       U.
 $F     ;$b=$B.$S;$c=$B.${U};}@{    P}=
@{     O};  while($_=pop@{P}  ){     @{
 P}    ||   y:_: :;$spooky++  ||    0|
  0    ||   y@_ @ _@;y:/:8:;   ;    ;
   ;   ;;   y:\\:/:;y:8:\\:;  @O   =
    (  @O    ,$_);}print@O;   q{
       Do     !Discuss:Rel    ig
       io       n,Politi      cs
        ,&                   &T
        heG                 rea
          tP              ump
            ki           n}

http://www.ascii-art.de/ascii/s 致敬/spider.txt

我手动构建了蜘蛛形状的代码,但请参阅Acme::AsciiArtinator CPAN 上的模块,以帮助自动化(或至少半自动化)任务。

Perl, 164 chars

195 184 171 167 164

print@o=((map{$z=_ x($x=1+$N-$_);$"x$x." /"x$_."\\$z|$z/".'\ 'x$_.$/}0..($N=<>)),
"_/"x++$N."   ".'\_'x$N.$/);
y'/\\'\/',@o||y#_# #,$t++||y#_ # _#,print while$_=pop@o

First 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.

           ;               "
         Tr                 Ic
        K|                   |t
       Re                     aT
       ",                     "H
       av                     e
        A:                    -
     )H AL                   LO  W
    ee  N"                   ,"  En
   jo    y_                 Yo    ur
   _      C&&             y";     ##
   &I      ();           $N=      1+
   <>;      $,=  $/;@O  =((     map 
    $" x($   X=$N-$_). ${   f}x$_.$
        B.${U}x$X.$P.${U}x$X.$
    F.${b}x$_,0..$N-1),${g}x$N.(${S}
   x3).${c}x$N);sub I{($F,$B,$U, $P)
  =qw         (/ \\ _ |);;         ${
 S}=        " ";$f=$S.$F;$g=$       U.
 $F     ;$b=$B.$S;$c=$B.${U};}@{    P}=
@{     O};  while($_=pop@{P}  ){     @{
 P}    ||   y:_: :;$spooky++  ||    0|
  0    ||   y@_ @ _@;y:/:8:;   ;    ;
   ;   ;;   y:\\:/:;y:8:\\:;  @O   =
    (  @O    ,$_);}print@O;   q{
       Do     !Discuss:Rel    ig
       io       n,Politi      cs
        ,&                   &T
        heG                 rea
          tP              ump
            ki           n}

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.

岁月如刀 2024-08-16 14:05:02

Golfscript - 124 个字符

所有空白都很重要!如果您不小心在末尾添加了换行符,输出末尾将会有一个额外的 _

~):@,{@\:&-:0' ': *& '/':/+*'\\':~'_':
0*.'|':|\/~ +&*n}%
/+@*   ~
+@*n ~+@*


@/ +*n@,{):& *@&-:( ~+*/[
 ](!=&*.|\~/ +(*n}%

Golfscript - 129 chars

~):@,{@\:&-:0' ': *&' /'*'\\':~'_':
0*.'|'\'/'~ +&*n}%'_/'@*   '\_'@*n ~+@*


@'/ '*n@,{):& *@&-:( ~+*'/'[
 ](!=&*.'|'\~'/ '(*n}%

Golfscript - 133 chars

~):@,{@\:&-:0' ': *&' /'*'\\':~'_':
0*.'|'\'/'~ +&*n}%'_/'@*3 *'\_'@*n' \\'@*3
*@'/ '*n@,{):& *@&-:( ~+*'/''_ '1/(!=&*.'|'\~'/ '(*n}%

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 output

~):@,{@\:&-:0' ': *& '/':/+*'\\':~'_':
0*.'|':|\/~ +&*n}%
/+@*   ~
+@*n ~+@*


@/ +*n@,{):& *@&-:( ~+*/[
 ](!=&*.|\~/ +(*n}%

Golfscript - 129 chars

~):@,{@\:&-:0' ': *&' /'*'\\':~'_':
0*.'|'\'/'~ +&*n}%'_/'@*   '\_'@*n ~+@*


@'/ '*n@,{):& *@&-:( ~+*'/'[
 ](!=&*.'|'\~'/ '(*n}%

Golfscript - 133 chars

~):@,{@\:&-:0' ': *&' /'*'\\':~'_':
0*.'|'\'/'~ +&*n}%'_/'@*3 *'\_'@*n' \\'@*3
*@'/ '*n@,{):& *@&-:( ~+*'/''_ '1/(!=&*.'|'\~'/ '(*n}%
冷弦 2024-08-16 14:05:02

Python - 212 个字符

n=input()+1;b,f,p,u,s='\/|_ '
a=[s*(n-i)+' /'*i+b+u*(n-i)+p+u*(n-i)+f+'\ '*i+s*(n-i)for
i in range(n)]
print"\n".join(a+['_/'*n+s*3+'\_'*n,' \\'*n+u*3+'/ '*n]+[x[::-1]for
x in a[:0:-1]]+[a[0][::-1].replace(u,s)])

Python - 212 chars

n=input()+1;b,f,p,u,s='\/|_ '
a=[s*(n-i)+' /'*i+b+u*(n-i)+p+u*(n-i)+f+'\ '*i+s*(n-i)for
i in range(n)]
print"\n".join(a+['_/'*n+s*3+'\_'*n,' \\'*n+u*3+'/ '*n]+[x[::-1]for
x in a[:0:-1]]+[a[0][::-1].replace(u,s)])
最佳男配角 2024-08-16 14:05:02

Perl:161 个字符

请注意,此代码包括源代码中的起始 Web。 (末尾的双反斜杠令人遗憾。早期版本没有这个。)

$_='
 \_|_/
_/   \_
 \___/ 
 /_|_\\';
for$x(1..<>){
s|(.\S).*([/\\].)|$1
amp;$2|g;
s|\\(.*)/| \\_$1_/

Perl:161 个字符

请注意,此代码包括源代码中的起始 Web。 (末尾的双反斜杠令人遗憾。早期版本没有这个。)

/
amp;\\ |;
s|(\s+)\K/(.*).$| \\
amp;/$1 /_$2_\\|
}
s|_(?=.*$)| |g;
print

$_ 中的空格很重要(当然),但其余的都不是。如果您有一个改进此问题的小建议,请随时编辑我的代码。例如,Kinopiko 很好地削减了 6 个字符!

根据你计算命令行开关的方式,这可能会更短(如果我能正确计算的话,按照通常的 Perl 高尔夫规则是 154):

#!perl -ap
$_='
 \_|_/
_/   \_
 \___/ 
 /_|_\\';
s|(.\S).*([/\\].)|$1
amp;$2|g,
s|\S(.*).| \\_$1_/

Perl:161 个字符

请注意,此代码包括源代码中的起始 Web。 (末尾的双反斜杠令人遗憾。早期版本没有这个。)

$_='
 \_|_/
_/   \_
 \___/ 
 /_|_\\';
for$x(1..<>){
s|(.\S).*([/\\].)|$1
amp;$2|g;
s|\\(.*)/| \\_$1_/

Perl:161 个字符

请注意,此代码包括源代码中的起始 Web。 (末尾的双反斜杠令人遗憾。早期版本没有这个。)

/
amp;\\ |;
s|(\s+)\K/(.*).$| \\
amp;/$1 /_$2_\\|
}
s|_(?=.*$)| |g;
print

$_ 中的空格很重要(当然),但其余的都不是。如果您有一个改进此问题的小建议,请随时编辑我的代码。例如,Kinopiko 很好地削减了 6 个字符!

根据你计算命令行开关的方式,这可能会更短(如果我能正确计算的话,按照通常的 Perl 高尔夫规则是 154):

/
amp;\\ |,
s|(\s+)\K/(.*).$| \\
amp;/$1 /_$2_\\|while$F[0]--;
s|_(?=.*$)| |g

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.)

$_='
 \_|_/
_/   \_
 \___/ 
 /_|_\\';
for$x(1..<>){
s|(.\S).*([/\\].)|$1
amp;$2|g;
s|\\(.*)/| \\_$1_/

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.)

/
amp;\\ |;
s|(\s+)\K/(.*).$| \\
amp;/$1 /_$2_\\|
}
s|_(?=.*$)| |g;
print

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):

#!perl -ap
$_='
 \_|_/
_/   \_
 \___/ 
 /_|_\\';
s|(.\S).*([/\\].)|$1
amp;$2|g,
s|\S(.*).| \\_$1_/

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.)

$_='
 \_|_/
_/   \_
 \___/ 
 /_|_\\';
for$x(1..<>){
s|(.\S).*([/\\].)|$1
amp;$2|g;
s|\\(.*)/| \\_$1_/

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.)

/
amp;\\ |;
s|(\s+)\K/(.*).$| \\
amp;/$1 /_$2_\\|
}
s|_(?=.*$)| |g;
print

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):

/
amp;\\ |,
s|(\s+)\K/(.*).$| \\
amp;/$1 /_$2_\\|while$F[0]--;
s|_(?=.*$)| |g
西瑶 2024-08-16 14:05:02

Vb.net、Windows 控制台、推断、严格、显式开启。

Microsoft Word 表示 442 个字符,不含空格。

也许可以进一步减少,但这是我的最后一次更新(尝试 #2

Module z
Sub Main()
    Dim i = CInt(Console.ReadLine), j = i + 1, h = j * 2 + 1, w = h * 2, z = "_", b = " "

    For y = 0 To h
        For x = 0 To w
            Dim l = (x + y Mod 2 + i Mod 2) Mod 2, u = j + y, e = j - y, k = h + e, o = x = h Or x = h - 1
            Console.Write(If(x = h, If(y = j, b, If(y = j + 1, z, "|")), "") & If(x = w, vbLf, If(y = j, If(x Mod 2 = 0 = (x < h), If(o, b, z), If(x < h, "/", "\")), If(x < k And x > u Or (x < u And x > k Or o) And y < h, z, If(x = k Or (x < u And y < j And x > e Or x > u And y > j And x < w + e) And l = 0, "/", If(x = u Or (x > k And y < j And x < h + u Or x < k And y > j And x > y - j - 1) And l = 1, "\", b))))))
        Next
    Next
End Sub
End Module

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)

Module z
Sub Main()
    Dim i = CInt(Console.ReadLine), j = i + 1, h = j * 2 + 1, w = h * 2, z = "_", b = " "

    For y = 0 To h
        For x = 0 To w
            Dim l = (x + y Mod 2 + i Mod 2) Mod 2, u = j + y, e = j - y, k = h + e, o = x = h Or x = h - 1
            Console.Write(If(x = h, If(y = j, b, If(y = j + 1, z, "|")), "") & If(x = w, vbLf, If(y = j, If(x Mod 2 = 0 = (x < h), If(o, b, z), If(x < h, "/", "\")), If(x < k And x > u Or (x < u And x > k Or o) And y < h, z, If(x = k Or (x < u And y < j And x > e Or x > u And y > j And x < w + e) And l = 0, "/", If(x = u Or (x > k And y < j And x < h + u Or x < k And y > j And x > y - j - 1) And l = 1, "\", b))))))
        Next
    Next
End Sub
End Module
丢了幸福的猪 2024-08-16 14:05:02

Python:240 个字符

这里没什么太棘手的;仅逐行打印 - 298 280 271 266 265 261 260 254 240 个字符(忽略最后 2 个换行符)

u,b,f,s,a='_\/ |'
m=input()+1
print'\n'.join([(m-x)*s+x*' /'+b+(m-x)*u+a+(m-x)*u+f+x*'\ 'for x in
  range(0,m)]+['_/'*m+s*3+'\_'*m+'\n'+(s+b)*m+u*3+'/ '*m]+[x*s+(m-x)*
  ' \\'+f+x*u+a+x*u+b+(m-x)*'/ 'for x in range(1,m)] + [s*m+f+s*m+a+s*m+b])

Python: 240 Characters

Nothing too tricky here; just printing line by line - 298 280 271 266 265 261 260 254 240 characters (ignore the last 2 line breaks)

u,b,f,s,a='_\/ |'
m=input()+1
print'\n'.join([(m-x)*s+x*' /'+b+(m-x)*u+a+(m-x)*u+f+x*'\ 'for x in
  range(0,m)]+['_/'*m+s*3+'\_'*m+'\n'+(s+b)*m+u*3+'/ '*m]+[x*s+(m-x)*
  ' \\'+f+x*u+a+x*u+b+(m-x)*'/ 'for x in range(1,m)] + [s*m+f+s*m+a+s*m+b])
反目相谮 2024-08-16 14:05:02

卢阿,290

n=...s=string r=s.reverse g=s.gsub a="\\|/"j=(" /"):rep(n+1)..a..("\\ "):rep(n+1) k=j o=k
l=n*4+7 for i=1,n+1 do k=g(k,"^(.- )/(.-)|(.*)\\(.-)$","%1%2_|_%3%4")o=k..o end
o=o..r(o)print((g(g(g(g(r(g(o:sub(1,l),"_"," ")..o:sub(l+1)),j,g(j," ","_")),("."):rep(l),"%1\n"),a,"   "),r(a),"___")))

Lua, 290

n=...s=string r=s.reverse g=s.gsub a="\\|/"j=(" /"):rep(n+1)..a..("\\ "):rep(n+1) k=j o=k
l=n*4+7 for i=1,n+1 do k=g(k,"^(.- )/(.-)|(.*)\\(.-)$","%1%2_|_%3%4")o=k..o end
o=o..r(o)print((g(g(g(g(r(g(o:sub(1,l),"_"," ")..o:sub(l+1)),j,g(j," ","_")),("."):rep(l),"%1\n"),a,"   "),r(a),"___")))
温柔少女心 2024-08-16 14:05:02

Ruby1.9 - 181 个字符

n=gets.to_i+1;s=' '
a=0.upto(n-1).map{|i|s*(j=n-i)+' /'*i+?\\+?_*j+'|'+?_*j+?/+'\ '*i+s*j}
d=a.reverse.map{|x|x.reverse};d[-1].tr!?_,s
puts a,'_/'*n+s*3+'\_'*n,' \\'*n+?_*3+'/ '*n,d

Ruby1.8 - 185 个字符
的一些改进

n=gets.to_i+1;s=' '
u='_';a=0.upto(n-1).map{|i|s*(j=n-i)+' /'*i+'\\'+u*j+'|'+u*j+'/'+'\ '*i+s*j}
d=a.reverse.map{|x|x.reverse}
d[-1].tr!u,s;puts a,'_/'*n+s*3+'\_'*n,' \\'*n+u*3+'/ '*n,d

JRL Ruby - 207 个字符
Ruby 似乎对“\”有一些特殊的规则

n=eval(gets)+1
b,f,p,u,s='\/|_ '.split""
a=0.upto(n-1).map{|i|s*(j=n-i)+' /'*i+b+u*j+"|"+u*j+f+"\\ "*i+s*j}
puts a,'_/'*n+s*3+'\_'*n,' \\'*n+u*3+'/ '*n,a[1..-1].reverse.map{
|x|x.reverse},a[0].reverse.tr(u,s)

Ruby1.9 - 181 chars

n=gets.to_i+1;s=' '
a=0.upto(n-1).map{|i|s*(j=n-i)+' /'*i+?\\+?_*j+'|'+?_*j+?/+'\ '*i+s*j}
d=a.reverse.map{|x|x.reverse};d[-1].tr!?_,s
puts a,'_/'*n+s*3+'\_'*n,' \\'*n+?_*3+'/ '*n,d

Ruby1.8 - 185 chars
Some improvements from JRL

n=gets.to_i+1;s=' '
u='_';a=0.upto(n-1).map{|i|s*(j=n-i)+' /'*i+'\\'+u*j+'|'+u*j+'/'+'\ '*i+s*j}
d=a.reverse.map{|x|x.reverse}
d[-1].tr!u,s;puts a,'_/'*n+s*3+'\_'*n,' \\'*n+u*3+'/ '*n,d

Ruby - 207 chars
Ruby seems to have some peculiar rules about the "\"

n=eval(gets)+1
b,f,p,u,s='\/|_ '.split""
a=0.upto(n-1).map{|i|s*(j=n-i)+' /'*i+b+u*j+"|"+u*j+f+"\\ "*i+s*j}
puts a,'_/'*n+s*3+'\_'*n,' \\'*n+u*3+'/ '*n,a[1..-1].reverse.map{
|x|x.reverse},a[0].reverse.tr(u,s)
神经大条 2024-08-16 14:05:02

Ruby1.8, 179

使用 ruby -n 运行

n=$_.to_i+1
u,s,c=%w{_ \  \ \\}
z=(1..n).map{|i|k=n-i
s*i+c*k+'/'+u*i+'|'+u*i+"\\"+'/ '*k+s*i}
y=z.reverse.map{|a|a.reverse}
z[-1].tr!u,s
puts y,'_/'*n+s*3+'\_'*n,c*n+u*3+'/ '*n,z

在下面的第一次尝试中,仅生成一个象限(我选择左下角),然后镜像两次似乎是个好主意获得整个网络。但是 gnibbler 得到了更好的结果,生成两个象限(上半部分),然后生成而不是修补内部区域。所以我修改了我的,最初也生成了另一个下象限,仅镜像一次,并且还将最里面的行留在镜像之外,这与其他条目会聚。

<罢工>红宝石,241

n=$_.to_i+1
m=2*n+1
u,s,b,f=%w{_ \  \\ /}
z=(0..n).map{|i|s*i+(s+b)*(n-i)+(i==0?u:f)+u*i}
q=z.reverse.map{|a|a.tr f+b,b+b+f}
q[n].gsub!' ','_'
q[n][m-1]=s
z=(q+z).map{|a|a+'|'+a.reverse.tr(f+b,b+b+f)}
z[n][m]=z[n+1][m]=s
z[m].gsub!u,s
puts z

Ruby1.8, 179

Run with ruby -n

n=$_.to_i+1
u,s,c=%w{_ \  \ \\}
z=(1..n).map{|i|k=n-i
s*i+c*k+'/'+u*i+'|'+u*i+"\\"+'/ '*k+s*i}
y=z.reverse.map{|a|a.reverse}
z[-1].tr!u,s
puts y,'_/'*n+s*3+'\_'*n,c*n+u*3+'/ '*n,z

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, 241

n=$_.to_i+1
m=2*n+1
u,s,b,f=%w{_ \  \\ /}
z=(0..n).map{|i|s*i+(s+b)*(n-i)+(i==0?u:f)+u*i}
q=z.reverse.map{|a|a.tr f+b,b+b+f}
q[n].gsub!' ','_'
q[n][m-1]=s
z=(q+z).map{|a|a+'|'+a.reverse.tr(f+b,b+b+f)}
z[n][m]=z[n+1][m]=s
z[m].gsub!u,s
puts z
岛徒 2024-08-16 14:05:02

C,573 个字符

显然,它甚至不在运行中,考虑到字符数。 573 数字只是我的 Windows 计算机上的文件大小,因此可能可以计算几个 ctrl-M。另一方面,也许 573 低估了它,因为我为了节省空间而抛弃了所有#include,招致了编译器的愤怒,该死的警告!

但是,嘿,这是我第一次尝试其中一个,尝试以更紧凑的方式重新表达它无疑是一个很好的实践。

#define B puts("");
#define K '\\'+'/'
#define F '_'+' '
#define P(s) putchar(s);
#define I int
c(I s,I f){if(s){P(f)c(s-1,f);P(f)}else P('|')} 
w(I lw,I s,I k,I f){if(s){P(' ')P(k)w(lw,s-1,k,f);P(K-k)P(' ')}else{P(K-k)c(1+lw,f);P(k)}}
h(I g,I s,I k,I f){I i;for(i=-1;i<g;++i)P(' ')w(g,s,k,f);} 
t(I g,I s){if(s)t(g+1,s-1);h(g,s,'/','_');B}
b(I g,I s){h(g,s,'\\',s?'_':' ');B;if(s)b(g+1,s-1);}
m(I s,I k,I f){if(s){P(f)P(k)m(s-1,k,f);P(K-k)P(f)}else{P(F-f)P(F-f)P(F-f)}}
main(I ac,char*av[]){I s;s=atoi(av[1]);t(0,s);m(1+s,'/','_');B;m(1+s,'\\',' ');B;b(0,s);}

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.

#define B puts("");
#define K '\\'+'/'
#define F '_'+' '
#define P(s) putchar(s);
#define I int
c(I s,I f){if(s){P(f)c(s-1,f);P(f)}else P('|')} 
w(I lw,I s,I k,I f){if(s){P(' ')P(k)w(lw,s-1,k,f);P(K-k)P(' ')}else{P(K-k)c(1+lw,f);P(k)}}
h(I g,I s,I k,I f){I i;for(i=-1;i<g;++i)P(' ')w(g,s,k,f);} 
t(I g,I s){if(s)t(g+1,s-1);h(g,s,'/','_');B}
b(I g,I s){h(g,s,'\\',s?'_':' ');B;if(s)b(g+1,s-1);}
m(I s,I k,I f){if(s){P(f)P(k)m(s-1,k,f);P(K-k)P(f)}else{P(F-f)P(F-f)P(F-f)}}
main(I ac,char*av[]){I s;s=atoi(av[1]);t(0,s);m(1+s,'/','_');B;m(1+s,'\\',' ');B;b(0,s);}
剑心龙吟 2024-08-16 14:05:02

Python,340 - 309 - 269 - 250 个字符

我认为仍有改进的空间。

s=input()+1
f,b="/ ","\\"
r=range(s)
for i in r:w="_"*(s-i);print" "*(s+(i>=1)-i)+(f*i)[:-1]+b+w+"|"+w+"/"+"\ "*i
print"_/"*s+" "*3+"\_"*s+"\n"+" \\"*s+"_"*3+f*s
for i in r[::-1]:u="_ "[i<1]*(s-i);print" "*(s-i+(i>=1))+("\ "*i)[:-1]+"/"+u+"|"+u+b+f*i

-

Python(替代版本),250 - 246 个字符

s=input()+1;r=range(s);c="/","\\";y="/ ","\\ "
def o(i,r):u="_ "[i<1 and r]*(s-i);print" "*(s+(i>=1)-i)+(y[r]*i)[:-1]+c[r<1]+u+"|"+u+c[r]+(y[r<1]*i)[:-1]
for i in r:o(i,0)
print"_/"*s+" "*3+"\_"*s+"\n"+" \\"*s+"_"*3+"/ "*s
for i in r[::-1]:o(i,1)

Python, 340 - 309 - 269 - 250 characters

Still room for improvement I think.

s=input()+1
f,b="/ ","\\"
r=range(s)
for i in r:w="_"*(s-i);print" "*(s+(i>=1)-i)+(f*i)[:-1]+b+w+"|"+w+"/"+"\ "*i
print"_/"*s+" "*3+"\_"*s+"\n"+" \\"*s+"_"*3+f*s
for i in r[::-1]:u="_ "[i<1]*(s-i);print" "*(s-i+(i>=1))+("\ "*i)[:-1]+"/"+u+"|"+u+b+f*i

-

Python (alternative version), 250 - 246 characters

s=input()+1;r=range(s);c="/","\\";y="/ ","\\ "
def o(i,r):u="_ "[i<1 and r]*(s-i);print" "*(s+(i>=1)-i)+(y[r]*i)[:-1]+c[r<1]+u+"|"+u+c[r]+(y[r<1]*i)[:-1]
for i in r:o(i,0)
print"_/"*s+" "*3+"\_"*s+"\n"+" \\"*s+"_"*3+"/ "*s
for i in r[::-1]:o(i,1)
三生殊途 2024-08-16 14:05:02

Python 和 Ruby 差不多*

我宁愿继续上面简要提到 Python 与 Ruby 的评论线程,但我需要格式化才能做到这一点。 Smashery 当然很优雅,但不必担心:事实证明,从某种角度来看,Python 和 Ruby 的竞争非常接近。我回过头来,在我参加的八场代码高尔夫中将 Python 与 Ruby 进行了比较。

    Challenge       Best Python             Best Ruby

    The Wave          161                    99
    PEMDAS          no python entry       (default victory?)
    Seven Segs        160                   175
    Banknotes          83 (beat Perl!)       87  
    Beehive           144                   164
    RPN (no eval)     111 (157)              80 (107)
    Cubes             249                   233
    Webs              212                   181

    Victories           3                     4 (5?)

所以这个问题肯定还没有解决,而且最近当 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.

    Challenge       Best Python             Best Ruby

    The Wave          161                    99
    PEMDAS          no python entry       (default victory?)
    Seven Segs        160                   175
    Banknotes          83 (beat Perl!)       87  
    Beehive           144                   164
    RPN (no eval)     111 (157)              80 (107)
    Cubes             249                   233
    Webs              212                   181

    Victories           3                     4 (5?)

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.

昇り龍 2024-08-16 14:05:02

dc - 262

dc (OpenBSD) 中的“直接”解决方案。虽然不是竞争者,但总是很有趣。 的换行符

[lcP1-d0<A]sA?sN[lK32sclAxRlNlK-l1scd0!=ARl3PlKl0sclAxRl9PlKlAxRl4PlNlK-
l2scd0!=AAPR]sW95s0124s9[ /]s1[\\ ]s292s347s4lN[dsKlWx1-d0<L]dsLx
[\\_][   ][_/][lN[rdPr1-d0<L]dsLxRRPlNlLxRR]dsBxAP[/ ][_ _][ \\]lBxAP[ \\]s1
[/ ]s247s392s41[dsKlWx1+dlN>L]dsLx32s032s9lNsKlWx

“可读性”示例输出

$ dc web.dc  
3
   \___|___/
   /\__|__/\ 
  / /\_|_/\ \ 
_/_/_/   \_\_\_
 \ \ \_ _/ / / 
  \ \/_|_\/ / 
   \/__|__\/ 
   /       \

dc - 262

A "straightforward" solution in dc (OpenBSD). Not a contender, but it is always fun. Line breaks for "readability"

[lcP1-d0<A]sA?sN[lK32sclAxRlNlK-l1scd0!=ARl3PlKl0sclAxRl9PlKlAxRl4PlNlK-
l2scd0!=AAPR]sW95s0124s9[ /]s1[\\ ]s292s347s4lN[dsKlWx1-d0<L]dsLx
[\\_][   ][_/][lN[rdPr1-d0<L]dsLxRRPlNlLxRR]dsBxAP[/ ][_ _][ \\]lBxAP[ \\]s1
[/ ]s247s392s41[dsKlWx1+dlN>L]dsLx32s032s9lNsKlWx

sample output

$ dc web.dc  
3
   \___|___/
   /\__|__/\ 
  / /\_|_/\ \ 
_/_/_/   \_\_\_
 \ \ \_ _/ / / 
  \ \/_|_\/ / 
   \/__|__\/ 
   /       \
久伴你 2024-08-16 14:05:02

Perl

通过内联子例程缩短了 264 个字符。

perl -E'$"="";($i=<>)++;@r=map{$p=$i-$_;@d=(" "x$_,(" ","\\")x$p,"/","_"x$_);($d="@d")=~y:\\/:/\\:;@d=reverse@d;$d.="|@d"}1..$i;say for reverse@r;$_=$r[0];y: _|:_  :;s:.(.*)\\.*/(.*).:$1_/   \\_$2:;say;y: _\\/:_ /\\:;say;$r[-1]=~y:_: :;say for grep{y:\\/:/\\:}@r;'

扩展以提高可读性。

perl -E'
  $"="";
  ($i=<>)++;
  @r=map{
    $p=$i-$_;
    @d=(
      " "x$_,
      (" ","\\")x$p,
      "/",
      "_"x$_
    );
    ($d="@d")=~y:\\/:/\\:;
    @d=reverse@d;
    $d.="|@d"
  }1..$i;
  say for reverse@r;
  $_=$r[0];
  y: _|:_  :;
  s:.(.*)\\.*/(.*).:$1_/   \\_$2:;
  say;
  y: _\\/:_ /\\:;
  say;
  $r[-1]=~y:_: :;
  say for grep{y:\\/:/\\:}@r;
'

这是我最小化之前的代码:

#! /opt/perl/bin/perl
use 5.10.1;

($i=<>)++;
$"=""; #" # This is to remove the extra spaces for "@d"

sub d(){
  $p=$i-$_;
  " "x$_,(" ","\\")x$p,"/","_"x$_
}

sub D(){
 @d=d;
 ($d="@d")=~y:\\/:/\\:; # swap '\' for '/'
 @d=reverse@d;
 $d.="|@d"
}

@r = map{D}1..$i;

say for reverse@r; # print preceding lines

# this section prints the middle two lines
$_=$r[0];
y: _|:_  :;
s:.(.*)\\.*/(.*).:$1_/   \\_$2:;
say;
y: _\\/:_ /\\:;
say;

$r[-1]=~y:_: :; # remove '_' from last line
say for grep{y:\\/:/\\:}@r; # print following lines

Perl 264 chars

shortened by in-lining the subroutines.

perl -E'$"="";($i=<>)++;@r=map{$p=$i-$_;@d=(" "x$_,(" ","\\")x$p,"/","_"x$_);($d="@d")=~y:\\/:/\\:;@d=reverse@d;$d.="|@d"}1..$i;say for reverse@r;$_=$r[0];y: _|:_  :;s:.(.*)\\.*/(.*).:$1_/   \\_$2:;say;y: _\\/:_ /\\:;say;$r[-1]=~y:_: :;say for grep{y:\\/:/\\:}@r;'

Expanded to improve readability.

perl -E'
  $"="";
  ($i=<>)++;
  @r=map{
    $p=$i-$_;
    @d=(
      " "x$_,
      (" ","\\")x$p,
      "/",
      "_"x$_
    );
    ($d="@d")=~y:\\/:/\\:;
    @d=reverse@d;
    $d.="|@d"
  }1..$i;
  say for reverse@r;
  $_=$r[0];
  y: _|:_  :;
  s:.(.*)\\.*/(.*).:$1_/   \\_$2:;
  say;
  y: _\\/:_ /\\:;
  say;
  $r[-1]=~y:_: :;
  say for grep{y:\\/:/\\:}@r;
'

This is the code before I minimized it:

#! /opt/perl/bin/perl
use 5.10.1;

($i=<>)++;
$"=""; #" # This is to remove the extra spaces for "@d"

sub d(){
  $p=$i-$_;
  " "x$_,(" ","\\")x$p,"/","_"x$_
}

sub D(){
 @d=d;
 ($d="@d")=~y:\\/:/\\:; # swap '\' for '/'
 @d=reverse@d;
 $d.="|@d"
}

@r = map{D}1..$i;

say for reverse@r; # print preceding lines

# this section prints the middle two lines
$_=$r[0];
y: _|:_  :;
s:.(.*)\\.*/(.*).:$1_/   \\_$2:;
say;
y: _\\/:_ /\\:;
say;

$r[-1]=~y:_: :; # remove '_' from last line
say for grep{y:\\/:/\\:}@r; # print following lines
ˉ厌 2024-08-16 14:05:02
(&)=(++) --9
f 0=[" \\_|_/","_/   \\_"," \\___/"," / | \\"] --52
f(n+1)=[s&h&u&"|"&u&g]&w(f n)&[s&g&s&"|"&s&h]where[a,b,c,d,e]=" _/\\|";[g,h]=["/","\\"];y=n+2;[u,s]=[r y b,r y a];p f s n x=let(a,b)=span(/=s)x in a&f b;i=dropWhile(==a);w[]=[];w[x]=[s&h&i(p(map(\x->if x==a then b else x))c d x)&g];w(l:e)|n==y*2-1=x%h:z|n>y=x&" "%" \\":z|n==y="_/"%"\\_":z|n<y=r(y-n)a&"\\ "%" /":z where n=length e;z=w e;x=r(n+1-y)a&g;(%)=(&).(&i l) --367
r=replicate --12
main=interact$unlines.f.read --29

Haskell 条目的重量为 469 个字符。我确信还有很大的改进空间。

祝你阅读它好运:)

这是一个更具可读性的版本。虽然自此版本以来发生了一些变化

spider 0=[" \\_|_/","_/   \\_"," \\___/"," / | \\"]
spider n=(s++"\\"++u++"|"++u++"/"):w m(spider(n-1))++[s++"/"++s++"|"++s++"\\"]
    where
        [a,b,c,d,e]=" _/\\|"
        [m,y]=[y*2,n+1]
        x=r y
        [u,s]=[x b,x a]
        t a b=map(\x->if x==a then b else x)
        p f s n x=let(a,b)=span(/=s)x;(c,d)=span(/=n)b in a++f c++d
        i=dropWhile(==a)
        w _[]=[]
        w _[x]=[s++"\\"++i(p(t a b)c d x)++"/"]
        w(a+1)(l:e) |a==m-1=wrapline x l"\\":z
                    |a>y=wrapline(x++" ")l" \\":z
                    |a==y=wrapline"_/"l"\\_":z
                    |a<y=wrapline(r(y-a)' '++"\\ ")l" /":z
            where
                z=w a e
                x=r(a+1-y)' '++"/"
                wrapline b l a=b++i l++a
r=replicate
main=interact$unlines.spider.read
(&)=(++) --9
f 0=[" \\_|_/","_/   \\_"," \\___/"," / | \\"] --52
f(n+1)=[s&h&u&"|"&u&g]&w(f n)&[s&g&s&"|"&s&h]where[a,b,c,d,e]=" _/\\|";[g,h]=["/","\\"];y=n+2;[u,s]=[r y b,r y a];p f s n x=let(a,b)=span(/=s)x in a&f b;i=dropWhile(==a);w[]=[];w[x]=[s&h&i(p(map(\x->if x==a then b else x))c d x)&g];w(l:e)|n==y*2-1=x%h:z|n>y=x&" "%" \\":z|n==y="_/"%"\\_":z|n<y=r(y-n)a&"\\ "%" /":z where n=length e;z=w e;x=r(n+1-y)a&g;(%)=(&).(&i l) --367
r=replicate --12
main=interact$unlines.f.read --29

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

spider 0=[" \\_|_/","_/   \\_"," \\___/"," / | \\"]
spider n=(s++"\\"++u++"|"++u++"/"):w m(spider(n-1))++[s++"/"++s++"|"++s++"\\"]
    where
        [a,b,c,d,e]=" _/\\|"
        [m,y]=[y*2,n+1]
        x=r y
        [u,s]=[x b,x a]
        t a b=map(\x->if x==a then b else x)
        p f s n x=let(a,b)=span(/=s)x;(c,d)=span(/=n)b in a++f c++d
        i=dropWhile(==a)
        w _[]=[]
        w _[x]=[s++"\\"++i(p(t a b)c d x)++"/"]
        w(a+1)(l:e) |a==m-1=wrapline x l"\\":z
                    |a>y=wrapline(x++" ")l" \\":z
                    |a==y=wrapline"_/"l"\\_":z
                    |a<y=wrapline(r(y-a)' '++"\\ ")l" /":z
            where
                z=w a e
                x=r(a+1-y)' '++"/"
                wrapline b l a=b++i l++a
r=replicate
main=interact$unlines.spider.read
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文