回答:
,ajJ'|
ここで試してみてください!私のボックスビルトインがついに重宝しました!:D
,ajJ'|
,a draw a box
j with width (input 1)
J and height (input 2)
' with options
| - corner
- the rest are defaults
,þ%,ỊḄị“-|| ”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
|--------|
| |
|--------|
ヘルパー関数f
は、2つの要素のリスト[a,b]
と数値n
を受け取り、1つa
、n-2
b
s 、1の順に続くリストを返しますa
。f
thrice:を使用して、上/下の行:f "|-" i
、中間の行:f "| " i
を作成し、これら2つから長方形全体を作成できますf [<top>,<middle>] j
(注:部分的なアプリケーションのためj
、パラメーターとして表示されませんg i
)。
編集:@dianneは、2つの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
テストケースはイデオンにあります
('- '[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
Adnanのおかげで2バイト節約
F„ -N_N¹<Q~è²Í×'|.ø,
。
w,h=input()
for c in'-%*c'%(h-1,45):print'|'+c*(w-2)+'|'
flornquakeは1バイトを節約しました。
%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
ます。繰り返しごとに、単一の文字列を作成します。真ん中は、2つの単一の長さの文字列の配列から選択され、$a-2
パイプで囲まれている間、で文字列が乗算されます。結果の文字列はパイプラインに残り、暗黙的な出力Write-Output
はプログラムの完了時に発生し、デフォルトの改行セパレーターが使用されます。
あるいは、55バイトでも
param($a,$b)1..$b|%{"|$((''+' -'[$_-in1,$b])*($a-2))|"}
これは、代わりに文字列を使用して中央の配列選択をゴルフしようとしていたために生じました。ただし、[char]
時間[int]
は定義されていないため、かっこと文字列としてキャストする必要があるため、節約量を失うことになります。''+
ます。
どちらのバージョンでも、-in
オペレーターにはv3以降が必要です。
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
];
Oh, that's a lot simpler :)
->x,y{y.times{|i|puts"|#{(-~i%y<2??-:' ')*(x-2)}|"}}
\n
.
i
and j
. Replace i
's definition with x-=2
. Instead of j
, just use (y-2)
.
Includes +1 for -n
Give sizes as 2 lines on STDIN
perl -nE 'say"|".$_ x($`-2)."|"for"-",($")x(<>-1-/$/),"-"'
3
8
^D
Just the code:
say"|".$_ x($`-2)."|"for"-",($")x(<>-1-/$/),"-"
Saved quite a few bytes by removing stupid over complexities.
function(w,h)function g(s)return'|'..s:rep(w-2)..'|\n'end b=g'-'print(b..g' ':rep(h-2)..b)end
Ungolfed:
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
Z"45ILJhY('|'5MZ(
This is a slightly different approach than the one of the MATL-God.
Z" Make a matrix of spaces of the given size
45ILJhY( Fill first and last row with '-' (code 45)
'|'5MZ( Fill first and last column with '|' (using the automatic clipboard entry 5M to get ILJh back)
Thanks @LuisMendo for all the help!
<?$R=str_repeat;echo$l="|{$R('-',$w=$W-2)}|
",$R("|{$R(' ',$w)}|
",$H-2),$l;
This assumes you have the default php.ini
settings for this version, including short_open_tag
and register_globals
enabled.
This requires access through a web server (e.g.: Apache), passing the values over session/cookie/POST/GET variables.
The key W
controls the width and the key H
controls the height.
For example: 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
let v = {(c:String,n:Int) -> String in var s = "";for _ in 1...n {s += c};return s;};_ = {var s = "|"+v("-",$0-2)+"|\n" + v("|"+v(" ",$0-2)+"|\n",$1-2) + "|"+v("-",$0-2)+"|";print(s);}(10,5)
I think Swift 3 could golf this a lot more but I don't feel like downloading Swift 3.
Ị
:)の素晴らしい使用です。-