Answers:
,þ%,ỊḄị“-|| ”Y
,þ%,ỊḄị“-|| ”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.
感谢@WeeingIfFirst和@LuisMendo的一些字节=)
function z=f(a,b);z(b,a)=' ';z([1,b],:)=45;z(:,[1,a])='|'
在Matlab中,这确实很简单:首先创建所需大小的矩阵,然后索引第一行和最后一行以插入,然后对第一行和最后一列进行-
相同的操作|
。
例如f(4,3)
回报
|--|
| |
|--|
z([1,b],1:a)=45;z(1:b,[1,a])=124;z=[z,'']
z(b,a)=' ';z([1,b],:)=45;z(:,[1,a])=124
z(b,a)=' '
初始化为char。之后,您可以填写数字,它们会自动转换为char。z
保持其原始类型
f[a,b]n=a:(b<$[3..n])++[a]
g i=unlines.f[f"|-"i,f"| "i]
用法示例:
*Main> putStr $ g 10 3
|--------|
| |
|--------|
helper函数f
接受一个由两个元素组成的列表[a,b]
和一个数字,n
并返回一个列表,列表由1 a
,n-2
b
s和1组成a
。我们可以使用f
thrice:来构建顶线/底线:f "|-" i
,中线:f "| " i
并从这两个中创建整个矩形:(f [<top>,<middle>] j
注意:由于部分应用,j
它不会作为参数出现g i
)。
编辑:@dianne通过将两个Char
参数合并String
为长度2 来保存一些字节。非常感谢!
#
主意!
(a:b)#n=a:([3..n]>>b)++[a]
和写入来节省一些字节["|-"#i,"| "#i]#j
@flornquake -3个字节(删除不必要的括号;h
用作计数器)
def f(w,h):exec"print'|'+'- '[1<h<%d]*(w-2)+'|';h-=1;"%h*h
测试用例在ideone
('- '[1<i<h])
不需要括号。
exec"print'|'+'- '[1<h<%d]*(w-2)+'|';h-=1;"%h*h
h
计数器是聪明!谢谢。
fu A(...)
exe "norm ".a:1."i|\ehv0lr-YpPgvr dd".a:2."p2dd"
endf
:call A(3,3)
fun A(...) " a function with unspecified params (a:1 and a:2)
exe " exe(cute) command - to use the parameters we must concatenate :(
norm " run in (norm) al mode
#i| " insert # vertical bars
\e " return (`\<Esc>`) to normal mode
hv0l " move left, enter visual mode, go to the beginning of the line, move right (selects inner `|`s)
r- " (r)eplace the visual selection by `-`s
YpP " (Y) ank the resulting line, and paste them twice
gv " re-select the previous visual selection
r<Space> " replace by spaces
dd " Cut the line
#p " Paste # times (all inner rows)
2dd " Remove extra lines
请注意,它没有使用,norm!
因此可能会干扰vim自定义映射!
'|-| '2:"iqWQB]E!+)
该方法类似于该其他答案中使用的方法。该代码构建了以下形式的数字数组
3 2 2 2 3
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
3 2 2 2 3
然后将其值用作字符串中的(基于1的,模块化的)索引,'|-| '
以产生所需的结果。
'|-| ' % Push this string
2:" ] % Do this twice
i % Take input
q % Subtract 1
W % 2 raised to that
Q % Add 1
B % Convert to binary
E % Multiply by 2
! % Transpose
+ % Add with broadcast
) % Index (modular, 1-based) into the string
输入为高度,然后为宽度。
F„ -N_N¹<Q~è²Í×'|.ø,
说明
F # height number of times do
N_ # current row == first row
~ # OR
N¹<Q # current row == last row
„ - è # use this to index into " -"
²Í× # repeat this char width-2 times
'| # push a pipe
.ø # surround the repeated string with the pipe
, # print with newline
由于节省了2个字节 Adnan,
F„ -N_N¹<Q~è²Í×'|.ø,
。
w,h=input()
for c in'-%*c'%(h-1,45):print'|'+c*(w-2)+'|'
flornquake保存了一个字节。
%c
转换保存一个字节:'-%*c'%(h-1,45)
%*c
根本没有东西!谢谢。:)
'-%%%dc'%~-h%45
也适用于相同的长度。
打高尔夫球:
(defun a(w h)(flet((f(c)(format t"|~v@{~A~:*~}|~%"(- w 2)c)))(f"-")(loop repeat(- h 2)do(f" "))(f"-")))
取消高尔夫:
(defun a (w h)
(flet ((f (c) (format t "|~v@{~A~:*~}|~%" (- w 2) c)))
(f "-")
(loop repeat (- h 2) do
(f " "))
(f "-")))
解释是稍微不再窃听
?;,u[*'|u]'|?@-[*:l'|l[|,l]d@ ],ur[|'-r]
? - input integer into register
; - move down by the contents of register
, - write the char variable, default *
u - move up
[* ] - while current cell is not *
'| - write |
u - move up
'| - write | again
? - input other integer into register
@- - set char variable to -
[* ] - while current char is not *
:l'|l - move right by amount in register, move left, write |, move left again
[|,l] - while current cell is not |, write char variable, move left
d@ - move down, set char variable to space (this means all but first iteration of loop writes space)
,ur -write char variable, move up, right
[| ] -while current char is not |
'-r - write -, move right
(来自@ mbomb007的反馈:-9个字节)
def d(x,y):return'\n'.join(('|'+('-'*(x-2)if n<1or n==~-y else' '*(x-2))+'|')for n in range(y))
(我的第一个代码高尔夫球,感谢反馈)
range(y)
代替range(0,y)
,如果n
从不为负,则可以使用if n<1or n==~-y else
public String rect(int x, int y){
String o="";
for(int i=-1;++i<y;){
o+="|";
for(int j=2;++j<x)
if(i<1||i==y-1)
o+="-";
else
o+=" ";
o+="|\n";
}
return o;
}
打高尔夫球:
String r(int x,int y){String o="";for(int i=-1;++i<y;){o+="|";for(int j=2;++j<x;)if(i<1||i==y-1)o+="-";else o+=" ";o+="|\n";}return o;}
o+=x "|\n"
?你是说把那放在+
那里吗?
param($a,$b)1..$b|%{"|$((' ','-')[$_-in1,$b]*($a-2))|"}
接受输入$a
和$b
。从循环1
到$b
。每次迭代,我们都构造一个字符串。从两个单长度字符串的数组中选择中间,然后将乘以,然后将$a-2
其用管道包围。结果字符串留在管道上,并通过隐式输出Write-Output
在程序完成时使用默认的换行符分隔符。
或者,也为55个字节
param($a,$b)1..$b|%{"|$((''+' -'[$_-in1,$b])*($a-2))|"}
之所以如此,是因为我试图通过使用字符串代替中间的阵列选择。但是,由于[char]
时间[int]
未定义,因此我们需要使用parens和转换为字符串,从而浪费了节省的时间''+
。
两种版本都要求v3或更高版本-in
。
PS C:\Tools\Scripts\golfing> .\draw-an-ascii-rectangle.ps1 10 3
|--------|
| |
|--------|
PS C:\Tools\Scripts\golfing> .\draw-an-ascii-rectangle.ps1 7 6
|-----|
| |
| |
| |
| |
|-----|
list(,$w,$h)=$argv;for($p=$h--*$w;$p;)echo$p--%$w?$p%$w?$p/$w%$h?" ":"-":"|
":"|";
索引包含换行符的静态字符串
list(,$w,$h)=$argv; // import arguments
for($p=$h--*++$w;$p;) // loop $p through all positions counting backwards
// decrease $h and increase $w to avoid parens in ternary conditions
echo" -|\n"[
$p--%$w // not (last+1 column -> 3 -> "\n")
? $p%$w%($w-2) // not (first or last row -> 2 -> "|")
?+!($p/$w%$h) // 0 -> space for not (first or last row -> 1 -> "-")
:2
:3
];
包括+1 -n
在STDIN上将大小设为2行
perl -nE 'say"|".$_ x($`-2)."|"for"-",($")x(<>-1-/$/),"-"'
3
8
^D
只是代码:
say"|".$_ x($`-2)."|"for"-",($")x(<>-1-/$/),"-"
通过消除愚蠢的复杂性节省了很多字节。
function(w,h)function g(s)return'|'..s:rep(w-2)..'|\n'end b=g'-'print(b..g' ':rep(h-2)..b)end
取消高尔夫:
function(w,h) -- Define Anonymous Function
function g(s) -- Define 'Row Creation' function. We use this twice, so it's less bytes to function it.
return'|'..s:rep(w-2)..'|\n' -- Sides, Surrounding the chosen filler character (' ' or '-'), followed by a newline
end
b=g'-' -- Assign the top and bottom rows to the g of '-', which gives '|---------|', or similar.
print(b..g' ':rep(h-2)..b) -- top, g of ' ', repeated height - 2 times, bottom. Print.
end
<?$R=str_repeat;echo$l="|{$R('-',$w=$W-2)}|
",$R("|{$R(' ',$w)}|
",$H-2),$l;
假定您具有php.ini
该版本的默认设置,包括short_open_tag
和register_globals
启用。
这需要通过Web服务器(例如Apache)进行访问,并将值传递到session / cookie / POST / GET变量中。
按键W
控制宽度,按键H
控制高度。
例如:http://localhost/file.php?W=3&H=5
p="|"
def r(w,h):m=w-2;b=p+"-"*m+p;return b+"\n"+(p+m*" "+p+"\n")*(h-2)+b
Ị
:)的一种出色用法: