开发者

Code Golf: Spider webs

开发者 https://www.devze.com 2022-12-10 07:04 出处:网络
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.

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:
            \________|________/
            /\_______|_______/\
           / /\______|______/\ \
          / / /\_____|_____/\ \ \
         / / / /\____|____/\ \ \ \
        / / / / /\___|___/\ \ \ \ \
       / / / / / /\__|__/\ \ \ \ \ \
      / / / / / / /\_|_/\ \ \ \ \ \ \
    _/_/_/_/_/_/_/_/   \_\_\_\_\_\_\_\_
     \ \ \ \ \ \ \ \___/ / / / / / / /
      \ \ \ \ \ \ \/_|_\/ / / / / / /
       \ \ \ \ \ \/__|__\/ / / / / /
        \ \ \ \ \/___|___\/ / / / /
         \ \ \ \/____|____\/ / / /
          \ \ \/_____|___开发者_JAVA技巧__\/ / /
           \ \/______|______\/ /
            \/_______|_______\/
            /        |        \

Code count includes input/output (i.e full program).


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.


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}%


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


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$&$2|g;
s|\\(.*)/| \\_$1_/$` /$&\\ |;
s|(\s+)\K/(.*).$| \\$&/$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$&$2|g,
s|\S(.*).| \\_$1_/$` /$&\\ |,
s|(\s+)\K/(.*).$| \\$&/$1 /_$2_\\|while$F[0]--;
s|_(?=.*$)| |g


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


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


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),"___")))


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)


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


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);}


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)


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.


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
   \___|___/
   /\__|__/\ 
  / /\_|_/\ \ 
_/_/_/   \_\_\_
 \ \ \_ _/ / / 
  \ \/_|_\/ / 
   \/__|__\/ 
   /       \


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


(&)=(++) --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
0

精彩评论

暂无评论...
验证码 换一张
取 消