Generate boxes!

19

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.

Spikatrix

Posted 2015-06-11T06:56:41.030

Reputation: 1 663

Answers

10

Pyth, 23 bytes

Mjbmsm@"= "&%k4dh*4HhyG

Defines the function g which works as desired.

Demonstration.

isaacg

Posted 2015-06-11T06:56:41.030

Reputation: 39 268

10

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

randomra

Posted 2015-06-11T06:56:41.030

Reputation: 19 909

One file per line always seems like a very odd design choice. – curiousdannii – 2015-06-12T03:32:25.830

1@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 – 2015-06-12T09:07:41.887

8

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}
`+x;alert(o)}

// TEST

// redefine alert to avoid that annoying popup during test
alert=x=>O.innerHTML=x

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>

edc65

Posted 2015-06-11T06:56:41.030

Reputation: 31 086

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 – 2015-06-12T11:58:55.720

@DennisJaheruddin , The same happens for me when using chrome (doesn't support ES6). Try firefox. – Spikatrix – 2015-06-12T12:22:39.853

1@DennisJaheruddin Test in Firefox – edc65 – 2015-06-12T12:32:20.153

7

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.)

apsillers

Posted 2015-06-11T06:56:41.030

Reputation: 3 632

7

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

DLosc

Posted 2015-06-11T06:56:41.030

Reputation: 21 213

6

Perl, 72 63 52 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

Stefan Majewsky

Posted 2015-06-11T06:56:41.030

Reputation: 161

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.

Kade

Posted 2015-06-11T06:56:41.030

Reputation: 7 463

5

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"
=============
=   =   =   =
=============
=   =   =   =
=============
$

Digital Trauma

Posted 2015-06-11T06:56:41.030

Reputation: 64 644

4

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.

Dennis

Posted 2015-06-11T06:56:41.030

Reputation: 196 637

4

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"

Sparr

Posted 2015-06-11T06:56:41.030

Reputation: 5 758

3

Python 3 2, 160 87 85 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)

=============
=   =   =   =   
=============
=   =   =   =   
=============
=   =   =   =   
=============

Try it out here.

Excuse the trailing whitespaces.

nsane

Posted 2015-06-11T06:56:41.030

Reputation: 141

1Indentation level can be reduced. – Spikatrix – 2015-06-11T11:30:56.333

3You don't need to build a list then join. Strings can be multiplied '='*(4*y+1) – Sp3000 – 2015-06-11T11:40:25.507

1Setting w=4*y+1 saves 3 bytes. – DLosc – 2015-06-11T20:53:55.587

@Cool Guy I'm using a tab, not 4 spaces. – nsane – 2015-06-12T04:24:28.047

Oh ok. Didn't notice that. – Spikatrix – 2015-06-12T04:26:34.257

3

CJam 52 51 46 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

TobiasR.

Posted 2015-06-11T06:56:41.030

Reputation: 161

3

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);
}

Digital Trauma

Posted 2015-06-11T06:56:41.030

Reputation: 64 644

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 – None – 2015-06-16T22:06:47.563

1Should have looked at the other answers first =/, my comment is a longer version of Reto Koradi's answer. – None – 2015-06-17T00:32:13.117

yes, I tried quite hard (and failed) to get this into a single (shorter) loop – Digital Trauma – 2015-06-17T00:39:35.397

3

PHP4.1, 76 71 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

-------------
|   |   |   |
-------------
|   |   |   |
-------------

Ismael Miguel

Posted 2015-06-11T06:56:41.030

Reputation: 6 797

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.

Alex A.

Posted 2015-06-11T06:56:41.030

Reputation: 23 761

3

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
> 

MickyT

Posted 2015-06-11T06:56:41.030

Reputation: 11 735

3

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
$ 

Digital Trauma

Posted 2015-06-11T06:56:41.030

Reputation: 64 644

2

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

vvye

Posted 2015-06-11T06:56:41.030

Reputation: 261

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 – 2015-06-11T10:34:00.743

@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 – 2015-06-11T11:23:08.227

2

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

WooiKent Lee

Posted 2015-06-11T06:56:41.030

Reputation: 413

2

Octave, 69 65 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)

sudo rm -rf slash

Posted 2015-06-11T06:56:41.030

Reputation: 1 076

2

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)

Reto Koradi

Posted 2015-06-11T06:56:41.030

Reputation: 4 870

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.

Reto Koradi

Posted 2015-06-11T06:56:41.030

Reputation: 4 870

You don't need the + at the end. CJam prints the entire stack. – Dennis – 2015-06-16T20:25:15.197

2

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)

aditsu quit because SE is EVIL

Posted 2015-06-11T06:56:41.030

Reputation: 22 326

1

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);}
}

Marco13

Posted 2015-06-11T06:56:41.030

Reputation: 1 131

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 – 2015-06-13T05:33:16.580

@CoolGuy Miscounted this - now 2 bytes more, but that doesn't change much... – Marco13 – 2015-06-13T11:45:28.797

1ok. +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 – 2015-06-13T14:46:05.703

1

C#, 153 151 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.

pmudra

Posted 2015-06-11T06:56:41.030

Reputation: 121

replace string with var. – CSharpie – 2015-06-12T19:54:11.577

Unfortunately that's not allowed. See this

– pmudra – 2015-06-12T20:01:14.757

1Declaring those ints outside the loop can save a byte. – Spikatrix – 2015-06-13T05:39:48.260

1

Dart, 57

b(y,x,{z:'='}){z+=z*4*x;print('$z\n=${'   ='*x}\n'*y+z);}

Try it at: https://dartpad.dartlang.org/36ed632613395303ef51

lrn

Posted 2015-06-11T06:56:41.030

Reputation: 521

1

Ruby, 57 48 45

f=->h,w{l=?=*w*4+?=;(l+$/+'=   '*w+"=
")*h+l}

Usage:

print f[2,5]

Thanks to manatwork for saving 3 bytes.

rr-

Posted 2015-06-11T06:56:41.030

Reputation: 273

Two small improvement possibilities: '='?= and "<LF>"$/. – manatwork – 2015-06-17T09:30:19.250

Another small one: ?=*(w*4+1)?=+?=*w*4 – manatwork – 2015-06-17T09:40:07.647

1

Brainfuck, 157 156 152 (123 120 118 117 with unary)

This works with Alex Pankratov's bff (brainfuck interpreter used on SPOJ and ideone) and Thomas Cort's BFI (used on Anarchy Golf).

The input must contain a single pair of integers (in decimal) separated by a space, without a trailing newline.

Demonstration on ideone.

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

Brief explanation:

At the end of first loop we are left with

r 0 c [0]

We then change this to

[r+1] 0 c 0 33 33 11 0

and begin the main loop. In that loop, the tricky part is alternating between 33 (exclamation mark) and 32 (space), and alternating between decrementing and not decrementing the number of rows. This is done with a flag using the cell to the right of c.

Update:

I had not considered unary input until reading some other answers. Here is a unary version:

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

Mitch Schwartz

Posted 2015-06-11T06:56:41.030

Reputation: 4 899

1

Lua, 95 93 85 bytes.

Works with command-line arguments.

Gotta love those string methods

a=arg[2]c="="l=c:rep(a*2+1)print((l.."\n"..(("= ")):rep(a)..c.."\n"):rep(arg[1])..l)

Trebuchette

Posted 2015-06-11T06:56:41.030

Reputation: 1 692

You can replace c..(" "):rep(3) by ("= ") – ECS – 2015-06-17T13:27:25.750

1

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)

=================
=   =   =   =   =
=================
=   =   =   =   =
=================
=   =   =   =   =
=================

heo

Posted 2015-06-11T06:56:41.030

Reputation: 91

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 – 2015-06-17T05:01:37.337

1

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:

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

Fried Egg

Posted 2015-06-11T06:56:41.030

Reputation: 91

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 – 2015-06-17T05:02:59.223

@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 – 2015-06-17T13:11:06.803

Your first link doesn't work for me. Chrome gives DNS_PROBE_FINISHED_NXDOMAIN – Spikatrix – 2015-06-20T06:07:01.493

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 – 2015-06-20T14:24:45.260

I don't know why, but the going to the control center causes the same error as mentioned in my earlier comment :/

– Spikatrix – 2015-06-21T05:56:17.030

1

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.

ECS

Posted 2015-06-11T06:56:41.030

Reputation: 361

Golf it more by using a for loop instead of a while loop. – Spikatrix – 2015-06-17T11:09:11.137

Unfortunately for(;w-->0;) is the same length as while(w-->0) – ECS – 2015-06-17T11:36:06.977

1I meant while(w-->0){d+=" =";b+="====";} --> for(;w-->0;b+="====")d+=" ="; which saves three bytes – Spikatrix – 2015-06-17T12:03:10.530

You are right. I actually managed to squeeze 4 bytes out of this thanks to your suggestion. – ECS – 2015-06-17T12:23:26.607

1

GolfScript, 48 bytes

2 5:b;:a;0`:z;{4b*1+z*n+}:r~{{z' '3*}b*z+n+r}a*

Takes the size as the first two inputs (here the first two digits).

Demonstration

000000000000000000000
0   0   0   0   0   0
000000000000000000000
0   0   0   0   0   0
000000000000000000000

Charlie Harding

Posted 2015-06-11T06:56:41.030

Reputation: 153

1

Haskell, 153

At least it's beating Java!

s r c
 |r==1=a c
 |otherwise=s (r-1)c++(take 2$tail(a c))
 where
 a 1=[f,"=   =",f]
 a n=map(\x->x++((take 4.tail)x))(a(n-1))
 f="====="

p r=unlines.s r

Works in ghci, although ghci doesn't take a new line on \n.

Here is the ungolfed version:

import System.Environment

xsquare :: Int -> [String]
xsquare 1 = ["=====","=   =","====="]
xsquare n = map (\x -> x++((take 4 . tail) x)) (xsquare (n-1))

ysquare :: Int -> [String] -> [String]
ysquare 1 x = x
ysquare n x = ysquare (n-1) (x ++ (take 2 $ tail x))

squares :: Int -> Int -> [String]
squares x y = ysquare y (xsquare x)

main = do
  a <- getArgs
  let [x,y] = (map read . take 2) a
  putStrLn $ unlines $ squares y x

It calculates the horizontal shape first, then finds the vertical shape by adding the last two lines of the horizontal shape the appropriate number of times. The main function is so I could test it properly with command line arguments and proper newline behaviour.

Craig Roy

Posted 2015-06-11T06:56:41.030

Reputation: 790