Kartların evi (sürüm 1)


25

Sürüm 2 burada .

Basit zorluk: Bir tam sayı verilirse, verilen sayıda öyküden oluşan bir kart evi çizin. Sayı negatifse, evi baş aşağı çizin. Örnekler:

Input: 2
Output:

 /\
 --
/\/\

Input: 5
Output:

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

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

Input: -3
Output:

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

Giriş sayısal veya bir dize olabilir. Çıktı tam olarak gösterildiği gibi olmalıdır, baştaki ve / veya sondaki boşluklara ve yeni satırlara izin verilir.

Bu , yani her dil için en kısa program / işlev kazanabilir!


Bu kum havuzundan geliyor .
Charlie,

Önde gelen yeni hatlara izin veriliyor mu?
Shaggy

@Shaggy evet, kartların evini aynen gösterildiği gibi çizdiğiniz sürece, önde gelen beyaz boşluklara ve yeni hatlara da sahip olabilirsiniz. Ekranın soluna hizalı olup olmadığını umursamıyorum.
Charlie,

Atıp hata yapabilir miyiz input=0?
Rod,

@Rod Bu boş çıktı üretirse , varsayılan olarak izin verilir
Luis Mendo

Yanıtlar:


14

Python 2 , 97 95 94 92 bayt

Luka sayesinde -2 bayt
Bu sürüm bir istisna oluşturuyor n=0, ancak hiçbir şey yazdırmıyor

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)

Çevrimiçi deneyin!

Hatalı olmayan sürüm, Python 2, 94 bayt

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)

Çevrimiçi deneyin!


x=n>0 or-1=>x=n>0or-1
Zacharý

@ Zacharý çalışmıyor, 0orbir okta sayısı olarak yorumlanacak
Rod

2 daha bayt kesin: m=abs(n). Sonra, yerine xkoymak n/myerine, x*nkoymakm
Luka

9

05AB1E , 30 29 24 bayt

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

Çevrimiçi deneyin!

açıklama

Ä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 bayt

negatif girişli yeni satır girişi

Girdi pozitif son satır

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

Çevrimiçi deneyin!

PHP , 130 bayt

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)."
";

Çevrimiçi deneyin!


5

MATL , 39 bayt

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

Çevrimiçi deneyin!

açıklama

|         % 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
Dostum, bu hızlıydı! İnşallah sürüm 2 bu kadar kolay olmaz ... :-)
Charlie

4

C (gcc) , 169171 173 160 164 bayt

#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,"--")}}

Negatif durum hatası için +13 bayt.

Çevrimiçi deneyin!

Ungolfed (tüm alanları ve yeni satırı kaldırdıktan sonra 207 bayt):

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 düzeltildi! teşekkürler
Keyu Gan

4

Kömür, 31 28 27 bayt

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

Çevrimiçi deneyin! Bağlantı, kodun ayrıntılı bir versiyonudur. Daha sonra bunu 4 farklı 32 byte cevabı buldum. Düzenleme: dize manipülasyon kullanarak gerçekleştirerek 3 4 bayt kaydedildi abs. Açıklama:

   ⪪θ-                          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

Kömür cevabının biteceğini biliyordum ¿‹θ⁰‖T↓. :-)
Charlie,

Kömür bir ASCII-art mücadelesinde 05AB1E tarafından yenildiğinde O_o
Gryphon - Reinstate Monica

@Gryphon Bir baytım yok abs...
Neil

Doğru, bunu görmek çok garip. Dünyanın ne olacağını merak ettin.
Gryphon - Reinstate Monica

Evet, bu abs yerleşik bir 23 byte olur. (Tebrikler 48K, btw)
ETHproductions

2

Japt , 40 38 bayt

@Shaggy sayesinde -2 bayt

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

Çevrimiçi deneyin!

açıklama

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

Gaia , 21 bayt

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

açıklama

:                      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 bayt

(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

Retina , 116 111 105 bayt

bu çok uzun sürdü:

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

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

Çevrimiçi deneyin!

negatif girdi olarak gösterilir ~n


Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.