16
\$\begingroup\$

(Based on this closed challenge, related)

Given an integer n > 1, draw a grid composed of the characters: "-" "+" and "|". However, since I want the grid to look nice, the horizontal component should be 2 wide.

Trailing spaces for each line are not required.

Examples:

n = 2

  | 
--+--
  | 

n = 3

  |  | 
--+--+--
  |  | 
--+--+--
  |  |

This is , so the shortest answer in bytes wins!

\$\endgroup\$
3
  • \$\begingroup\$ Sandbox \$\endgroup\$
    – Underslash
    Jun 18 at 19:05
  • \$\begingroup\$ Somewhat related \$\endgroup\$
    – Shaggy
    Jun 18 at 19:14
  • \$\begingroup\$ @DLosc that's generally allowed, so yes \$\endgroup\$
    – Underslash
    Jun 19 at 0:58

21 Answers 21

10
\$\begingroup\$

Python 3, 49 bytes

lambda n:f'\n{"--+"*~-n}--\n'.join(['  |'*~-n]*n)

Try it online!

\$\endgroup\$
8
\$\begingroup\$

Vyxal rj, 15 bytes

`+--|  `½*ẋfḢvḢ

Try it Online!

Port of hyper-neutrino's Jelly answer

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Nice use of the split to save some bytes \$\endgroup\$
    – Underslash
    Jun 18 at 19:24
  • \$\begingroup\$ Ninja'd \$\endgroup\$ Jun 18 at 19:25
  • 1
    \$\begingroup\$ wow you beat charcoal \$\endgroup\$
    – Makonede
    Jun 18 at 19:26
7
\$\begingroup\$

Charcoal, 16 bytes

UO⊖׳N⊖⊗θ  |¶--+

Try it online! Link is to verbose version of code. Explanation:

UO

Draw a rectangle...

⊖׳N

... width 3n-1...

⊖⊗θ

... height 2n-1...

  |¶--+

... filled with | and --+ on alternate lines.

\$\endgroup\$
7
\$\begingroup\$

Jelly, 14 bytes

“+--“|  ”ẋḊ¥⁺€

Try it online!

-2 bytes thanks to Jonathan Allan

Returns a matrix of characters.

“+--“|  ”ẋḊ¥⁺€  Main Link
“+--“|  ”       ["+--", "|  "]
           ¥    Apply last two to a list and X
         ẋ      Repeat the list X times
          Ḋ     Remove the first element
             €  To each row,
            ⁺   Do the above
\$\endgroup\$
4
  • \$\begingroup\$ Well that was fast \$\endgroup\$ Jun 18 at 19:08
  • 7
    \$\begingroup\$ @SlamJammington i am speed \$\endgroup\$
    – hyper-neutrino
    Jun 18 at 19:10
  • 1
    \$\begingroup\$ Save two using ¥, , and some reordering of operations TIO. \$\endgroup\$ Jun 18 at 21:09
  • 1
    \$\begingroup\$ @JonathanAllan Ah, I had a feeling there had to be some way of getting around the chaining without requiring a µ. thanks! \$\endgroup\$
    – hyper-neutrino
    Jun 18 at 21:12
7
\$\begingroup\$

Python 3, 70 62 60 58 bytes

lambda n:f"-{'-+-'*~-n}-\n".join([f" {' | '*~-n} \n"]*n)

Thanks to @Underslash, @Jonathan Allan for removing 4 bytes.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ You can shave off two bytes by removing f= from the beginning as anonymous functions are allowed. \$\endgroup\$
    – Underslash
    Jun 18 at 19:46
  • 1
    \$\begingroup\$ Save two with f-strings - TIO. \$\endgroup\$ Jun 18 at 21:54
  • \$\begingroup\$ This is some of the hardest to follow Python I've ever seen! \$\endgroup\$ Jun 20 at 18:23
7
\$\begingroup\$

JavaScript (ES6),  67  66 bytes

Recursive.

f=(n,k=n*2*(n*=3)-n)=>k--?` -|+
`[k%n?~-k%n%3&2|k/n&1:4]+f(n,k):''

Try it online!


JavaScript (ES6),  61  58 bytes

Saved 3 bytes thanks to @EliteDaMyth

The straightforward way.

n=>`${s='  |'[R='repeat'](--n)}
${'--+'[R](n)}--
`[R](n)+s

Try it online!

\$\endgroup\$
1
6
\$\begingroup\$

Vyxal j, 19 bytes

:`+--``|  `"*$ẋfḢvḢ

Try it Online!

Ports my Jelly answer. I'll stop for a bit to give other people time to answer.

:`+--``|  `"*$ẋfḢvḢ  Full Program
:                    Duplicate X
 `+--`               Push "+--"
      `|  `          Push "|  "
           "         Pair into ["+--", "|  "]
            *        Repeat each X times
             $ẋ      Repeat that X times
               f     Flatten
                Ḣ    Remove first row
                 vḢ  Remove first column from each

j                    flag: output top of stack separated by newlines
\$\endgroup\$
2
6
\$\begingroup\$

Excel, 62 bytes

=LEFT(REPT(IF(MOD(SEQUENCE(2*A1-1),2),"  |","--+"),A1),3*A1-1)

With gridlines turned off, it looks the same as the single string with line feeds.

\$\endgroup\$
5
\$\begingroup\$

Ruby, 37 bytes

->n{['  |'*~-n]*n*"
#{['--']*n*?+}
"}

Try it online!

Ruby, 37 bytes

A very similar approach, also 37 bytes:

->n{['  |'*~-n]*n*"
#{'--+'*~-n}--
"}

Try it online!

\$\endgroup\$
1
5
\$\begingroup\$

Haskell, 42 bytes

n%a=tail$[1..n]>>a
f n=n%[n%"+--",n%"|  "]

Try it online!

Defines f :: Integer -> [String].

[1..n]>>a means: for each element of [1..n], cycle through all elements of a. Then, tail removes the first one. So 3%"+--" evaluates to "--+--+--".

% does double duty, first acting on Strings to make the two grid rows, and then on [String] to transform them into whole grid.

(The tempting 39-byte f n|g<-tail.([1..n]>>)=g[g"+--",g"| "] is thus foiled by the monomorphism restriction.)

\$\endgroup\$
5
\$\begingroup\$

Pip -l, 28 24 23 bytes

@>*@>:["+--""|  "]XaRLa

Try it online!

Explanation

Same idea as my Python answer, implemented a bit more directly:

      ["+--""|  "]       List of two strings
                  Xa     Repeat each string n times
                    RLa  Repeat the whole list n times
   @>:                   All but the first item of the list (: forces @> to be
                           lower precedence than X and RL)
@>*                      All but the first item of each string in the list
                         Autoprint (implicit), one string per line (-l flag)
\$\endgroup\$
4
\$\begingroup\$

C (clang), 71 bytes

l;f(n){for(l=n*3,n=n*2*l-l;--n;)putchar(n%l?"|  +--"[n/l%2*3+n%3]:10);}

Try it online!

l=n*3        line length (\n included)
n=n*2*l-l    total characters count
for(..;--n)  while we iterate backwards :
n%l?..:10    > put a return at end of line or..
"|  +--"[n/l%2*3+n%3] > take the proper char to put
\$\endgroup\$
4
\$\begingroup\$

J, 34 bytes

'| +-'{~<:@+:$#:@6(,:+&2)@$~*&3-1:

Try it online!

Consider 3 as input:

  • #:@6...$~*&3-1: Six in binary #:@6 expands to 1 1 0 and "input times 3 minus 1" is 8 *&3-1:, so we extend 1 1 0 that far:

    1 1 0 1 1 0 1 1
    
  • (,:+&2)@ Stack that with 2 added to itself:

    1 1 0 1 1 0 1 1
    3 3 2 3 3 2 3 3
    
  • <:@+:$ Now extend that to length "2 times input minus 1":

    1 1 0 1 1 0 1 1
    3 3 2 3 3 2 3 3
    1 1 0 1 1 0 1 1
    3 3 2 3 3 2 3 3
    1 1 0 1 1 0 1 1
    
  • '| +-'{~ And use that to index into the grid characters:

      |  |  
    --+--+--
      |  |  
    --+--+--
      |  |  
    
\$\endgroup\$
4
\$\begingroup\$

6502 machine code, Apple 2, 61 bytes

I got a bit carried away...

$130 - $F3 = $3D (61 decimal) bytes

Listing:

0000F3r 1               n := $f0
0000F3r 1               c := $f1
0000F3r 1               ; X register contains n
0000F3r 1  86 F0        grid:   stx n
0000F5r 1  CA                   dex
0000F6r 1  86 F1                stx c
0000F8r 1  A6 F1        @loop: ldx c
0000FAr 1  A9 rr        @cloop: lda #<coldiv
0000FCr 1  A0 rr                ldy #>coldiv
0000FEr 1  20 rr rr             jsr msgoutay
000101r 1  CA                   dex
000102r 1  D0 F6                bne @cloop
000104r 1  20 8E FD             jsr CROUT
000107r 1  C6 F0                dec n
000109r 1  F0 19                beq @done
00010Br 1  A6 F1                ldx c
00010Dr 1  A9 rr        @rloop: lda #<rowdiv
00010Fr 1  A0 rr                ldy #>rowdiv
000111r 1  20 rr rr             jsr msgoutay
000114r 1  CA                   dex
000115r 1  D0 F6                bne @rloop
000117r 1  A9 rr                lda #<rowend
000119r 1  A0 rr                ldy #>rowend
00011Br 1  20 rr rr             jsr msgoutay
00011Er 1  20 8E FD             jsr CROUT
000121r 1  4C rr rr             jmp @loop
000124r 1  60           @done:  rts
000125r 1               
000125r 1               
000125r 1  A0 A0 FC 00  coldiv:    ASZ "  |"
000129r 1  AD AD AB 00  rowdiv:   ASZ "--+"
00012Dr 1  AD AD 00     rowend:  ASZ "--"
000130r 1 
              

Example run called with

    ldx #4
    jsr grid

Example Test Run

\$\endgroup\$
3
\$\begingroup\$

Python 2 or 3, 49 46 42 bytes

-4 bytes thanks to dingledooper

lambda n:([("+--"*n)[1:],"  |"*~-n]*n)[1:]

Returns a list of lines. Try it online!

Explanation

It's easy to generate this grid:

+--+--+--
|  |  |  
+--+--+--
|  |  |  
+--+--+--
|  |  |  

Then we can trim off the first line as well as the first character of each line.

Since the latest golf, the code works a little differently:

([("+--"*n)[1:],"  |"*~-n]*n)[1:]
   "+--"*n                         # Repeat the horizontal line pattern n times
  (       )[1:]                    # and remove the first character
                "  |"*~-n          # Repeat the other pattern n-1 times
                                   # (trailing spaces aren't required, so
                                   # we don't have to remove anything)
 [             ,         ]         # Put those two strings in a list
                          *n       # Repeat the list n times
(                           )[1:]  # and remove the first element
\$\endgroup\$
2
  • \$\begingroup\$ Oh nice, haha. In that case, lambda n:([("+--"*n)[1:]," |"*~-n]*n)[1:] should work, as trailing spaces are not required. \$\endgroup\$ Jun 19 at 1:22
  • \$\begingroup\$ @dingledooper Ah, true. I'm a little disappointed to break the symmetry of it, but bytes is bytes. \$\endgroup\$
    – DLosc
    Jun 19 at 1:29
3
\$\begingroup\$

APL(Dyalog Unicode), 4629 bytes SBCS

{⍉1↓⍉⊃,/⍵⍴⊂(⍵+⍵-1)3⍴'|  +--'}

Try it on APLgolf!

  • saved 17 thanks to @Razetime tips!

(⍵+⍵-1)3⍴'| +--' we build the entire column e.g.

| 
+--
|  
+--
| 

Then :

⍵⍴⊂    ⍝  n copies of that column 
,/     ⍝  concatenate 
⊃      ⍝  disclose
⍉1↓⍉   ⍝ transpose, drop line, re transpose 
\$\endgroup\$
3
  • \$\begingroup\$ 1↓[2]⍉1↓⍉ \$\endgroup\$
    – Razetime
    Jun 20 at 12:04
  • \$\begingroup\$ @Razetime thanks but it is 1 character shorter but 3 bytes longer.. \$\endgroup\$
    – AZTECCO
    Jun 20 at 12:57
  • 1
    \$\begingroup\$ All the APL symbols + ASCII have a single byte encoding :) \$\endgroup\$
    – Razetime
    Jun 20 at 13:14
2
\$\begingroup\$

Retina 0.8.2, 43 bytes

.+
$*
1
$_¶$_¶
1
+--
T`-+` |`¶.+¶
1A`
%`^.

Try it online! Explanation:

.+
$*

Convert to unary.

1
$_¶$_¶

Create 2n rows of n 1s.

1
+--

Replace each 1 with +--.

T`-+` |`¶.+¶

Change those to | on alternate lines.

1A`

Delete the first line of --+s.

%`^.

Delete the leading +s or |s on each line.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 46 bytes

->n{(2..n*2).map{|x|[" -"[x%=2]*2]*n*"|+"[x]}}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 93 bytes

n=>('  |'[r='repeat'](--n)+'  0')[r](n+1).slice(0,-1).split(0).join('\n'+'--+'[r](n)+'--\n');

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Stax, 14 bytes

╬^û╗i§║╜P◙ƒ═½\

Run and debug it

Similar to the Jelly answer.

\$\endgroup\$
1
\$\begingroup\$

[PowerShell], 53 bytes

&{$c=$n-1;@("  |"*$c)*$n -join "`n$(""--+""*$c)--`n"}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Nice answer. Could you provide some way of running this online, maybe via tio.run so others can test it out? \$\endgroup\$
    – Underslash
    Jun 21 at 19:15

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.