纸牌屋(第1版)


25

版本2 在这里

简单的挑战:给定一个整数,用给定数目的故事画一张纸牌屋。如果数字为负,则将房子上下颠倒。例子:

Input: 2
Output:

 /\
 --
/\/\

Input: 5
Output:

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

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

Input: -3
Output:

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

输入可以是数字或字符串。输出必须与显示的完全一样,并允许前导和/或尾随空格和换行符。

这是,因此每种语言中最短的程序/功能可能会获胜!


这来自沙盒
查理

允许换行符吗?
毛茸茸的

@Shaggy是的,只要您完全按照所示绘制纸牌屋,您还可以使用领先的空格和换行符。我不在乎它是否未与屏幕左侧对齐。
查理

我们可以抛出错误input=0吗?
罗德

@Rod如果产生空输出,默认情况下
Luis Mendo

Answers:


14

Python 2中97个 95 94 92字节

感谢Luka -2个字节
此版本在上产生异常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ý不起作用,0or将被解释为八进制数字
Rod

减少2个字节:m=abs(n)。然后,而不是xn/m,而不是x*nm
Luka

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不会那么容易... :-)
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↓

在线尝试!链接是详细版本的代码。我有大约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艺术挑战赛中被05AB1E击败O_o

@Gryphon我没有一个字节abs……
Neil

没错,尽管如此,这很奇怪。让您想知道世界即将到来。
狮phon-恢复莫妮卡

是的,这是23字节,内置abs。(恭喜48K,顺便说一句)
ETHproductions'Yul

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


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)&

1

视网膜116 111 105字节

这太久了:/

\d+
$*
+`^~?( *1*)1
 $1¶$&¶_$&
.*$

+`(_.*)1
$1--
1
/\
Ts`/\\`\\/`.*~.*
+`(.*)¶((.*¶)*)(~.*)
$2$4¶$1
~|_

在线尝试!

负输入表示为 ~n


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.