# Generate boxes!

Your task is to generate boxes using any one ASCII character with respect to the inputs given.

# Test Cases

1 1   --> =====
=   =
=====

1 2   --> =========
=   =   =
=========

2 1   --> =====
=   =
=====
=   =
=====

2 2   --> =========
=   =   =
=========
=   =   =
=========

2 5   --> =====================
=   =   =   =   =   =
=====================
=   =   =   =   =   =
=====================


# Input

• Input can be taken from one of the following

• stdin
• Command-line arguments
• Function arguments (2 arguments, one for each number)
• Input, if taken from stdin or command line arguments, will contain two positive integers, seperated by a space.

• The two numbers denote the number of boxes in each column and row

# Output

• Boxes must be outputted in stdout (or closest equivalent)
• Each box should have three horizontal spaces in them

# Rules

• Both the numbers will be greater than 0, but will not go beyond 1000
• Any visible character can be used for outputting the boxes. (as long as they aren't too harsh on the eye!)
• You are permitted to write a full program or a function.
• There should be no unnecessary characters except an optional trailing newline character.

# Scoring

This is , so the shortest submission (in bytes) wins.

# Pyth, 23 bytes

Mjbmsm@"= "&%k4dh*4HhyG


Defines the function g which works as desired.

Demonstration.

# Retina, 57 bytes

1(?=.* (1*))
$1#$1#

1(?=(1*#1*#)*$) = 1 ==== # =#  Takes input in unary with a trailing newline. Each line should go to its own file and # should be changed to newline in the files. This is impractical but you can run the code as is as one file with the -s flag, keeping the # markers (and changing the trailing newline to # in the input too). You can change the #'s back to newlines in the output for readability if you wish. E.g.: > echo -n 11 111#|retina -s boxes|tr # '\n' ============= = = = = ============= = = = = =============  Method: 5 single substitution steps. The first two (first 4 lines) creates an 2*N+1 by M grid of ones and the next 3 steps (6 lines) format it into the desired output. The intermediate strings (delimited by -'s): 11 111 ------------------ 111 111 111 111 111 ------------------ 111 111 111 111 111 ------------------ 111 = = = 111 = = = 111 ------------------ ============ = = = ============ = = = ============ ------------------ ============= = = = = ============= = = = = =============  • One file per line always seems like a very odd design choice. – curiousdannii Jun 12 '15 at 3:32 • @curiousdannii It was made this way so the newline character would be usable in the regex expressions without escaping but it is planned to be changed into a more reasonable 1-file format. – randomra Jun 12 '15 at 9:07 # JavaScript (ES6), 83 A function with parameters rows and columns. Using template strings, there are 2 embedded newlines that are significant and counted. Output via alert (popup). As alert use a proportional font, we get a better result using a letter with a width similar to the space instead of =. Test in Firefox using the console to have the real alert output, or run the snippet below. f=(r,c)=>{for(x=y="|";c--;)x+="||||",y+=" |";for(o=x;r--;)o+=${y}

// TEST

// redefine alert to avoid that annoying popup during test

test=_=>([a,b]=I.value.match(/\d+/g),f(a,b))

test()
<input id=I value='2 3'><button onclick="test()">-></button>
<pre id=O></pre>

• Run code snippet does not work for me, just see a box with '2 3' inside and an arrow that seems to suggest that you should click it to get the result. Using the full page button or tryhing a different browser does not change anything. – Dennis Jaheruddin Jun 12 '15 at 11:58
• @DennisJaheruddin , The same happens for me when using chrome (doesn't support ES6). Try firefox. – Spikatrix Jun 12 '15 at 12:22
• @DennisJaheruddin Test in Firefox – edc65 Jun 12 '15 at 12:32

# JavaScript (ES6), 75 73

Using copious repeat calls, we repeat |, then repeat | with trailing spaces, and repeat both of those repeats to make rows:

f=(y,x)=>alert(((s="|"[r="repeat"](x*4)+|
)+"|   "[r](x)+|
)[r](y)+s)


(Newlines are significant, per edc65's suggestion to use template strings.)

Snippet:

<input id="x" type="range" max="10" min="1" step="1" value="3"><input id="y" type="range" max="10" min="1" step="1" value="2"><pre id="o"></pre><script>function f(y,x){return ((s="|"[r="repeat"](x*4)+"|\n")+"|   "[r](x)+"|\n")[r](y)+s};function redraw(){document.getElementById("o").innerHTML=f(document.getElementById("y").value,document.getElementById("x").value)};document.getElementById("x").onchange=redraw;document.getElementById("y").onchange=redraw;document.getElementById("x").oninput=redraw;document.getElementById("y").oninput=redraw;redraw();</script>

Without template strings, without method-name reuse, and with added whitespace:

f=(y,x)=>alert(
(
(s="|".repeat(x*4)+"|\n") +
"|   ".repeat(x)+"|\n"
).repeat(y)+s
)


(Using edc65's recommendation to use | for better visual spacing.)

# Pip, 29 24 = 23 + 1 bytes

Requires the -n flag. Takes the height and width as command-line args and builds boxes of of 1s.

([1X4m]XbRLa+1)@<v.1R0s


Explanation:

                         a,b are cmdline args; m is 1000; v is -1; s is " " (implicit)
[1X4m]                  List containing 1111 and 1000
Xb                String repetition of each element b times
RLa+1           Repeat the list a+1 times
(             )@<v       Now we have one row too many at the end, so take everything
but the last item (equiv to Python x[:-1])
.1     Concatenate a 1 to the end of each row
R0s  Replace 0 with space
Print, joining list on newlines (implicit, -n flag)


This program takes heavy advantage of the fact that strings are numbers and numbers are strings in Pip. (And the three spaces in those boxes happened to be just right to take advantage of the built-in m variable!)

Here's how the list gets built with the input 2 3:

[1111;1000]
[111111111111;100010001000]
[111111111111;100010001000;111111111111;100010001000;111111111111;100010001000]
[111111111111;100010001000;111111111111;100010001000;111111111111]
[1111111111111;1000100010001;1111111111111;1000100010001;1111111111111]
[1111111111111;"1   1   1   1";1111111111111;"1   1   1   1";1111111111111]


And the final output:

C:\> pip.py 2 3 -ne "([1X4m]XbRLa+1)@<v.1R0s"
1111111111111
1   1   1   1
1111111111111
1   1   1   1
1111111111111


More on Pip

## Perl, 726352 50 bytes

My shortest version yet uses $/ to get a newline char more compactly: $ perl -e 'print((($,="="." ="x pop.$/)=~s/./=/gr)x(1+pop))' 2 5
=====================
=   =   =   =   =   =
=====================
=   =   =   =   =   =
=====================


The previous update puts the mostly empty lines in the output record separator $,, and prints a list of continuous lines. $ perl -e 'print((($,="="." ="x pop."\n")=~s/./=/gr)x(1+pop))' 2 5  The previous version might be a bit clearer for the casual reader: $ perl -E 'say($y=($x="="."   ="x pop)=~s/./=/gr);for(1..pop){say$x;say$y}' 2 5


The first version used @ARGV instead of pop:

$perl -E 'say($y=($x="="." ="x$ARGV[1])=~s/./=/gr);for(1..$ARGV[0]){say$x;say$y}' 2 5  # Python 2, 58 57 Bytes Fairly straightforward implementation. def f(x,y):a="="*(4*y+1);print(a+"\n="+" ="*y+"\n")*x+a  Check it out here. Thanks to Sp3000 for saving one byte. # GNU sed -r, 160 Sigh, I thought this would be smaller, but here it is anyway. Unfortunately sed regexes don't have any lookaround capability. : s/(.*)1$/   =\1/;t
s/([= ]+)/\1\n\1/
:b
s/   (.*\n)/===\1/;tb
s/(1*)1 $/\n\1/ :c s/([^\n]*\n[^\n]*\n)(1*)1$/\1\1\2/;tc
s/(=+)(.*)/\1\2\1/
s/(^|\n)(.)/\1=\2/g


Taking input as unary from STDIN:

$sed -rf boxes.sed <<< "11 111" ============= = = = = ============= = = = = =============$


# CJam, 25 bytes

q~)S3*'=5*+4/f*3f>\)*1>N*


Try it online in the CJam interpreter.

### How it works

q~                        e# Read H and W from STDIN.
S3*'=5*+               e# Push "   =====".
4/             e# Chop into ["   =" "===="].
)          f*           e# Repeat each string W+1 times.
3f>        e# Cut off the first three characters.
\)*     e# Repeat the resulting array H+1 times.
1>   e# Remove the first string.
N* e# Join the lines.


# Marbelous, 168 bytes

This answer only works up to 255x255, not 1000x1000, due to limitations of the Marbelous language. I'm working on a 32-bit library, but it's not going to be ready any time soon.

Input is provided as two command line parameters or function parameters to the main board.

@2@3}1@0
SLEL//>0\/
@3@1}0--
}1&0@0/\&0
@1/\@2}1\/
:SL
..}0@0
&0/\>0&0
EN..--\/
{0@0/\ES
:EL
..@0
..>0EN
}0--\/
@0/\EX
:EX
}0
\/3D3D3D3D
:ES
}0
\/3D202020
:EN
}0
{03D0A


Pseudocode:

MB(H,W):
EL(W)
for 1..H:
SL(W)
EL(W)
EL(W):
for 1..W:
EX()
EN()
SL(W):
for 1..W:
ES()
EN()
EX():
print "===="
ES():
print "=   "
EN():
print "=\n"


# CJam 525146 41 bytes

l~:B;:A;'=:U{{U4*}B*N}:V~{U{SZ*U}B*NUV}A*


Thanks to Sp3000 for -5 chars

Thanks to Martin Büttner♦ for another 5 chars

Try it

# c function, 81

x,y;f(h,w){for(;y<=h*2;y++)for(x=0;x<w*4+2;x++)putchar(x>w*4?10:x&3&&y&1?32:61);}


### Test program:

x,y;f(h,w){for(;y<=h*2;y++)for(x=0;x<w*4+2;x++)putchar(x>w*4?10:x&3&&y&1?32:61);}

int main (int argc, char **argv)
{
f(2, 3);
}

• I dropped a few characters to treat it as a single line instead of double for: x;f(h,w){for(w=w*4+2;x<=w*h*2+w;x++)putchar(x%w?x/w%2?x%w%4!=1?32:61:61:10);} -- 78 – user21677 Jun 16 '15 at 22:06
• Should have looked at the other answers first =/, my comment is a longer version of Reto Koradi's answer. – user21677 Jun 17 '15 at 0:32
• yes, I tried quite hard (and failed) to get this into a single (shorter) loop – Digital Trauma Jun 17 '15 at 0:39

# PHP4.1, 7671 69 bytes

This is as golfed as I can get.

$R=str_repeat;echo$R(($T=$R('-',4*$H+1))." |{$R('   |',$H)} ",$V),$T;  This expects the key H to have the number of lines and V the number of boxes per line. I'm using - and | just so the boxes actually look like boxes. If required, I can change it to =. It doesn't matter the char that is used. Leaving - and | also helps to understand the mess that's going on. Old method, 76 bytes: for($R=str_repeat;$H--;)echo$T=$R('-',($V*4)+1),"
|{$R(' |',$V)}
";echo$T;  Example of output: http://localhost/file.php?H=2&V=3 ------------- | | | | ------------- | | | | -------------  # Julia, 59 bytes (n,m)->(l="="^(4m+1)*"\n";print(l*("="*" ="^m*"\n"*l)^n))  This creates an unnamed function that accepts two integers as input and prints the result to STDOUT. To call it, give it a name, e.g. f=(n,m)->.... Ungolfed + explanation: function f(n, m) # Store the solid row l = "="^(4m + 1) * "\n" # Print all rows by repeating a pattern n times print(l * ("=" * " ="^m * "\n" * l)^n) end  Examples: julia> f(2, 3) ============= = = = = ============= = = = = ============= julia> f(1, 5) ===================== = = = = = = =====================  Any suggestions are welcome. # bash + coreutils, 57 dc -e"2do$2 4*1+^1-pd8*F/1+$1si[fli1-dsi0<c]dscx"|tr 0 \  This uses dc to print binary numbers that have 1s for the box edges and 0s for the spaces. • the all-ones number X is calculated by 2 ^ (width * 4 + 1) - 1, then pushed and printed • the 10001...0001 number is calculated by X* 8 / 15 + 1, then pushed • the stack is then dumped height times The tr then converts the 0s to space characters. ### Output $ ./boxes.sh 2 4
11111111111111111
1   1   1   1   1
11111111111111111
1   1   1   1   1
11111111111111111
$ # R, 83 81 As an unnamed function taking h and w as parameters. Builds the 1st and second lines into a vector for each row and replicates it h times. Appends a vector for the bottom line and cats out the vector using fill to restrict the length of the lines. Now takes advantage of the any visible character rule. function(h,w)cat(rep(c(A<-rep(1,w*4+2),rep(' 1',w)),h),A[-1],fill=w*4+1,sep='')  Test run > f=function(h,w)cat(rep(c(A<-rep(1,w*4+2),rep(' 1',w)),h),A[-1],fill=w*4+1,sep='') > f(4,2) 111111111 1 1 1 111111111 1 1 1 111111111 1 1 1 111111111 1 1 1 111111111 >  # Python 3 2, 1608785 79 bytes I know this can be golfed a lot more, I would like some suggestions as this is my first try at golfing. def d(x,y): for i in[1]*x:print'='*(4*y+1)+'\n'+'= '*(y+1) print'='*(4*y+1)  Thanks to @Cool Guy and @Sp3000's tips, I narrowed the size down to just above below half. Eg: d(3,3) ============= = = = = ============= = = = = ============= = = = = =============  Excuse the trailing whitespaces. • Indentation level can be reduced. – Spikatrix Jun 11 '15 at 11:30 • You don't need to build a list then join. Strings can be multiplied '='*(4*y+1) – Sp3000 Jun 11 '15 at 11:40 • Setting w=4*y+1 saves 3 bytes. – DLosc Jun 11 '15 at 20:53 • @Cool Guy I'm using a tab, not 4 spaces. – nsane Jun 12 '15 at 4:24 • Oh ok. Didn't notice that. – Spikatrix Jun 12 '15 at 4:26 # KDB(Q), 37 bytes {(3+2*x-1)#(5+4*y-1)#'(4#"=";"= ")}  # Explanation  (4#"=";"= ") / box shape (5+4*y-1)#' / extend y axis (3+2*x-1)# / extend x axis { } / lambda  # Test q){(3+2*x-1)#(5+4*y-1)#'(4#"=";"= ")}[2;5] "=====================" "= = = = = =" "=====================" "= = = = = =" "====================="  # JavaScript, 92 85 bytes I had hoped this would be shorter than the other JS answer (nice work as always, edc65), but oh well. I have a feeling the math here can be further golfed. f=(r,c)=>(x=>{for(i=s='';(y=i++/x)<r-~r;)s+=i%x?' *'[-~y%2|!(-~i%4)]:'\n'})(4*c+2)||s  • Sorry, can't help with the math - my head is spinning ... but here is some microop: f=(r,c)=>(x=>{for(i=s='';(y=i++/x)<r-~r;)s+=i%x?' *'[-~y%2|!(-~i%4)]:'\n'})(4*c+2)||s --> 85 – edc65 Jun 11 '15 at 10:34 • @edc65 that's great, thanks! Things like 2*r+1 => r-~r are exactly what I meant by golfing the math, and that particular one is genius. :) – vvye Jun 11 '15 at 11:23 # Octave, 6965 64 y=ones([2,4].*input()+1);y(1:2:end,:)=y(:,1:4:end)=4;char(y+31)  Thanks to DLosc for pointing out issues that led to -1 Takes input as [1 1] and outputs: ##### # # #####  You can also just input '1' and get 1x1. If the input really needs to be 1 1, the size goes up to 88 85 84: y=ones([2,4].*eval(['[',input(0,'s'),']'])+1);y(1:2:end,:)=y(:,1:4:end)=4;char(y+31)  Note: Matlab doesn't allow Octave's chaining or input(integer), but here is the Matlab version (67): y=ones([2,4].*input('')+1);y(1:2:end,:)=4;y(:,1:4:end)=4;char(y+31)  # C, 76 bytes w,n;f(r,c){for(w=c*4+2,n=w*r*2+w;n--;)putchar(n%w?n/w%2&&n%w%4-1?32:61:10);}  Invoked as a function with number of rows and columns as arguments. For example: f(5, 2)  # CJam, 30 29 bytes New version with redundant + at the end removed (thanks, Dennis): l~_4*)'=*N+:F\'=S3*+*'=+N++*F  I know that Dennis already posted a CJam solution that beats this by miles. But this is my very first attempt at CJam, so it's a miracle that it works at all. :) Fairly brute force. Builds the first line from 4 * H + 1 = signs, then the second line from = repeated H times, with another = added. Then concatenates the two lines, repeats the whole thing V times, and then adds another copy of the first line. It feels like I have way too many stack manipulations, and even ended up storing the first line in a variable because I had to shuffle stuff around on the stack even more otherwise. Not very elegant overall, but you have to start somewhere... and I wanted to try a simple problem first. • You don't need the + at the end. CJam prints the entire stack. – Dennis Jun 16 '15 at 20:25 # CJam, 23 q~[F8]f{2b*1+' f+N}*_0=  Try it online Explanation: q~ read and evaluate the input (height width) [F8] make an array [15 8] - 1111 and 1000 in base 2 f{…} for width and each of [15 8] 2b convert the number to base 2 * repeat the digits "width" times 1+ append a 1 to the array of digits (right edge) ' f+ add the space character to each digit (0->' ', 1->'!') N push a newline * repeat the resulting array "height" times _0= copy the first line (bottom edge)  # Dart, 57 b(y,x,{z:'='}){z+=z*4*x;print('$z\n=${' ='*x}\n'*y+z);}  ## Java, 181 I hope that according to You are permitted to write a full program or a function. it is compliant to the rules to count the bytes of the function, which is 181 in this case import static java.lang.System.*; public class Boxes { public static void main(String[] args) { Boxes b=new Boxes(); System.out.println("1,1:"); b.b(1,1); System.out.println("1,2:"); b.b(1,2); System.out.println("2,1:"); b.b(2,1); System.out.println("2,2:"); b.b(2,2); System.out.println("2,5:"); b.b(2,5); } void b(int R, int C){String s="",e=s,x,y,z=s,a="====",n="=\n";int r,c;for(r=R;r-->0;){x=y=e;for(c=C;c-->0;){x+=a;y+="= ";}s+=x+n+y+n;}for(c=C;c-->0;){z+=a;}s+=z+n;out.println(s);} }  • The output is wrong. See output no 2 : "Each box should have three horizontal spaces in them". Your code outputs two spaces, not three – Spikatrix Jun 13 '15 at 5:33 • @CoolGuy Miscounted this - now 2 bytes more, but that doesn't change much... – Marco13 Jun 13 '15 at 11:45 • ok. +1. Save two bytes by changing for(r=R;r-->0;){x=y=e;for(c=C;c-->0;){x+=a;y+="= ";}s+=x+n+y+n;} to for(r=R;r-->0;s+=x+n+y+n){x=y=e;for(c=C;c-->0;y+="= "){x+=a;}} – Spikatrix Jun 13 '15 at 14:46 # C#, 153151 150 This can't really compete but here it is just for fun: (h,w)=>{string s="=",e="\n",a="====",b=" =",m=a,o;int i=0,j;for(;i++<=h*2;){o=s;for(j=0;j++<w+1;o=m)System.Console.Write(o);m=m==a?b:a;s=e+s;e="";}}  How to run: public class Program { public static void Main() { new System.Action<int, int>((h,w)=>{string s="=",e="\n",a="====",b=" =",m=a,o;int i=0,j;for(;i++<=h*2;){o=s;for(j=0;j++<w+1;o=m)System.Console.Write(o);m=m==a?b:a;s=e+s;e="";}})(3, 4); } }  Open for improvements. • replace string with var. – CSharpie Jun 12 '15 at 19:54 • Unfortunately that's not allowed. See this – pmudra Jun 12 '15 at 20:01 • Declaring those ints outside the loop can save a byte. – Spikatrix Jun 13 '15 at 5:39 # Python 2.7, 66 bytes. I know there are already better solutions in python, but that's the best I came up with. def f(x,y):a,b,c="="*4," =","\n=";print "="+a*y+(c+b*y+c+a*y)*x  Example: f(3,4) ================= = = = = = ================= = = = = = ================= = = = = = =================  • Sorry, but this produces wrong output for test cases 2,3 and 5. You mixed up the columns and rows and got it the opposite way. – Spikatrix Jun 17 '15 at 5:01 ## Ruby, 5748 45 f=->h,w{l=?=*w*4+?=;(l+$/+'=   '*w+"=
")*h+l}


Usage:

print f[2,5]


Thanks to manatwork for saving 3 bytes.

• Two small improvement possibilities: '='?= and "<LF>"$/. – manatwork Jun 17 '15 at 9:30 • Another small one: ?=*(w*4+1)?=+?=*w*4 – manatwork Jun 17 '15 at 9:40 # Java, 123 119 bytes void p(int h,int w){String b="=",d="\n=";for(;w-->0;d+=" =")b+="====";for(d+="\n"+b;h-->0;b+=d);System.out.print(b);}  Abusing the input parameters as counters greatly helped in decreasing code size. Thanks to Cool Guy for suggesting the abuse of for syntax. • Golf it more by using a for loop instead of a while loop. – Spikatrix Jun 17 '15 at 11:09 • Unfortunately for(;w-->0;) is the same length as while(w-->0) – ECS Jun 17 '15 at 11:36 • I meant while(w-->0){d+=" =";b+="====";} --> for(;w-->0;b+="====")d+=" ="; which saves three bytes – Spikatrix Jun 17 '15 at 12:03 • You are right. I actually managed to squeeze 4 bytes out of this thanks to your suggestion. – ECS Jun 17 '15 at 12:23 # SAS, 117 119 macro a array a[0:1]$4('#   ' '####');do x=1 to 2+2*&x-1;t=repeat(a[mod(x,2)],&y-1);put t \$char%%eval(&y*3). '#';end;%


Example:

%let x=4;
%let y=4;
data;a;run;


Result:

#############
#   #   #   #
#############
#   #   #   #
#############
#   #   #   #
#############
#   #   #   #
#############

• Is there any online compiler where I could test this? BTW, as per your result, your program produces wrong output. See output2 : Each box should have three horizontal spaces in them – Spikatrix Jun 17 '15 at 5:02
• @CoolGuy, you're right, I did not catch that, updated my post. You can try SAS On Demand, would be the least troublesome way to access an online compiler without setting up your own AWS Instance – Fried Egg Jun 17 '15 at 13:11
• Your first link doesn't work for me. Chrome gives DNS_PROBE_FINISHED_NXDOMAIN – Spikatrix Jun 20 '15 at 6:07
• Try this one, although the first link works fine for me SAS On Demand or follow the link on this page to the 'Control Center' here – Fried Egg Jun 20 '15 at 14:24
• I don't know why, but the going to the control center causes the same error as mentioned in my earlier comment :/ – Spikatrix Jun 21 '15 at 5:56