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.
-
2\$\begingroup\$ Nice first post! Welcome to PPCG! \$\endgroup\$MD XF– MD XF2017年06月02日 22:26:17 +00:00Commented Jun 2, 2017 at 22:26
-
1\$\begingroup\$ Can I assume that the numbers are positive? Can there be trailing newlines? \$\endgroup\$dzaima– dzaima2017年06月02日 22:27:13 +00:00Commented Jun 2, 2017 at 22:27
-
\$\begingroup\$ @dzaima Positive integers, no trailing or leading stuff \$\endgroup\$Robinlemon– Robinlemon2017年06月02日 23:09:40 +00:00Commented Jun 2, 2017 at 23:09
-
\$\begingroup\$ can we print to console or do we need to return the string? \$\endgroup\$Giuseppe– Giuseppe2017年06月02日 23:10:29 +00:00Commented 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\$Destructible Lemon– Destructible Lemon2017年06月03日 00:53:45 +00:00Commented Jun 3, 2017 at 0:53
39 Answers 39
Pyth - (削除) 7 (削除ここまで) 5 bytes
-2 bytes by a clever trick thanks to insert_name_here
VE*`Y
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
-
4\$\begingroup\$ You can save 2 bytes by using
`Y
(string representation of empty list) instead of"[]"
. \$\endgroup\$insert_name_here– insert_name_here2017年06月03日 01:12:05 +00:00Commented Jun 3, 2017 at 1:12 -
\$\begingroup\$ @insert_name_here Ingenious!! I updated the answer. Thanks for pointing that out! \$\endgroup\$Maria– Maria2017年06月03日 02:55:59 +00:00Commented Jun 3, 2017 at 2:55
-
1\$\begingroup\$ Came up with this exact code independently. Nicely done. \$\endgroup\$izzyg– izzyg2017年06月03日 06:33:37 +00:00Commented Jun 3, 2017 at 6:33
Mathematica, 26 bytes
Grid@Table["[]",{#2},{#}]&
-
1\$\begingroup\$ Does a Mathematica
Grid
object count as "a string with newline characters"? \$\endgroup\$David Zhang– David Zhang2017年06月04日 02:58:46 +00:00Commented Jun 4, 2017 at 2:58
05AB1E, 6 bytes
×ばつ,
Explanation
Input takes as height, width
F # height times do
„[] # push "[]"
×ばつ # repeat width times
, # print with newline
MATL, 7 bytes
v&DiiX"
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
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?
-
\$\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– Conor O'Brien2017年06月03日 00:40:13 +00:00Commented Jun 3, 2017 at 0:40 -
\$\begingroup\$ That only works when width is 2. \$\endgroup\$dbandstra– dbandstra2017年06月03日 00:48:27 +00:00Commented Jun 3, 2017 at 0:48
-
\$\begingroup\$ So it does, my mistake \$\endgroup\$Conor O'Brien– Conor O'Brien2017年06月03日 00:48:42 +00:00Commented Jun 3, 2017 at 0:48
-
\$\begingroup\$ Great first golf! Welcome to the site! \$\endgroup\$MD XF– MD XF2017年06月03日 16:25:03 +00:00Commented Jun 3, 2017 at 16:25
-
1\$\begingroup\$ Wouldn't using a
for
loop shorten the code even more? \$\endgroup\$Spikatrix– Spikatrix2017年06月04日 08:15:55 +00:00Commented Jun 4, 2017 at 8:15
;#+, 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
+++++++++[>++++++++++<-]>+[>+>+<<-]>>++>,>+++++++++[<----->-]<--->>>,>+++++++++[<----->-]<--->++++++++++<[<<<[>+>+<<-]>[<<<.>.>>-]>[<<+>>-]>>.<-]
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
<-]
-
\$\begingroup\$ Impressive. Welcome to the site! :) \$\endgroup\$DJMcMayhem– DJMcMayhem2017年06月04日 16:31:45 +00:00Commented 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\$CalculatorFeline– CalculatorFeline2017年06月07日 20:36:35 +00:00Commented Jun 7, 2017 at 20:36
-
\$\begingroup\$ I just wanted to meet the criteria for input, @calculatorFeline \$\endgroup\$willowis.cool– willowis.cool2017年06月07日 20:59:42 +00:00Commented Jun 7, 2017 at 20:59
-
\$\begingroup\$ ...Oh right. :| \$\endgroup\$CalculatorFeline– CalculatorFeline2017年06月07日 21:05:10 +00:00Commented 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\$Weird Glyphs– Weird Glyphs2024年12月15日 20:48:13 +00:00Commented Dec 15, 2024 at 20:48
J, 12 bytes
'[]'$~],+:@[
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.
Jelly, 7 bytes
ẋ)[]ẋ$Y
A dyadic link returning a list of characters (or a full program printing the result).
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ドル
G`1
1
[]
Try it online! Takes height and width input on separate lines.
V, 7 bytes
Ài[]<esc>ÀÄ
where <esc>
is 0x1b
.
Explanation
Ài[]<esc> " arg1 times insert []
ÀÄ " arg2 times duplicate this line
Ohm, 9 bytes
M"[]"َJ,
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!
-
\$\begingroup\$ You can shorten it to 25 like so:
param($w,$h),("[]"*$w)*$h
\$\endgroup\$Mathias R. Jessen– Mathias R. Jessen2017年06月04日 12:33:11 +00:00Commented Jun 4, 2017 at 12:33
APL (Dyalog), 11 bytes
×ばつ⊣
'[]'
the string
⍴⍨
cyclically repeated to fill the shape
⊢
right argument (rows)
,
and
×ばつ
twice
⊣
the left argument (columns)
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
-
\$\begingroup\$ Well it has drawing primitives for this but still 8 bytes :P \$\endgroup\$ASCII-only– ASCII-only2017年06月03日 03:11:57 +00:00Commented Jun 3, 2017 at 3:11
-
\$\begingroup\$ @ASCII-only Sorry, I didn't realise that Oblong worked on arbitrary strings. Neat! \$\endgroup\$Neil– Neil2017年06月03日 08:12:55 +00:00Commented Jun 3, 2017 at 8:12
-
\$\begingroup\$ @ASCII-only Oh, and what's the verbose name of the predefined empty string variable? \$\endgroup\$Neil– Neil2017年06月03日 08:26:40 +00:00Commented Jun 3, 2017 at 8:26
-
\$\begingroup\$ It's
w
, name, greek to verbose \$\endgroup\$ASCII-only– ASCII-only2017年06月03日 08:28:47 +00:00Commented Jun 3, 2017 at 8:28 -
\$\begingroup\$ @ASCII-only Then what am I doing wrong here: Try it online! \$\endgroup\$Neil– Neil2017年06月03日 09:30:18 +00:00Commented Jun 3, 2017 at 9:30
Japt -mR
, (削除) 13 (削除ここまで) (削除) 12 (削除ここまで) 5 bytes
Takes input in reverse order
Vç"[]
- 1 byte saved thanks to obarakon.
-
\$\begingroup\$ Being a little drunk can sometimes help your programming skills though :P \$\endgroup\$ETHproductions– ETHproductions2017年06月02日 23:16:09 +00:00Commented Jun 2, 2017 at 23:16
-
\$\begingroup\$ @ETHproductions: the very cartoon I was looking for but was too drunk to find! \$\endgroup\$Shaggy– Shaggy2017年06月03日 00:25:04 +00:00Commented Jun 3, 2017 at 0:25
-
\$\begingroup\$ Haha, hope you guys are having a fun night. fyi,
U*V
can be shortened toN×
\$\endgroup\$Oliver– Oliver2017年06月03日 02:31:40 +00:00Commented Jun 3, 2017 at 2:31 -
1\$\begingroup\$ @obarakon: That's 2 opportunities to work with
N
last night. Never drink & golf, kids! \$\endgroup\$Shaggy– Shaggy2017年06月03日 13:11:04 +00:00Commented Jun 3, 2017 at 13:11
R, 70 bytes
p=paste
function(w,h)p(rep(p(rep('[]',w),collapse=''),h),collapse='
')
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.
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)}
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.
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.
-
1\$\begingroup\$ I don't think this works for multi-digit numbers \$\endgroup\$user41805– user418052017年06月03日 07:39:34 +00:00Commented Jun 3, 2017 at 7:39
-
\$\begingroup\$ @KritixiLithos It should work now \$\endgroup\$Conor O'Brien– Conor O'Brien2017年06月03日 18:45:10 +00:00Commented 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>