カードの家(バージョン1)


25

バージョン2はこちら

簡単な課題:整数を指定すると、指定した数のストーリーを持つカードの家を描きます。数値が負の場合、家を上下逆さまに描きます。例:

Input: 2
Output:

 /\
 --
/\/\

Input: 5
Output:

    /\
    --
   /\/\
   ----
  /\/\/\
  ------
 /\/\/\/\
 --------
/\/\/\/\/\

Input: 0
Output: <empty, whitespace or newline>

Input: -3
Output:

\/\/\/
 ----
 \/\/
  --
  \/

入力は数値または文字列にすることができます。出力は示されているとおりで、先頭および/または末尾のスペースと改行が許可されている必要があります。

これはなので、各言語の最短のプログラム/機能が勝つかもしれません!


これはサンドボックスから来ます。
チャーリー

主要な改行は許可されていますか?
シャギー

@Shaggyはい、示されているとおりにカードの家を描く限り、先頭に空白や改行を入れることもできます。画面の左に揃えられていなくても構いません。
チャーリー

スローしてエラーを発生させることはできますinput=0か?
ロッド

回答:


14

Pythonの297 95 94 92バイト

-2バイト、Lukaのおかげ
このバージョンでは、例外を生成しますがn=0、何も印刷しません。

n=input()*2
m=abs(n)
for i in range(2,m+1)[::n/m]:print(i/2*'/-\-'[i%2::2][::n/m]).center(m)

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

非エラーバージョン、Python 2、94バイト

n=input()*2
x=n>0 or-1
for i in range(2,x*n+1)[::x]:print(i/2*'/-\-'[i%2::2][::x]).center(n*x)

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


x=n>0 or-1=>x=n>0or-1
ザカリー

@Zacharýは機能せず、8進0or数として解釈されます
ロッド

さらに2バイトをカットしますm=abs(n)。次に、xputの代わりにputのn/m代わりx*nm
ルカ

9

05AB1E30 29 24バイト

ÄF„--Nׄ/\N>×}).C∊2ä¹0‹è

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

説明

ÄF                         # for N in [0 ... abs(input-1)] do:
  „--N×                    # push the string "--" repeated N times
       „/\N>×              # push the string "/\" repeated N+1 times
             }             # end loop
              )            # wrap stack in a list
               .C          # pad strings on both sides to equal length
                 ∊         # vertically mirror the resulting string
                  2ä       # split in 2 parts
                    ¹0‹    # push input < 0
                       è   # index into the the list with the result of the comparison

7

PHP、125バイト

負の先行改行を入力します

正の末尾改行を入力します

for($s=str_pad;++$i<$b=2*abs($argn);)$t.=$s($s("",2*ceil($i/2),["-","/\\"][1&$i]),$b," ",2)."
";echo$argn>0?$t:$t=strrev($t);

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

PHP、130バイト

for(;++$i<$b=2*abs($a=$argn);)echo($s=str_pad)($s("",2*abs(($a<0?$a:$i&1)+($i/2^0)),["-",["/\\","\/"][0>$a]][1&$i]),$b," ",2)."
";

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


5

MATL、39バイト

|:"G|@-:~'/\'G0<?P]@E:)htg45*c]xXhG0<?P

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

説明

|         % Implicitly input, N. Absolute value
:"        % For k from 1 to that
  G|      %   Push absolute value of N again
  @-      %   Subtract k
  :       %   Range [1 2 ... N-k]
  ~       %   Convert to vector of N-k zeros
  '/\'    %   Push this string
  G0<     %   Is input negative?
  ?       %   If so
    P     %     Reverse that string (gives '\/')
  ]       %   End
  @E      %   Push 2*k
  :       %   Range [1 2 ... 2*k]
  )       %   Index (modularly) into the string: gives '/\/\...' or '\/\/...'
  h       %   Horizontally concatenate the vector of zeros and the string. Zeros
          %   are implicitly converted to char, and will be shown as spaces
  t       %   Duplicate
  g       %   Convert to logical: zeros remain as 0, nonzeros become 1
  45*c    %   Multiply by 45 (ASCII for '=') and convert to char
]         % End
x         % Delete (unwanted last string containing '=')
Xh        % Concatenate into a cell array
G0<       % Is input negative?
?         % If so
  P       %   Reverse that cell array
          % Implicit end. Implicit display

1
男、それは速かった!! バージョン2がそれほど簡単ではないことを願っています... :
チャーリー

4

C(gcc)、169171 173 160 164 バイト

#define F(A,B,C)for(i=A;B--;)printf(C);
#define P puts("");F(y,i," ")F(abs(n)-y
s,i,x,y;f(n){x=n<0;for(s=x?1-n:n;s--;){y=x?-n-s:s;P,i,x?"\\/":"/\\")y+=x;P,s>x&&i,"--")}}

負のケースバグの場合は+13バイト。

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

Ungolfed(すべてのスペースと改行を削除した後の207バイト):

s, i, x, y;
f(n) {
  x = n < 0;
  for (s = x ? 1 - n : n; s--;) {
    y = x ? - n - s : s;
    puts("");
    for (i = y; i--;) printf(" ");
    for (i = abs(n) - y; i--;) printf(x ? "\\/" : "/\\");;
    y += x;
    puts("");
    for (i = y; i--;) printf(" ");
    for (i = abs(n) - y; s > x && i--;) printf("--");;
  }
}

1
@officialaimm修正済み!感謝
Keyuガン

4

木炭、31 28 27バイト

FI⊟⪪θ-«←ι↓→/…\/ι↙»‖M¿‹N⁰‖T↓

オンラインでお試しください!リンクは、コードの詳細バージョンです。約4種類の32バイトの回答があり、これを見つけました。編集:文字列操作を使用して3 4バイトを保存しましたabs。説明:

   ⪪θ-                          Split the input (θ = first input) on -
  ⊟                             Take the (last) element
 I                              Convert it to a number i.e. abs(θ)
F     «                         Repeat that many times
       ←ι                       Print half of the -s
         ↓                      Position for the /\s
          →/                    Print the first /
            …\/ι                Print half of any remaining \/s
                ↙               Position for the next row of -s
                 »              End of the loop
                  ‖M            Mirror everything horizontally
                    ¿‹N⁰        If the input was negative
                        ‖T↓     Reflect everything vertically

木炭の答えはで終わることを知っていました¿‹θ⁰‖T↓。:
チャーリー

ASCIIアートチャレンジO_oで木炭が05AB1Eにbeatられたとき
グリフォン-モニカの復活

私は、1バイトを持っていない@Gryphon abs...
ニール

確かに、これを見るのは奇妙です。世界がどうなるのか不思議に思うでしょう。
グリフォン-モニカの復活

ええ、absが組み込まれた23バイトです。(おめでとう、48K、ところで)
-ETHproductions

2

Japt40 38バイト

@Shaggyのおかげで-2バイト

o½½@aXc)ç +"--/\\\\/"ò gYv *Ug)pXc a÷

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

説明

o½½@aXc)ç +"--/\\\\/"ò gYv *Ug)pXc a÷              // implicit: U = input integer
o.5,.5,XYZ{UaXc)ç +"--/\\\\/"ò gYv *Ug)pXc a} qR    // ungolfed
o.5,.5,                                             // array [.5,U] with step size .5
       XYZ{                                 }       // mapped by the function: (X = value, Y = index)
           UaXc)                                    //   absolute diff between U and ceil(X)
                ç                                   //   " " times that value
                  +"--/\\\\/"ò g      )             //   plus ["--","/\","\/"].get(...
                                Yv                  //     if Y is even, 1, else 0
                                   *Ug              //     times sign(U)
                                       pXc a        //   repeated abs(ceil(X)) times
                                              qR    // all that joined with newlines


2

ガイア、21 バイト

:┅“/\\“--”צ¦_€|ḣ¤ọ×ṣ

説明

:                      Push 2 copies of the input
 ┅                     Get the range to the input. If positive: [1 .. n]. If negative: 
                       [-1 .. n]. If zero: [0].
  “/\\“--”             Push ["/\", "--"]
          צ¦          Repeat both of those strings by each number in the range. Strings go
                       in reverse order when repeated a negative number of times.
             _         Flatten the list
              €|       Centre-align the rows of the list
                ḣ      Remove the last row (the "--"s on the bottom)
                 ¤     Swap (bring input back to the top)
                  ọ    Sign: -1 for negative, 0 for 0, 1 for positive
                   ×   Repeat the list that many times; (-1 × list) reverses it
                    ṣ  Join with newlines and implicitly output

1

Mathematica、140バイト

(T=Table;z=Column;B[a_]:=""<>"/\\"~T~a;If[#>0,m=0,m=Pi];z[Join[z/@T[{B@i,""<>"--"~T~i},{i,Abs@#-1}],{B@Abs@#}],Alignment->Center]~Rotate~m)&


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