Draw an ASCII Rectangle
Jelly, 14 bytes
,þ%,ỊḄị“-|| ”Y
Try it online! or verify all test cases.
How it works
,þ%,ỊḄị“-|| ”Y Main link. Left argument: w. Right argument: h
,þ Pair table; yield a 2D array of all pairs [i, j] such that
1 ≤ i ≤ w and 1 ≤ j ≤ h.
, Pair; yield [w, h].
% Take the remainder of the element-wise division of each [i, j]
by [w, h]. This replaces the highest coordinates with zeroes.
Ị Insignificant; map 0 and 1 to 1, all other coordinates to 0.
Ḅ Unbinary; convert each pair from base 2 to integer.
[0, 0] -> 0 (area)
[0, 1] -> 1 (top or bottom edge)
[1, 0] -> 2 (left or right edge)
[1, 1] -> 3 (vertex)
“-|| ” Yield that string. Indices are 1-based and modular in Jelly, so the
indices of the characters in this string are 1, 2, 3, and 0.
ị At-index; replace the integers by the correspoding characters.
Y Join, separating by linefeeds.
Matlab, 69 65 56 bytes
Thanks @WeeingIfFirst and @LuisMendo for some bytes=)
function z=f(a,b);z(b,a)=' ';z([1,b],:)=45;z(:,[1,a])='|'
This is really simple in Matlab: First make a matrix of the desired size, then index the first and last row to insert the -
, and do the same with the first and last column to insert |
.
For example f(4,3)
returns
|--|
| |
|--|
PHP, 74 Bytes
for(;$i<$n=$argv[2];)echo str_pad("|",$argv[1]-1,"- "[$i++&&$n-$i])."|\n";