Dynamically Create Boxes

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.

• Nice first post! Welcome to PPCG! Jun 2, 2017 at 22:26
• Can I assume that the numbers are positive? Can there be trailing newlines? Jun 2, 2017 at 22:27
• @dzaima Positive integers, no trailing or leading stuff Jun 2, 2017 at 23:09
• can we print to console or do we need to return the string? Jun 2, 2017 at 23:10
• what if we literally can't not print trailing newlines? it tends to be good practice to allow one trailing newline Jun 3, 2017 at 0:53

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


Mathematica, 26 bytes

Grid@Table["[]",{#2},{#}]&

• Does a Mathematica Grid object count as "a string with newline characters"? Jun 4, 2017 at 2:58

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


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

• You can save 2 bytes by using Y (string representation of empty list) instead of "[]". Jun 3, 2017 at 1:12
• @insert_name_here Ingenious!! I updated the answer. Thanks for pointing that out! Jun 3, 2017 at 2:55
• Came up with this exact code independently. Nicely done. Jun 3, 2017 at 6:33

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?

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

05AB1E, 6 bytes

F„[]×,


Try it online!

Explanation

Input takes as height, width

F         # height times do
„[]      # push "[]"
×     # repeat width times
,    # print with newline


;#+, 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.

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

• Impressive. Welcome to the site! :) Jun 4, 2017 at 16:31
• 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) Jun 7, 2017 at 20:36
• I just wanted to meet the criteria for input, @calculatorFeline Jun 7, 2017 at 20:59
• ...Oh right. :| Jun 7, 2017 at 21:05

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. • 11 bytes Jun 3, 2017 at 2:45 Python 2.7, 32 bytes Full program: n,m=input() exec"print'[]'*n;"*m  Try it online! 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


Retina, 32 bytes

.+
$* 1(?=1*(¶1+))|.$1
G1
1
[]


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

Python 3, 26 bytes

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


Try it online!

V, 7 bytes

Ài[]<esc>ÀÄ


where <esc> is 0x1b.

Try it online!

Explanation

Ài[]<esc>                    " arg1 times insert []
ÀÄ                  " arg2 times duplicate this line


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


PowerShell, 25 Bytes

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


-3 thanks to Mathias!

• You can shorten it to 25 like so: param($w,$h),("[]"*$w)*$h Jun 4, 2017 at 12:33

Japt, 13 12+1= 14 13 bytes

+1 for the -R flag.

"[]"pN× òU*2


Try it online

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

APL (Dyalog), 11 bytes

'[]'⍴⍨⊢,2×⊣


Try it online!

'[]' the string

⍴⍨ cyclically repeated to fill the shape

⊢ right argument (rows)

, and

2× twice

⊣ the left argument (columns)

Charcoal, 8 7 bytes

ＥＮ×[]Ｉη


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:

 Ｎ      First input as a number
Ｅ       Map over implcit range
η Second input
Ｉ  Cast to number
[]   Literal string
×     Repeat
Implicitly print on separate lines

• Well it has drawing primitives for this but still 8 bytes :P Jun 3, 2017 at 3:11
• @ASCII-only Sorry, I didn't realise that Oblong worked on arbitrary strings. Neat!
– Neil
Jun 3, 2017 at 8:12
• @ASCII-only Oh, and what's the verbose name of the predefined empty string variable?
– Neil
Jun 3, 2017 at 8:26
• It's w, name, greek to verbose Jun 3, 2017 at 8:28
• @ASCII-only Then what am I doing wrong here: Try it online!
– Neil
Jun 3, 2017 at 9:30

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

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


Go, 74 bytes

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


Try it online!

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. Pyke, 8 bytes }A;**"[]  Try it here! Also 8 bytes:  F2K*"[]  Try it here! 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.

• I don't think this works for multi-digit numbers Jun 3, 2017 at 7:39
• @KritixiLithos It should work now Jun 3, 2017 at 18:45

C#, 78 bytes

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


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

CJam, 10 bytes

l~"[]"*N+*


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>

J, 11 bytes

,./@\$'[]'"0


Try it online!

Canvas, 4 bytes

[]×＊
`

Try it here!