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
stdinor 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 code-golf, so the shortest submission (in bytes) wins.
34 Answers 34
Pyth, 23 bytes
Mjbmsm@"= "&%k4dh*4HhyG
Defines the function g which works as desired.
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
------------------
============
= = =
============
= = =
============
------------------
=============
= = = =
=============
= = = =
=============
-
\$\begingroup\$ One file per line always seems like a very odd design choice. \$\endgroup\$curiousdannii– curiousdannii2015年06月12日 03:32:25 +00:00Commented 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– randomra2015年06月12日 09:07:41 +00:00Commented Jun 12, 2015 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}
`+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>
-
\$\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\$Dennis Jaheruddin– Dennis Jaheruddin2015年06月12日 11:58:55 +00:00Commented Jun 12, 2015 at 11:58
-
\$\begingroup\$ @DennisJaheruddin , The same happens for me when using chrome (doesn't support ES6). Try firefox. \$\endgroup\$Spikatrix– Spikatrix2015年06月12日 12:22:39 +00:00Commented Jun 12, 2015 at 12:22
-
1\$\begingroup\$ @DennisJaheruddin
Test in Firefox\$\endgroup\$edc65– edc652015年06月12日 12:32:20 +00:00Commented Jun 12, 2015 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
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
Python 2, (削除) 58 (削除ここまで) 57 Bytes
Fairly straightforward implementation.
def f(x,y):a="="*(4*y+1);print(a+"\n="+" ="*y+"\n")*x+a
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円\n1円/
:b
s/ (.*\n)/===1円/;tb
s/(1*)1 $/\n1円/
: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"
=============
= = = =
=============
= = = =
=============
$
-
\$\begingroup\$ here's a bunch of little edits that get it to 139 bytes without changing anything: Try it online! \$\endgroup\$guest4308– guest43082024年09月07日 10:04:49 +00:00Commented Sep 7, 2024 at 10:04
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 (削除) 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
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);
}
-
\$\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– user216772015年06月16日 22:06:47 +00:00Commented 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– user216772015年06月17日 00:32:13 +00:00Commented Jun 17, 2015 at 0:32
-
\$\begingroup\$ yes, I tried quite hard (and failed) to get this into a single (shorter) loop \$\endgroup\$Digital Trauma– Digital Trauma2015年06月17日 00:39:35 +00:00Commented Jun 17, 2015 at 0:39
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
-------------
| | | |
-------------
| | | |
-------------
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"2do2ドル 4*1+^1-pd8*F/1+1ドルsi[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...0001number is calculated byX* 8 / 15 + 1, then pushed - the stack is then dumped
heighttimes
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, (削除) 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)
=============
= = = =
=============
= = = =
=============
= = = =
=============
Excuse the trailing whitespaces.
-
1\$\begingroup\$ Indentation level can be reduced. \$\endgroup\$Spikatrix– Spikatrix2015年06月11日 11:30:56 +00:00Commented 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– Sp30002015年06月11日 11:40:25 +00:00Commented Jun 11, 2015 at 11:40 -
1\$\begingroup\$ Setting
w=4*y+1saves 3 bytes. \$\endgroup\$DLosc– DLosc2015年06月11日 20:53:55 +00:00Commented Jun 11, 2015 at 20:53 -
\$\begingroup\$ @Cool Guy I'm using a tab, not 4 spaces. \$\endgroup\$nsane– nsane2015年06月12日 04:24:28 +00:00Commented Jun 12, 2015 at 4:24
-
\$\begingroup\$ Oh ok. Didn't notice that. \$\endgroup\$Spikatrix– Spikatrix2015年06月12日 04:26:34 +00:00Commented Jun 12, 2015 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
-
\$\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– edc652015年06月11日 10:34:00 +00:00Commented Jun 11, 2015 at 10:34 -
\$\begingroup\$ @edc65 that's great, thanks! Things like
2*r+1=>r-~rare exactly what I meant by golfing the math, and that particular one is genius. :) \$\endgroup\$vvye– vvye2015年06月11日 11:23:08 +00:00Commented Jun 11, 2015 at 11:23
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)
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.
-
\$\begingroup\$ You don't need the
+at the end. CJam prints the entire stack. \$\endgroup\$Dennis– Dennis2015年06月16日 20:25:15 +00:00Commented Jun 16, 2015 at 20:25
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.
-
\$\begingroup\$ Two small improvement possibilities:
'='→?=and"<LF>"→$/. \$\endgroup\$manatwork– manatwork2015年06月17日 09:30:19 +00:00Commented Jun 17, 2015 at 9:30 -
\$\begingroup\$ Another small one:
?=*(w*4+1)→?=+?=*w*4\$\endgroup\$manatwork– manatwork2015年06月17日 09:40:07 +00:00Commented Jun 17, 2015 at 9:40
CJam, 23
q~[F8]f{2b*1+' f+N}*_0=
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);}
Try it at: https://dartpad.dartlang.org/36ed632613395303ef51
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);}
}
-
\$\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– Spikatrix2015年06月13日 05:33:16 +00:00Commented Jun 13, 2015 at 5:33
-
\$\begingroup\$ @CoolGuy Miscounted this - now 2 bytes more, but that doesn't change much... \$\endgroup\$Marco13– Marco132015年06月13日 11:45:28 +00:00Commented 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;}tofor(r=R;r-->0;s+=x+n+y+n){x=y=e;for(c=C;c-->0;y+="= "){x+=a;}}\$\endgroup\$Spikatrix– Spikatrix2015年06月13日 14:46:05 +00:00Commented Jun 13, 2015 at 14:46
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.
-
\$\begingroup\$ replace string with var. \$\endgroup\$CSharpie– CSharpie2015年06月12日 19:54:11 +00:00Commented Jun 12, 2015 at 19:54
-
-
1\$\begingroup\$ Declaring those
ints outside the loop can save a byte. \$\endgroup\$Spikatrix– Spikatrix2015年06月13日 05:39:48 +00:00Commented Jun 13, 2015 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)
=================
= = = = =
=================
= = = = =
=================
= = = = =
=================
-
\$\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– Spikatrix2015年06月17日 05:01:37 +00:00Commented Jun 17, 2015 at 5:01
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.
-
\$\begingroup\$ Golf it more by using a
forloop instead of awhileloop. \$\endgroup\$Spikatrix– Spikatrix2015年06月17日 11:09:11 +00:00Commented Jun 17, 2015 at 11:09 -
\$\begingroup\$ Unfortunately for(;w-->0;) is the same length as while(w-->0) \$\endgroup\$ECS– ECS2015年06月17日 11:36:06 +00:00Commented 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– Spikatrix2015年06月17日 12:03:10 +00:00Commented 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– ECS2015年06月17日 12:23:26 +00:00Commented Jun 17, 2015 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:
#############
# # # #
#############
# # # #
#############
# # # #
#############
# # # #
#############
-
\$\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– Spikatrix2015年06月17日 05:02:59 +00:00Commented 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– Fried Egg2015年06月17日 13:11:06 +00:00Commented Jun 17, 2015 at 13:11
-
\$\begingroup\$ Your first link doesn't work for me. Chrome gives
DNS_PROBE_FINISHED_NXDOMAIN\$\endgroup\$Spikatrix– Spikatrix2015年06月20日 06:07:01 +00:00Commented 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– Fried Egg2015年06月20日 14:24:45 +00:00Commented 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– Spikatrix2015年06月21日 05:56:17 +00:00Commented Jun 21, 2015 at 5:56