19
\$\begingroup\$

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.

\$\endgroup\$
0

34 Answers 34

10
\$\begingroup\$

Pyth, 23 bytes

Mjbmsm@"= "&%k4dh*4HhyG

Defines the function g which works as desired.

Demonstration.

\$\endgroup\$
0
10
\$\begingroup\$

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
------------------
============
=   =   =   
============
=   =   =   
============
------------------
=============
=   =   =   =
=============
=   =   =   =
=============
\$\endgroup\$
2
  • \$\begingroup\$ One file per line always seems like a very odd design choice. \$\endgroup\$ Commented Jun 12, 2015 at 3:32
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – randomra
    Commented Jun 12, 2015 at 9:07
8
\$\begingroup\$

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>

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 12, 2015 at 11:58
  • \$\begingroup\$ @DennisJaheruddin , The same happens for me when using chrome (doesn't support ES6). Try firefox. \$\endgroup\$
    – Spikatrix
    Commented Jun 12, 2015 at 12:22
  • 1
    \$\begingroup\$ @DennisJaheruddin Test in Firefox \$\endgroup\$
    – edc65
    Commented Jun 12, 2015 at 12:32
7
\$\begingroup\$

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

\$\endgroup\$
0
7
\$\begingroup\$

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

\$\endgroup\$
6
\$\begingroup\$

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

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.

\$\endgroup\$
0
5
\$\begingroup\$

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"
=============
=   =   =   =
=============
=   =   =   =
=============
$
\$\endgroup\$
1
  • \$\begingroup\$ here's a bunch of little edits that get it to 139 bytes without changing anything: Try it online! \$\endgroup\$
    – guest4308
    Commented Sep 7 at 10:04
4
\$\begingroup\$

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

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"
\$\endgroup\$
0
3
\$\begingroup\$

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

\$\endgroup\$
0
3
\$\begingroup\$

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);
}
\$\endgroup\$
3
  • \$\begingroup\$ 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 \$\endgroup\$
    – user21677
    Commented Jun 16, 2015 at 22:06
  • 1
    \$\begingroup\$ Should have looked at the other answers first =/, my comment is a longer version of Reto Koradi's answer. \$\endgroup\$
    – user21677
    Commented Jun 17, 2015 at 0:32
  • \$\begingroup\$ yes, I tried quite hard (and failed) to get this into a single (shorter) loop \$\endgroup\$ Commented Jun 17, 2015 at 0:39
3
\$\begingroup\$

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

-------------
|   |   |   |
-------------
|   |   |   |
-------------
\$\endgroup\$
0
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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

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

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Indentation level can be reduced. \$\endgroup\$
    – Spikatrix
    Commented Jun 11, 2015 at 11:30
  • 3
    \$\begingroup\$ You don't need to build a list then join. Strings can be multiplied '='*(4*y+1) \$\endgroup\$
    – Sp3000
    Commented Jun 11, 2015 at 11:40
  • 1
    \$\begingroup\$ Setting w=4*y+1 saves 3 bytes. \$\endgroup\$
    – DLosc
    Commented Jun 11, 2015 at 20:53
  • \$\begingroup\$ @Cool Guy I'm using a tab, not 4 spaces. \$\endgroup\$
    – nsane
    Commented Jun 12, 2015 at 4:24
  • \$\begingroup\$ Oh ok. Didn't notice that. \$\endgroup\$
    – Spikatrix
    Commented Jun 12, 2015 at 4:26
2
\$\begingroup\$

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

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
\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$
    – edc65
    Commented Jun 11, 2015 at 10:34
  • \$\begingroup\$ @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. :) \$\endgroup\$
    – vvye
    Commented Jun 11, 2015 at 11:23
2
\$\begingroup\$

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

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

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.

\$\endgroup\$
1
  • \$\begingroup\$ You don't need the + at the end. CJam prints the entire stack. \$\endgroup\$
    – Dennis
    Commented Jun 16, 2015 at 20:25
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Two small improvement possibilities: '='?= and "<LF>"$/. \$\endgroup\$
    – manatwork
    Commented Jun 17, 2015 at 9:30
  • \$\begingroup\$ Another small one: ?=*(w*4+1)?=+?=*w*4 \$\endgroup\$
    – manatwork
    Commented Jun 17, 2015 at 9:40
2
\$\begingroup\$

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

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

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

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);}
}
\$\endgroup\$
3
  • \$\begingroup\$ The output is wrong. See output no 2 : "Each box should have three horizontal spaces in them". Your code outputs two spaces, not three \$\endgroup\$
    – Spikatrix
    Commented Jun 13, 2015 at 5:33
  • \$\begingroup\$ @CoolGuy Miscounted this - now 2 bytes more, but that doesn't change much... \$\endgroup\$
    – Marco13
    Commented Jun 13, 2015 at 11:45
  • 1
    \$\begingroup\$ 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;}} \$\endgroup\$
    – Spikatrix
    Commented Jun 13, 2015 at 14:46
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ replace string with var. \$\endgroup\$
    – CSharpie
    Commented Jun 12, 2015 at 19:54
  • \$\begingroup\$ Unfortunately that's not allowed. See this \$\endgroup\$
    – pmudra
    Commented Jun 12, 2015 at 20:01
  • 1
    \$\begingroup\$ Declaring those ints outside the loop can save a byte. \$\endgroup\$
    – Spikatrix
    Commented Jun 13, 2015 at 5:39
1
\$\begingroup\$

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)

=================
=   =   =   =   =
=================
=   =   =   =   =
=================
=   =   =   =   =
=================
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Spikatrix
    Commented Jun 17, 2015 at 5:01
1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Golf it more by using a for loop instead of a while loop. \$\endgroup\$
    – Spikatrix
    Commented Jun 17, 2015 at 11:09
  • \$\begingroup\$ Unfortunately for(;w-->0;) is the same length as while(w-->0) \$\endgroup\$
    – ECS
    Commented Jun 17, 2015 at 11:36
  • 1
    \$\begingroup\$ I meant while(w-->0){d+=" =";b+="====";} --> for(;w-->0;b+="====")d+=" ="; which saves three bytes \$\endgroup\$
    – Spikatrix
    Commented Jun 17, 2015 at 12:03
  • \$\begingroup\$ You are right. I actually managed to squeeze 4 bytes out of this thanks to your suggestion. \$\endgroup\$
    – ECS
    Commented Jun 17, 2015 at 12:23
1
\$\begingroup\$

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:

#############
#   #   #   #
#############
#   #   #   #
#############
#   #   #   #
#############
#   #   #   #
#############
\$\endgroup\$
5
  • \$\begingroup\$ 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 \$\endgroup\$
    – Spikatrix
    Commented Jun 17, 2015 at 5:02
  • \$\begingroup\$ @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 \$\endgroup\$
    – Fried Egg
    Commented Jun 17, 2015 at 13:11
  • \$\begingroup\$ Your first link doesn't work for me. Chrome gives DNS_PROBE_FINISHED_NXDOMAIN \$\endgroup\$
    – Spikatrix
    Commented Jun 20, 2015 at 6:07
  • \$\begingroup\$ 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 \$\endgroup\$
    – Fried Egg
    Commented Jun 20, 2015 at 14:24
  • \$\begingroup\$ I don't know why, but the going to the control center causes the same error as mentioned in my earlier comment :/ \$\endgroup\$
    – Spikatrix
    Commented Jun 21, 2015 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.