31
\$\begingroup\$

Given two positive integers, W and H, output an ASCII-art box whose border is made of slashes (/ and \) with W "spikes" on the top and bottom edges, and H "spikes" on the left and right edges. The box's interior is filled with spaces.

A "spike" is simply two slashes coming together to form an arrow shape:

/\    \/

/      \
\      /

So the output for W = 4, H = 3 would be

/\/\/\/\
\      /
/      \
\      /
/      \
\/\/\/\/

as there are 4 spikes on the top pointing up, 4 on the bottom pointing down, 3 on the left pointing left, and 3 on the right pointing right.

Here are some other input/output pairs:

W H
[spiky slash box]

1 1
/\
\/

1 2
/\
\/
/\
\/

2 1
/\/\
\/\/

2 2
/\/\
\  /
/  \
\/\/

1 3
/\
\/
/\
\/
/\
\/

3 1
/\/\/\
\/\/\/

2 3
/\/\
\  /
/  \
\  /
/  \
\/\/

3 2
/\/\/\
\    /
/    \
\/\/\/

10 1
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/

10 2
/\/\/\/\/\/\/\/\/\/\
\                  /
/                  \
\/\/\/\/\/\/\/\/\/\/

4 5
/\/\/\/\
\      /
/      \
\      /
/      \
\      /
/      \
\      /
/      \
\/\/\/\/

No lines in the output should have leading or trailing spaces. There may optionally be one trailing newline.

The shortest code in bytes wins.

\$\endgroup\$
1
  • \$\begingroup\$ Can someone who javascripts make a stack snippet for this? \$\endgroup\$ Dec 23 '17 at 20:17

34 Answers 34

1
2
0
\$\begingroup\$

Jelly, 19 bytes

JṖḊ
Ḥ+þ/ị⁾\/⁶Ǧ€Ç¦Y

Try it online!

How?

JṖḊ - Link 1, middle indexes: list
J   - range of length [1,2,3,...,length-1,length]
 Ṗ  - pop             [1,2,3,...,length-1]
  Ḋ - dequeue           [2,3,...,length-1]

Ḥ+þ/ị⁾\/⁶Ǧ€Ç¦Y - Main link: list of numbers [w,h]
Ḥ               - double (vectorises) -> [2*w,2*h]
   /            - reduce by:
  þ             -   outer product with:
 +              -     addition ->  [[1,2,...,w],[2,3,...,1+w],...,[h+1,h+2,...,h+w]]
     ⁾\/        - literal ['\','/']
    ị           - index into (vectorises) -> ["/\/\.../\","\/\/...\/",...,"\/\/...\/"]
             ¦  - sparse application:
            Ç   - ...to indexes: call the last link as a monad (the middle ones)
          ¦€    - ...of: sparse application for €ach:
         Ç      -        ...to indexes: call the last link as a monad (the middle ones)
        ⁶       -        ...of: a space character (blank out the middle entries)
              Y - join with newlines
                - as a full program: implicit print
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog), 30 bytes

' \/'[(⌽⌈⊖)3|((-0=⌊)*+)/↑⍳2×⎕]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

><>, 180 Bytes

0&0\
a*+>i68*-:0(?v$
/)\^0&1v?)0&~/
? 0 v:$<
/ \:<-1oo'/\'
\~$:@2*0&\
'/'1>&o1->a\ /'/'>o$:2*>1-:1)?\~$&1=?\
'\'0//?(3:o/?/'\'/     \o*48  /      \
/?)0\ &:&1=/;
\  \:~
'/'/\<oo'\'
\$\endgroup\$
0
\$\begingroup\$

Excel VBA, 89 Bytes

Anonymous VBE immediate window function that takes input as w from [A1] and h from [B1] and then outputs to the VBE immediate window.

a=[Rept("/\",A1)]:?a:s=space(2*[A1-1]):For i=1To[B1-1]:?"\"s"/":?"/"s"\":Next:?Mid(a,2)"/
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.