画像の出力視覚図


22

フレーム付きポートレートの絵画の寸法、マット幅、およびフレーム幅を入力するプログラムを作成します。プログラムはX、ペイントのシンボルを使用してダイアグラムを出力する必要があります。+マッ#フレーミングがあります。記号はスペースで区切る必要があります。出力が視覚的に基準に一致する限り、末尾の空白は問題ありません。入力はにすることができます0

入力:3 2 1 2(幅、高さ、マット幅、フレーム幅)

出力:

最初の3と2は、ペイントの幅と高さです。 1はその周りのマット幅です。 2は全体の周囲のフレーム幅です。

テキスト形式:

# # # # # # # # #
# # # # # # # # #
# # + + + + + # #
# # + X X X + # #
# # + X X X + # #
# # + + + + + # #
# # # # # # # # #
# # # # # # # # #

勝ったコードは、最小限のバイトで条件を完了します。


2
ナイスチャレンジ!将来の課題のために、Sandbox
MilkyWay90

2
入力の順序が異なっていても構いませんか?
vityavv

1
文字列のリストを返すことはできますか?
MilkyWay90

5
入力のいずれかをゼロにすることはできますか?
ライコニ

1
各行の最後にスペースを入れることはできますか?
ルイスメンドー

回答:


5

Python 2、98バイト

w,h,a,b=input()
a*='+'
b*='#'
for c in b+a+h*'X'+a+b:print' '.join(min(c,d)for d in b+a+w*'X'+a+b)

オンラインでお試しください!

厳密に仕様に従って、スペースで区切られたグリッドを印刷します。数値を文字列*=に変換しaたりb、数値から文字列に変換したりするのが面白かったです。

Python 3 ' '.joinでは、f文字列と代入式を使用することにより、を回避することにより、いくつかのバイトを節約できます。-2バイトのJo Kingに感謝します。

Python 3、93バイト

def f(w,h,a,b):a*='+';b*='#';[print(*[min(c,d)for d in b+a+w*'X'+a+b])for c in b+a+h*'X'+a+b]

オンラインでお試しください!


私はアウトゴルフされました!素敵な仕事、かなりゴルフのようです
MilkyWay90

素敵なゴルフ!非常に賢い。
ジョージハリス

4

JavaScriptの(ES6)、 118の113  107バイト

(w,h,M,F)=>(g=(c,n)=>'01210'.replace(/./g,i=>c(i).repeat([F,M,n][i])))(y=>g(x=>'#+X'[x<y?x:y]+' ',w)+`
`,h)

オンラインでお試しください!

コメント済み

(w, h, M, F) => (       // given the 4 input variables
  g = (                 // g = helper function taking:
    c,                  //   c = callback function returning a string to repeat
    n                   //   n = number of times the painting part must be repeated
  ) =>                  //
    '01210'             // string describing the picture structure, with:
    .replace(           //   0 = frame, 1 = matte, 2 = painting
      /./g,             // for each character in the above string:
      i =>              //   i = identifier of the current area
        c(i)            //   invoke the callback function
        .repeat         //   and repeat the result ...
        ([F, M, n][i])  //   ... either F, M or n times
    )                   // end of replace()
)(                      // outer call to g:
  y =>                  //   callback function taking y:
    g(                  //     inner call to g:
      x =>              //       callback function taking x:
        '#+X'           //         figure out which character to use
        [x < y ? x : y] //         according to the current position
        + ' ',          //         append a space
      w                 //       repeat the painting part w times
    )                   //     end of inner call
    + '\n',             //     append a line feed
  h                     //   repeat the painting part h times
)                       // end of outer call

3

MATL、24バイト

&l,ithYaQ]'#+X'w)TFX*cYv

入力は、高さ、幅、マット幅、フレーム幅です。

オンラインでお試しください!

説明

&l      % Take height and width implicitly. Push matrix of that size with all
        % entries equal to 1
,       % Do twice
  i     %   Take input
  th    %   Duplicate, concatenate: gives a 1×2 vector with the number repeated
  Ya    %   Pad matrix with those many zeros vertically and horizontally
  Q     %   Add 1 to each entry 
]       % End
'#+X'   % Push this string
w)      % Index into the string with the padded matrix
TF      % Push row vector [1 0]
X*      % Kronecker product. This inserts columns of zeros
c       % Convert to char again. Char 0 is will be displayed as space
Yv      % Remove trailing spaces in each line. Implicitly display


2

48 47 44バイト

≔×NXθ≔×NXηFE+#×Nι«≔⁺ι⁺θιθ≔⁺ι⁺ηιη»Eη⪫⭆θ⌊⟦ιλ⟧ 

オンラインでお試しください!リンクは、コードの詳細バージョンです。注:末尾のスペース。編集:@xnorのアルゴリズムを使用するようになりました。説明:

≔×NXθ≔×NXη

幅と高さを入力し、Xsの文字列に変換します。

FE+#×Nι

文字+とをループし、#残りの2つの入力で指定された長さの文字列に変換します。次に、これら2つの文字列をループします。

«≔⁺ι⁺θιθ≔⁺ι⁺ηιη»

マットとフレーミング用の文字列を使用して、ペイントのプレフィックスとサフィックスを付けます。

Eη⪫⭆θ⌊⟦ιλ⟧ 

文字列をループし、最小の水平および垂直文字を取得し、行を二重に間隔を空けて、各行を独自の行に暗黙的に印刷します。


2

05AB1E(レガシー) / 05AB1E --no-lazy32 31 バイト

и'Xׄ+#vyI©×UεX.ø}®FDнgy×.ø]€S»

入力を順番に受け取りますheight, width, matte, frame。チャレンジで指定された入力順序が厳密な場合(OPが検証を待機している場合)、先頭s(スワップ)を+1バイトに追加できます。

--no-lazy05AB1Eの新しいバージョンではElixirコンパイラフラグが必要でした。これは、Elixirがネストされたマップ/ループの遅延評価のために奇妙な動作をするためです(ここではこのフラグのない結果)。

05AB1Eのレガシーバージョンでオンラインで試してください。フラグ
が追加された05AB1Eの新しいバージョンでオンラインで試してください--no-lazy

説明:

и              # Repeat the second (implicit) input the first (implicit) input amount of
               # times as list
 'X×          '# Repeat "X" that many times
„+#v           # Loop `y` over the characters ["+","#"]:
    y          #  Push character `y`
     I         #  Push the next input (matte in the first iteration; frame in the second)
      ©        #  And store it in the register (without popping)
       ×       #  Repeat character `y` that input amount of times
        U      #  Pop and store that string in variable `X`
    εX.ø}      #  Surround each string in the list with string `X`
    ®F         #  Inner loop the value from the register amount of times:
      Dнg      #   Get the new width by taking the length of the first string
         y×    #   Repeat character `y` that many times
             #   And surround the list with this leading and trailing string
   ]           # Close both the inner and outer loops
    S         # Convert each inner string to a list of characters
      »        # Join every list of characters by spaces, and then every string by newlines
               # (and output the result implicitly)




1

Python 3.8(プレリリース)116 115 113バイト

lambda a,b,c,d:"\n".join((g:=['#'*(a+2*c+2*d)]*d+[(h:='#'*d)+'+'*(a+c*2)+h]*c)+[h+'+'*c+'X'*a+'+'*c+h]*b+g[::-1])

オンラインでお試しください!

ゴルフの最初の試みは、すぐに改善されます。 a幅、b高さ、cマット幅、dフレーム幅です。

-1バイトとして:=定義する演算子を使用he * d

-2バイト、eキングとfパラメータを削除するように勧めてくれたJo Kingに感謝

説明:

lambda a,b,c,d:          Define a lambda which takes in arguments a, b, c, and d (The width of the painting, the height of the painting, the padding of the matte, and the padding of the frame width, respectively).
    "\n".join(                       Turn the list into a string, where each element is separated by newlines
        (g:=                         Define g as (while still evaling the lists)...
            ['#'*(a+2*c+2*d)]*d+       Form the top rows (the ones filled with hashtags)
            [(h:='#'*d)+'+'*(a+c*2)+h]*c Form the middle-top rows (uses := to golf this section)
        )+
        [h+'+'*c+'X'*a+'+'*c+h]*b+       Form the middle row
        g[::-1]                      Uses g to golf the code (forms the entire middle-bottom-to-bottom)
    )

e割り当てを削除すると2バイトf節約されますが、割り当てでは何も節約されません
Jo King

@JoKingああすごい、私は削除するのを忘れた ef発見した後の割り当てをgショートカットを
MilkyWay90

0

Javascript、158バイト

(w,h,m,f)=>(q="repeat",(z=("#"[q](w+2*(m+f)))+`
`)[q](f))+(x=((e="#"[q](f))+(r="+"[q](m))+(t="+"[q](w))+r+e+`
`)[q](m))+(e+r+"X"[q](w)+r+e+`
`)[q](h)+x+z)

おそらく少しトリミングすることができます

f=

(w,h,m,f)=>(q="repeat",(z=("# "[q](w+2*(m+f))+`
`)[q](f))+(x=((e="# "[q](f))+(r="+ "[q](m))+(t="+ "[q](w))+r+e+`
`)[q](m))+(e+r+"X "[q](w)+r+e+`
`)[q](h)+x+z)

console.log(f(3,2,1,2))


0

Perl 6、98バイト

{map(&min,[X] map (($_='#'x$^d~'+'x$^c)~'X'x*~.flip).comb,$^a,$^b).rotor($b+2*($c+$d)).join("\n")}

オンラインでお試しください!

これはxnorのPython answerの移植版です

Perl 6 6、115バイト

->\a,\b,\c,\d{$_=['#'xx$!*2+a]xx($!=c+d)*2+b;.[d..^*-d;d..^a+$!+c]='+'xx*;.[$!..^*-$!;$!..^a+$!]='X'xx*;.join("
")}

オンラインでお試しください!

Perl 6の多次元リスト割り当てを使用して、大まかにゴルフした匿名コードブロック。たとえば、インデックス1のリストからインデックス2の要素に@a[1;2] = 'X';割り当て、リストのインデックスを持つすべての要素をインデックスに置き換えます'X'@a[1,2,3;3,4,5]='X'xx 9;3,4,51,2,3とします'X'

説明:

最初に、リストをsのa+2*(c+d)by b+2*(c+d)長方形として初期化します#

$_=['#'xx$!*2+a]xx($!=c+d)*2+a;
State:
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #

次に、+sの内側の長方形を割り当てます

.[d..^*-d;d..^a+$!+c]='+'xx*;
State:
# # # # # # # # #
# # # # # # # # #
# # + + + + + # #
# # + + + + + # #
# # + + + + + # #
# # + + + + + # #
# # # # # # # # #
# # # # # # # # #

最後に、Xsの最も内側の長方形。

.[$!..^*-$!;$!..^a+$!]='X'xx*;
# # # # # # # # #
# # # # # # # # #
# # + + + + + # #
# # + X X X + # #
# # + X X X + # #
# # + + + + + # #
# # # # # # # # #
# # # # # # # # #

0

ゼリー35 31 28 24バイト

Dịx@“#+X+#”
⁽-Fç«þ⁽-ȥç$G

オンラインでお試しください!

入力をフレーム、マット、幅、高さの順に受け取ります。カンマ区切り。フレームとマット付きのASCIIアート画像を出力します。入力順序が厳しい場合は、元の投稿に従って、さらにバイトを追加する必要があります。

@EriktheOutgolferの回答に基づくゴルフのカップル。文字がASCII順になっていることに気づきましたが、それを最大限に活用する方法を考えていなかったため、を忘れていましたG。それでも彼の方が良い答えです!


Jellyでプログラムすることはありませんが、43134,43234は圧縮できますか?編集:私は読むことを学ぶ必要があります、あなたは彼らが実際にそれぞれ4バイトでエンコードできることに言及しています。しかし、これらの数値をエンコードできるかどうかに関係する入力順序は何ですか?:S
ケビンクルーッセン

@KevinCruijssen 2バイト構文を使用してエンコードできる最大整数は32250です。どちらも超えているため、バイトを保存できません。今のところ、私は物事を交換し、許可されていない場合は元に戻すことができると仮定します!
ニックケネディ

ああ、わかった。43134エンコード文字を3つ必要とします。エンコード文字は、エンコードされていることを示す先頭/末尾の文字も含めて5バイトになります。また、2番目の数値は最初の数値より100大きいため、Jellyには何らかの種類の重複があるのでしょうか?アクションが43134をプッシュし、複製し、100をプッシュし、さらに、ゼリーでペアが可能かつ短くなるかどうかわかりませんか?
ケビンクルーイッセン

@KevinCruijssen私はもともと+0,100を使って試しましたが、何も保存されません。nilad³では100であるという事実を使用するためにniladチェーンを使用できると思いますが、入力を並べ替えることができる場合は、基本250整数の方が優れています
Nick Kennedy


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.