22
\$\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.

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

33 Answers 33

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
\$\endgroup\$
4
\$\begingroup\$

Mathematica, 26 bytes

Grid@Table["[]",{#2},{#}]&
\$\endgroup\$
  • \$\begingroup\$ Does a Mathematica Grid object count as "a string with newline characters"? \$\endgroup\$ – David Zhang Jun 4 '17 at 2:58
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
\$\endgroup\$
4
\$\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
\$\endgroup\$
  • 3
    \$\begingroup\$ You can save 2 bytes by using `Y (string representation of empty list) instead of "[]". \$\endgroup\$ – insert_name_here Jun 3 '17 at 1:12
  • \$\begingroup\$ @insert_name_here Ingenious!! I updated the answer. Thanks for pointing that out! \$\endgroup\$ – Maria Jun 3 '17 at 2:55
  • 1
    \$\begingroup\$ Came up with this exact code independently. Nicely done. \$\endgroup\$ – isaacg Jun 3 '17 at 6:33
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?

\$\endgroup\$
  • \$\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\$ – Conor O'Brien Jun 3 '17 at 0:40
  • \$\begingroup\$ That only works when width is 2. \$\endgroup\$ – dbandstra Jun 3 '17 at 0:48
  • \$\begingroup\$ So it does, my mistake \$\endgroup\$ – Conor O'Brien Jun 3 '17 at 0:48
  • \$\begingroup\$ Great first golf! Welcome to the site! \$\endgroup\$ – MD XF Jun 3 '17 at 16:25
  • 1
    \$\begingroup\$ Wouldn't using a for loop shorten the code even more? \$\endgroup\$ – Spikatrix Jun 4 '17 at 8:15
3
\$\begingroup\$

05AB1E, 6 bytes

F„[]×,

Try it online!

Explanation

Input takes as height, width

F         # height times do
 „[]      # push "[]"
    ×     # repeat width times
     ,    # print with newline
\$\endgroup\$
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.

\$\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
<-]
\$\endgroup\$
  • \$\begingroup\$ Impressive. Welcome to the site! :) \$\endgroup\$ – DJMcMayhem Jun 4 '17 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\$ – CalculatorFeline Jun 7 '17 at 20:36
  • \$\begingroup\$ I just wanted to meet the criteria for input, @calculatorFeline \$\endgroup\$ – vityavv Jun 7 '17 at 20:59
  • \$\begingroup\$ ...Oh right. :| \$\endgroup\$ – CalculatorFeline Jun 7 '17 at 21:05
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.

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

Python 2.7, 32 bytes

Full program:

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

Try it online!

\$\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
\$\endgroup\$
2
\$\begingroup\$

Retina, 32 bytes

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

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

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

Python 3, 26 bytes

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

Try it online!

\$\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
\$\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
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 25 Bytes

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

-3 thanks to Mathias!

\$\endgroup\$
  • \$\begingroup\$ You can shorten it to 25 like so: param($w,$h),("[]"*$w)*$h \$\endgroup\$ – Mathias R. Jessen Jun 4 '17 at 12:33
2
\$\begingroup\$

Japt, 13 12+1= 14 13 bytes

+1 for the -R flag.

"[]"pN× òU*2

Try it online

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

APL (Dyalog), 11 bytes

'[]'⍴⍨⊢,2×⊣

Try it online!

'[]' the string

⍴⍨ cyclically repeated to fill the shape

 right argument (rows)

, and

 twice

the left argument (columns)

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

Charcoal, 8 7 bytes

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

\$\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
\$\endgroup\$
1
\$\begingroup\$

Go, 74 bytes

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

Try it online!

\$\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.

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

Pyke, 8 bytes

}A;**"[]

Try it here!

Also 8 bytes:

 F2K*"[]

Try it here!

\$\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.

\$\endgroup\$
  • 1
    \$\begingroup\$ I don't think this works for multi-digit numbers \$\endgroup\$ – Kritixi Lithos Jun 3 '17 at 7:39
  • \$\begingroup\$ @KritixiLithos It should work now \$\endgroup\$ – Conor O'Brien Jun 3 '17 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.

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

CJam, 10 bytes

l~"[]"*N+*

 

\$\endgroup\$
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>

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

J, 11 bytes

,./@$'[]'"0

Try it online!

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

Canvas, 4 bytes

[]×*

Try it here!

\$\endgroup\$

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.