# Draw an ASCII Grid

(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!

• Sandbox Commented Jun 18, 2021 at 19:05
• Somewhat related Commented Jun 18, 2021 at 19:14
• @DLosc that's generally allowed, so yes Commented Jun 19, 2021 at 0:58

# Python 3, 49 bytes

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


Try it online!

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

• You can shave off two bytes by removing f= from the beginning as anonymous functions are allowed. Commented Jun 18, 2021 at 19:46
• Save two with f-strings - TIO. Commented Jun 18, 2021 at 21:54
• This is some of the hardest to follow Python I've ever seen! Commented Jun 20, 2021 at 18:23

# 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!

# Charcoal, 16 bytes

ＵＯ⊖×³Ｎ⊖⊗θ  |¶--+


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

ＵＯ


Draw a rectangle...

⊖×³Ｎ


... width 3n-1...

⊖⊗θ


... height 2n-1...

  |¶--+


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

# Vyxalrj, 15 bytes

+--|  ½*ẋfḢvḢ


Try it Online!

• Nice use of the split to save some bytes Commented Jun 18, 2021 at 19:24
• Ninja'd Commented Jun 18, 2021 at 19:25
• wow you beat charcoal Commented Jun 18, 2021 at 19:26

# 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

• Well that was fast Commented Jun 18, 2021 at 19:08
• @SlamJammington i am speed Commented Jun 18, 2021 at 19:10
• Save two using ¥, ⁺, and some reordering of operations TIO. Commented Jun 18, 2021 at 21:09
• @JonathanAllan Ah, I had a feeling there had to be some way of getting around the chaining without requiring a µ. thanks! Commented Jun 18, 2021 at 21:12

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.) # 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. # 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! # APL, 44 bytes {1 1↓⊃⍪/,/⍵ ⍵⍴⊂2 3⍴'+--| '}  2 3⍴'+--| ' gives: +-- |  ⍵ ⍵⍴⊂2 3⍴'+--| ' gives n×n tiles, and ⍪/,/ joins them together. For example n = 3:  +--+--+-- | | | +--+--+-- | | | +--+--+-- | | |  And finally 1 1↓⊃ discloses and drops the first column and row.  | | --+--+-- | | --+--+-- | |  Works in both Dyalog APL and GNU APL. Try it online! • Welcome to Code Golf, and nice answer! Commented Mar 4, 2022 at 16:36 • You might want to add a TIO link so others can test your code online. Commented Mar 4, 2022 at 19:43 # Vyxalj, 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

• s/Ports/Beats Commented Jun 18, 2021 at 19:23
• @AaronMiller Looks like I was beaten to golfing it. Commented Jun 18, 2021 at 19:24

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

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


# Pip-l, 2824 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)


# 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


# Python 2 or 3, 4946 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

• Oh nice, haha. In that case, lambda n:([("+--"*n)[1:]," |"*~-n]*n)[1:] should work, as trailing spaces are not required. Commented Jun 19, 2021 at 1:22
• @dingledooper Ah, true. I'm a little disappointed to break the symmetry of it, but bytes is bytes. Commented Jun 19, 2021 at 1:29

# 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  # Pari/GP, 71 bytes n->[print(concat([[" ","--";"|","+"][j+1,t+1]|j<-r]))|t<-r=[2..2*n]%2]  Try it online! Thanks to alephalpha for saving 39 bytes. • 93 bytes. You can use ; to execute multiple statements in a function. ; is not allowed in list comprehensions, but here you can use + because the return value of g is just 0. print1 prints something without a trailing newline. Commented Mar 1, 2022 at 10:35 • Or 71 bytes. Commented Mar 1, 2022 at 10:56 • @alephalpha Thanks! Commented Mar 1, 2022 at 18:54 # Ruby, 46 bytes ->n{(2..n*2).map{|x|[" -"[x%=2]*2]*n*"|+"[x]}}  Try it online! # 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  • 1↓[2]⍉1↓⍉ Commented Jun 20, 2021 at 12:04 • @Razetime thanks but it is 1 character shorter but 3 bytes longer.. Commented Jun 20, 2021 at 12:57 • All the APL symbols + ASCII have a single byte encoding :) Commented Jun 20, 2021 at 13:14 # brainfuck, 841 bytes ->>>>>+++++[>+++++>+++++<<-],[+>-[<-->-]>[>>>>+>+<<<<<-]>>>,]+++[<+++>-]+[-<<<<<[->+>+>+<<<]>>>[<<<+>>>-]<<<+]->>>>>->->->>>[<+++++++++[>+>+>+<<<-]>>>>>>]->--[<<+>>-]>[<<+++++>>-]<<[[->>>>>+<<<<<]<]<+[--[>>>>>>>..>.<<<<<<<<-]+<<<<<[[<<<+>>>-]<<<[->>>+>+>+<<<<<]>>>>+>-<[>[[-]<[-]<->>>>>[+++++++++>>>>>]<<<<<+>>>>]<[--<<<<<]]>[++>>>--<<[>>>>>]>]<<]<<+]++++++++++.[-]-<+[--[->>++[-<<<+[->>[-]<<[<+>-]<[>+>>+<<<-]<<<<+]-[>>>>>]<<<+[--[>[>+>-<<-]>[<+>-]>+[->->>>..>.<<<<<]>+[->>>+++++++++++++..--.-----------<<<]<<<<-]+<<<<<[[<<<+>>>-]<<<[->>>+>+>+<<<<<]>>>>+>-<[>[[-]<[-]<->>>>>[+++++++++>>>>>]<<<<<+>>>>]<[--<<<<<]]>[++>>>--<<[>>>>>]>]<<]<<+]>[>+>+<<-]>[<+>-]>[->>>>+++++++++++++..-------------<<<<]<++++++++++.[-]<]<<]+<<<<<[[<<+>>-]<<[->>+>>+>+<<<<<]>>>>+>-<[>[[-]<[-]<<->>>>>[+++++++++>>>>>]<<<<<+>>>>>]<[--<<<<<]]>[++>>--<[>>>>>]>]<<<]<<+]  edit1, thank you for the suggestion by Radvylf Programs this work by using 5 cells per digit, 1 for original number, 1 for row number, 1 for column number, and 2 for verifying that help to make the number system work. Try it online! also, I edited the first one(adding "+" version) to get a better result. here # brainfuck(adding "+" version) ,273 198 bytes -[>+>+>+<<<-]>>> >>> >>> >+++++[<+++<+++++<<++++++<++>>>>>-]<<[<+++++>-]>[<+++>-]<<-<++<<<[->>>..>.<<<<]>>.<<<<[->[->+>+<<]>[<+>-]>[>>>>..--.++<<<<-]>>>>..<<<.<<<[->+>+<<]>[<+>-]>[>>..>.<<<-]>.<<<<]  Try it online! • You'll need to use , for input according to site rules, using +s in the header isn't an allowed input format as far as I know. Commented Mar 1, 2022 at 15:12 # 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. # 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! # Stax, 14 bytes ╬^û╗i§║╜P◙ƒ═½\  Run and debug it Similar to the Jelly answer. # [PowerShell], 53 bytes &{$c=$n-1;@(" |"*$c)*$n -join "n$(""--+""*$c)--n"}  Try it online! • Nice answer. Could you provide some way of running this online, maybe via tio.run so others can test it out? Commented Jun 21, 2021 at 19:15 # sed 4.2.2-n, 78 bytes h #copy PATTERN space to HOLD space s/./&&/g #duplicate s/../&|/g #add | after char pairs s/.$//      #delete last char
x;s/.//;x   #decrement HOLD space