House of Cards (รุ่น 1)


25

รุ่นที่ 2 ที่นี่

ความท้าทายง่าย ๆ : รับจำนวนเต็มวาดบ้านไพ่ด้วยจำนวนเรื่องที่กำหนด หากจำนวนเป็นลบให้วาดบ้านกลับหัวกลับหาง ตัวอย่าง:

Input: 2
Output:

 /\
 --
/\/\

Input: 5
Output:

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

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

Input: -3
Output:

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

อินพุตอาจเป็นตัวเลขหรือสตริง เอาต์พุตต้องตรงตามที่แสดงพร้อมกับช่องว่างนำหน้าและ / หรือช่องว่างและบรรทัดใหม่

นี่คือดังนั้นโปรแกรม / ฟังก์ชั่นที่สั้นที่สุดสำหรับแต่ละภาษาจะชนะ!


นี้มาจากSandbox
Charlie

มีการขึ้นบรรทัดใหม่นำหรือไม่
Shaggy

@Shaggy ใช่คุณสามารถมีช่องว่างและช่องว่างนำขึ้นบรรทัดใหม่ได้ตราบใดที่คุณวาดไพ่ที่บ้านอย่างที่แสดง ฉันไม่คิดว่ามันจะไม่จัดชิดซ้ายของหน้าจอ
Charlie

เราขว้างและทำผิดพลาดไปได้input=0ไหม?
ร็อด

@Rod หากผลิตผลเปล่าจะอนุญาตโดยค่าเริ่มต้น
Luis Mendo

คำตอบ:


14

Python 2 , 97 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ý

@ Zacharýไม่ทำงาน0orจะถูกตีความว่าเป็นเลขแปดตัว
Rod

ตัด 2 m=abs(n)ไบต์เพิ่มเติมได้ที่: จากนั้นแทนที่จะxใส่n/mแทนที่จะx*nใส่m
Luka

9

05AB1E , 30 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 ไบต์

ขึ้นบรรทัดใหม่นำอินพุตเชิงลบ

ป้อน newline ต่อท้ายที่เป็นบวก

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 จะไม่ใช่เรื่องง่าย ... :-)
Charlie

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 Gan

4

ถ่าน, 31 28 27 ไบต์

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

ลองออนไลน์! การเชื่อมโยงคือการใช้รหัสเวอร์ชันอย่างละเอียด ฉันมีคำตอบ 32 ไบต์ที่แตกต่างกันประมาณ 4 ข้อจึงพบสิ่งนี้ แก้ไข: บันทึกแล้ว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↓ฉันรู้ว่าคำตอบที่ถ่านจะจบลงด้วย :-)
Charlie

เมื่อ Charcoal พ่ายแพ้ต่อ 05AB1E ในการแข่งขัน ASCII-art O_o
Gryphon - Reinstate Monica

@Gryphon ฉันไม่มีหนึ่งไบต์abs...
Neil

จริงมันแปลกมากที่ได้เห็นสิ่งนี้ ทำให้คุณสงสัยว่าโลกกำลังจะมาถึง
Gryphon - Reinstate Monica

ใช่นี่เป็น 23 ไบต์ด้วย abs บิวอิน (ขอแสดงความยินดีกับ 48K, btw)
ETHproductions

2

Japté , 40 38 ไบต์

-2 ไบต์ต้องขอบคุณ @Shaggy

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

38 ไบต์
Shaggy

2

Gaia ขนาด 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.