23
\$\begingroup\$

The Challenge:

Draw a rectangle of ASCII boxes: []

The Rules:

Takes a width and a height input

You can assume both of these are numbers

Must produce a string with newline characters, \n

Examples:

2, 2:

[][]
[][]

2, 3:

[][]
[][]
[][]

Fewest Bytes wins.

xnor
149k26 gold badges286 silver badges673 bronze badges
asked Jun 2, 2017 at 22:14
\$\endgroup\$
13
  • 2
    \$\begingroup\$ Nice first post! Welcome to PPCG! \$\endgroup\$ Commented Jun 2, 2017 at 22:26
  • 1
    \$\begingroup\$ Can I assume that the numbers are positive? Can there be trailing newlines? \$\endgroup\$ Commented Jun 2, 2017 at 22:27
  • \$\begingroup\$ @dzaima Positive integers, no trailing or leading stuff \$\endgroup\$ Commented Jun 2, 2017 at 23:09
  • \$\begingroup\$ can we print to console or do we need to return the string? \$\endgroup\$ Commented Jun 2, 2017 at 23:10
  • 6
    \$\begingroup\$ what if we literally can't not print trailing newlines? it tends to be good practice to allow one trailing newline \$\endgroup\$ Commented Jun 3, 2017 at 0:53

39 Answers 39

1
2
6
\$\begingroup\$

SOGL, 5 bytes

Ƨ[]*∙

Simple:

Ƨ[] push "[]"
 * multiply horizontally (repeating width times)
 ∙ get an array with input (height) items of that
 implicitly output the array joined with newlines
answered Jun 2, 2017 at 22:23
\$\endgroup\$
5
\$\begingroup\$

Pyth - (削除) 7 (削除ここまで) 5 bytes

-2 bytes by a clever trick thanks to insert_name_here

VE*`Y

Try it here

Explanation:

VE*`Y
V # Loop
 E # <input> number of times
 `Y # String representation of empty list (used to be "[]", but insert_name_here pointed out this shorter alternative)
 * # repeat string implicit input number of times
 # implicit print
answered Jun 2, 2017 at 23:02
\$\endgroup\$
3
  • 4
    \$\begingroup\$ You can save 2 bytes by using `Y (string representation of empty list) instead of "[]". \$\endgroup\$ Commented Jun 3, 2017 at 1:12
  • \$\begingroup\$ @insert_name_here Ingenious!! I updated the answer. Thanks for pointing that out! \$\endgroup\$ Commented Jun 3, 2017 at 2:55
  • 1
    \$\begingroup\$ Came up with this exact code independently. Nicely done. \$\endgroup\$ Commented Jun 3, 2017 at 6:33
4
\$\begingroup\$

Mathematica, 26 bytes

Grid@Table["[]",{#2},{#}]&
answered Jun 2, 2017 at 22:19
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Does a Mathematica Grid object count as "a string with newline characters"? \$\endgroup\$ Commented Jun 4, 2017 at 2:58
4
\$\begingroup\$

05AB1E, 6 bytes

×ばつ,

Try it online!

Explanation

Input takes as height, width

F # height times do
 „[] # push "[]"
 ×ばつ # repeat width times
 , # print with newline
answered Jun 2, 2017 at 22:52
\$\endgroup\$
4
\$\begingroup\$

MATL, 7 bytes

v&DiiX"

Try it online!

Explanation

v % Concatenate the (non-existing) stack contents: gives []
&D % String representation: gives '[]'
ii % Take two inputs
X" % Repeat those numbers of times vertically and horizontally. Implicit display
answered Jun 2, 2017 at 22:54
\$\endgroup\$
4
\$\begingroup\$

C, (削除) 47 (削除ここまで) 46 bytes

f(w,h){for(h*=w;h--;)printf(h%w?"[]":"[]\n");}

or

f(w,h){for(h*=w;h--;)printf("[]%c",h%w?0:10);}

My first code golf attempt, did I miss anything obvious?

answered Jun 3, 2017 at 0:05
\$\endgroup\$
5
  • \$\begingroup\$ There's this for 45, but it has a newline at the beginning: f(w,h){h*=w;while(h--)printf("\n[]"+!(h%w));} \$\endgroup\$ Commented Jun 3, 2017 at 0:40
  • \$\begingroup\$ That only works when width is 2. \$\endgroup\$ Commented Jun 3, 2017 at 0:48
  • \$\begingroup\$ So it does, my mistake \$\endgroup\$ Commented Jun 3, 2017 at 0:48
  • \$\begingroup\$ Great first golf! Welcome to the site! \$\endgroup\$ Commented Jun 3, 2017 at 16:25
  • 1
    \$\begingroup\$ Wouldn't using a for loop shorten the code even more? \$\endgroup\$ Commented Jun 4, 2017 at 8:15
3
\$\begingroup\$

;#+, 197 bytes

>;;;;;;~++++++++:>~;;;;:>~*(-:~<~+-::>-:::<~<-+++++++++~:::<~+-:::>-::*)-::<-::::>-::(;)::>-::*(-:~<~+-::>-:::<~<-+++++++++~:::<~+-:::>-::*)-:<~<;;;;;-+>-:<-:-(-:::~<-:::(~<#<-;;-#~;)-:<#-::<;>-:-)

Try it online! Requires a zero byte after each input number.

I kinda don't know how this works. What I can tell you is that this part of the code:

 *(-:~<~+-::>-:::<~<-+++++++++~:::<~+-:::>-::*)-::<-::::>-::(;)::>-::*(-:~<~+-::>-:::<~<-+++++++++~:::<~+-:::>-::*)

is parsing the input numbers.

answered Jun 2, 2017 at 23:45
\$\endgroup\$
3
\$\begingroup\$

brainfuck, 145 bytes

+++++++++[>++++++++++<-]>+[>+>+<<-]>>++>,>+++++++++[<----->-]<--->>>,>+++++++++[<----->-]<--->++++++++++<[<<<[>+>+<<-]>[<<<.>.>>-]>[<<+>>-]>>.<-]

Try it online!

My first ever code golf! Yay!

The input is in ascii + 48, so in order to do 50, 50 you must input b, b (the ascii letters for 98)

Explanation

+++++++++[>++++++++++<-]>+ Get the opening square bracket into first position
[>+>+<<-] Get it into the second and third position
>>++ Get the third position to be the closing bracket
>
,>+++++++++[<----->-]<--- Get first number into fourth cell
>>>
,>+++++++++[<----->-]<--- Get second number into seventh cell
>++++++++++ get newline into 8th position
<
[ Start our height loop
<<<[>+>+<<-] Get the width into the fifth and sixth positions
>[ Start our width loop at the fifth position
<<<.>. Print the second and third positions
>>-] Decrement the fifth position
>
[<<+>>-] copy the sixth position into the fourth position
>>. print newline
<-]
Okx
16.5k5 gold badges45 silver badges114 bronze badges
answered Jun 4, 2017 at 16:21
\$\endgroup\$
5
  • \$\begingroup\$ Impressive. Welcome to the site! :) \$\endgroup\$ Commented Jun 4, 2017 at 16:31
  • \$\begingroup\$ Why is the input ASCII+48? You can save a lot of bytes by just using ASCII+0 input (possibly linking to the ASCII+48 version for usability) \$\endgroup\$ Commented Jun 7, 2017 at 20:36
  • \$\begingroup\$ I just wanted to meet the criteria for input, @calculatorFeline \$\endgroup\$ Commented Jun 7, 2017 at 20:59
  • \$\begingroup\$ ...Oh right. :| \$\endgroup\$ Commented Jun 7, 2017 at 21:05
  • \$\begingroup\$ I don't know if anyone is interested, but you can rewrite this code using 122 bytes (129 without wrapping) \$\endgroup\$ Commented Dec 15, 2024 at 20:48
2
\$\begingroup\$

J, 12 bytes

'[]'$~],+:@[

Try it online!

Explanation

'[]'$~],+:@[ input: y, x
 +:@[ double y
 ], pair with x
 this gives (x, 2y)
 $~ shape the left argument into the right argument's shape
'[]' 2-length character string

This gives us an x by 2y string of repeating [] characters.

answered Jun 2, 2017 at 22:35
\$\endgroup\$
1
  • \$\begingroup\$ 11 bytes \$\endgroup\$ Commented Jun 3, 2017 at 2:45
2
\$\begingroup\$

Python 2.7, 32 bytes

Full program:

n,m=input()
exec"print'[]'*n;"*m

Try it online!

answered Jun 2, 2017 at 22:38
\$\endgroup\$
2
\$\begingroup\$

Jelly, 7 bytes

ẋ)[]ẋ$Y

A dyadic link returning a list of characters (or a full program printing the result).

Try it online!

How?

ẋ)[]ẋ$Y - Main link: number w, number h e.g. 2, 3
ẋ - repeat w h times [2,2,2]
 $ - last two links as a monad:
 )[] - literal ['[',']'], "[]"
 ẋ - repeat list (vectorises) ["[][]","[][]","[][]"]
 Y - join with newlines "[][]\n[][]\n[][]"
 - if a full program, implicit print
answered Jun 2, 2017 at 23:04
\$\endgroup\$
2
\$\begingroup\$

Retina, 32 bytes

.+
$*
1(?=1*(¶1+))|.
1ドル
G`1
1
[]

Try it online! Takes height and width input on separate lines.

answered Jun 3, 2017 at 0:36
\$\endgroup\$
2
\$\begingroup\$

Python 3, 26 bytes

lambda n,m:('[]'*n+'\n')*m

Try it online!

answered Jun 3, 2017 at 6:05
\$\endgroup\$
2
\$\begingroup\$

V, 7 bytes

Ài[]<esc>ÀÄ

where <esc> is 0x1b.

Try it online!

Explanation

Ài[]<esc> " arg1 times insert []
 ÀÄ " arg2 times duplicate this line
answered Jun 3, 2017 at 7:45
\$\endgroup\$
2
\$\begingroup\$

Ohm, 9 bytes

M"[]"َJ, 

Try it online!

Explanation

M"[]"َJ,
M //Executes code input1 times
 "[]" //Pushes []
 َ //Duplicates [] input2 times
 J //Joins the stack
 , //Prints with a trailing newline
answered Jun 3, 2017 at 15:33
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 25 Bytes

param($w,$h),("[]"*$w)*$h

-3 thanks to Mathias!

answered Jun 2, 2017 at 23:12
\$\endgroup\$
1
  • \$\begingroup\$ You can shorten it to 25 like so: param($w,$h),("[]"*$w)*$h \$\endgroup\$ Commented Jun 4, 2017 at 12:33
2
\$\begingroup\$

APL (Dyalog), 11 bytes

×ばつ

Try it online!

'[]' the string

⍴⍨ cyclically repeated to fill the shape

right argument (rows)

, and

×ばつ twice

the left argument (columns)

answered Jun 8, 2017 at 12:34
\$\endgroup\$
2
\$\begingroup\$

Charcoal, (削除) 8 (削除ここまで) 7 bytes

×ばつ[]Iη

Try it online! Link is to verbose version of code. Takes input in the order height, width. Charcoal's drawing primitives aren't suited to this, so this just takes the easy way out and repeats the [] string appropriately. Explanation:

 N First input as a number
E Map over implcit range
 η Second input
 I Cast to number
 [] Literal string
 ×ばつ Repeat
 Implicitly print on separate lines
answered Jun 3, 2017 at 0:48
\$\endgroup\$
7
  • \$\begingroup\$ Well it has drawing primitives for this but still 8 bytes :P \$\endgroup\$ Commented Jun 3, 2017 at 3:11
  • \$\begingroup\$ @ASCII-only Sorry, I didn't realise that Oblong worked on arbitrary strings. Neat! \$\endgroup\$ Commented Jun 3, 2017 at 8:12
  • \$\begingroup\$ @ASCII-only Oh, and what's the verbose name of the predefined empty string variable? \$\endgroup\$ Commented Jun 3, 2017 at 8:26
  • \$\begingroup\$ It's w, name, greek to verbose \$\endgroup\$ Commented Jun 3, 2017 at 8:28
  • \$\begingroup\$ @ASCII-only Then what am I doing wrong here: Try it online! \$\endgroup\$ Commented Jun 3, 2017 at 9:30
2
\$\begingroup\$

Japt -mR, (削除) 13 (削除ここまで) (削除) 12 (削除ここまで) 5 bytes

Takes input in reverse order

Vç"[]

Try it

  • 1 byte saved thanks to obarakon.
answered Jun 2, 2017 at 23:14
\$\endgroup\$
4
  • \$\begingroup\$ Being a little drunk can sometimes help your programming skills though :P \$\endgroup\$ Commented Jun 2, 2017 at 23:16
  • \$\begingroup\$ @ETHproductions: the very cartoon I was looking for but was too drunk to find! \$\endgroup\$ Commented Jun 3, 2017 at 0:25
  • \$\begingroup\$ Haha, hope you guys are having a fun night. fyi, U*V can be shortened to \$\endgroup\$ Commented Jun 3, 2017 at 2:31
  • 1
    \$\begingroup\$ @obarakon: That's 2 opportunities to work with N last night. Never drink & golf, kids! \$\endgroup\$ Commented Jun 3, 2017 at 13:11
1
\$\begingroup\$

R, 70 bytes

p=paste
function(w,h)p(rep(p(rep('[]',w),collapse=''),h),collapse='
')

Try it online!

Returns an anonymous function that constructs and returns the string.

45 bytes, non-conforming

function(w,h)write(matrix('[]',w,h),'',w,,'')

An anonymous function that prints out the string in the desired format.

Try this online

answered Jun 2, 2017 at 23:25
\$\endgroup\$
1
\$\begingroup\$

Japt, 7 bytes

6 bytes of code, +1 for the -R flag.

VÆç"[]

Doesn't work in the latest version due to a bug with ç, but it does work in commit f619c52. Test it online!

Explanation

VÆ ç"[]
VoX{Uç"[]"} // Ungolfed
 // Implicit: U, V = input integers
VoX{ } // Create the range [0...V) and replace each item X with
 Uç"[]" // U copies of the string "[]".
-R // Join the result with newlines.
 // Implicit: output result of last expression
answered Jun 2, 2017 at 23:24
\$\endgroup\$
1
\$\begingroup\$

Go, 74 bytes

import."strings"
func(x,y int)string{return Repeat(Repeat("[]",x)+"\n",y)}

Try it online!

answered Jun 3, 2017 at 1:30
\$\endgroup\$
1
\$\begingroup\$

QBIC, 14 bytes

[:|?[:|?@[]`';

Explanation:

[:| FOR a = 1 to (read input from cmd line)
? PRINT a newlne
[:| FOR c = 1 to (read input from cmd line)
?@[]` PRINT A$ (containing the box)
'; and inject a semicolon in the compiled QBasic code to suppress newlines

This takes its arguments in the order of #rows, #cols. Output starts with a newline.

answered Jun 3, 2017 at 8:27
\$\endgroup\$
1
\$\begingroup\$

Pyke, 8 bytes

}A;**"[]

Try it here!

Also 8 bytes:

 F2K*"[]

Try it here!

answered Jun 3, 2017 at 15:28
\$\endgroup\$
1
\$\begingroup\$

Bash, 55 bytes

seq $((1ドル*2ドル))|sed s/.*/[]/|tr -d "
"|fold -w $((1ドル*2))

Try it online! Uses the TIO flavor of bash, since I run windows.

answered Jun 2, 2017 at 23:22
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I don't think this works for multi-digit numbers \$\endgroup\$ Commented Jun 3, 2017 at 7:39
  • \$\begingroup\$ @KritixiLithos It should work now \$\endgroup\$ Commented Jun 3, 2017 at 18:45
1
\$\begingroup\$

C#, 78 bytes

(w,h)=>"".PadLeft(h).Replace(" ","".PadLeft(w).Replace(" ","[]")+'\n').Trim();

Run in C# Pad

This is shorter than with for-loops and I'm not aware of any function in C# which can repeat with less code.

answered Jun 3, 2017 at 19:59
\$\endgroup\$
1
\$\begingroup\$

CJam, 10 bytes

l~"[]"*N+*
answered Jun 4, 2017 at 18:02
\$\endgroup\$
0
1
\$\begingroup\$

JavaScript (ES6), (削除) 43 (削除ここまで) 36 bytes

From the comments, a trailing newline is now permitted.

w=>h=>("[]".repeat(w)+`
`).repeat(h)

Try it

f=
w=>h=>("[]".repeat(w)+`
`).repeat(h)
oninput=_=>o.innerText=f(+i.value)(+j.value);o.innerText=f(i.value=2)(j.value=2)
*{font-family:sans-serif;}
input{margin:0 5px 0 0;width:50px;}
<label for=i>w: </label><input id=i type=number><label for=j>h: </label><input id=j type=number><pre id=o>

answered Jun 3, 2017 at 10:10
\$\endgroup\$
1
\$\begingroup\$

J, 11 bytes

,./@$'[]'"0

Try it online!

answered Oct 21, 2017 at 20:53
\$\endgroup\$
1
\$\begingroup\$

Canvas, 4 bytes

×ばつ*

Try it here!

answered Sep 8, 2018 at 16:00
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.