Kartenhaus (Version 1)

25

Version 2 hier .

Einfache Herausforderung: Ziehen Sie mit einer Ganzzahl ein Kartenhaus mit der angegebenen Anzahl von Geschichten. Wenn die Zahl negativ ist, stellen Sie das Haus auf den Kopf. Beispiele:

Input: 2
Output:

 /\
 --
/\/\

Input: 5
Output:

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

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

Input: -3
Output:

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

Die Eingabe kann numerisch oder als Zeichenfolge erfolgen. Die Ausgabe muss exakt der Abbildung entsprechen, wobei führende und / oder nachfolgende Leerzeichen und Zeilenumbrüche zulässig sind.

Das ist , also kann das kürzeste Programm / die kürzeste Funktion für jede Sprache gewinnen!

Charlie
quelle
Das kommt aus dem Sandkasten .
Charlie
Sind führende Zeilenumbrüche erlaubt?
Shaggy
@Shaggy Ja, Sie können auch führende Leerzeichen und Zeilenumbrüche verwenden, sofern Sie das Kartenhaus genau wie abgebildet zeichnen. Es macht mir nichts aus, wenn es nicht auf der linken Seite des Bildschirms ausgerichtet ist.
Charlie
Können wir werfen und Fehler machen input=0?
Rod
@ Rod Wenn das leere Ausgabe produziert, ist es standardmäßig erlaubt
Luis Mendo

Antworten:

14

Python 2 , 97 95 94 92 Bytes

-2 Bytes dank Luka
Diese Version erzeugt eine Ausnahme n=0, ohne jedoch irgendetwas zu drucken

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)

Probieren Sie es online!

Fehlerfreie Version, Python 2, 94 Bytes

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)

Probieren Sie es online!

Stange
quelle
x=n>0 or-1=>x=n>0or-1
Zacharý
@ Zacharý funktioniert nicht, 0orwird als Oktanummer interpretiert
Rod
Schneiden Sie 2 weitere Bytes: m=abs(n). Dann, anstatt zu xsetzen n/m, statt zu x*nsetzenm
Luka
9

05AB1E , 30 29 24 Bytes

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

Probieren Sie es online!

Erläuterung

Ä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
Emigna
quelle
7

PHP , 125 Bytes

negative führende Newline eingeben

Geben Sie eine positive nachgestellte Newline ein

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

Probieren Sie es online!

PHP , 130 Bytes

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

Probieren Sie es online!

Jörg Hülsermann
quelle
5

MATL , 39 Bytes

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

Probieren Sie es online!

Erläuterung

|         % 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
Luis Mendo
quelle
1
Mann, das war schnell !! Ich hoffe, Version 2 wird nicht so einfach sein ... :-)
Charlie
4

C (gcc) , 169171 173 160 164 Bytes

#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 Bytes für Fehler in negativer Groß- / Kleinschreibung.

Probieren Sie es online!

Ungolfed (207 Bytes nach Entfernen aller Leerzeichen und Zeilenumbrüche):

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("--");;
  }
}
Keyu Gan
quelle
1
@officialaimm behoben! Danke
Keyu Gan
4

Kohle, 31 28 27 Bytes

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

Probieren Sie es online! Link ist eine ausführliche Version des Codes. Ich hatte ungefähr 4 verschiedene 32-Byte-Antworten und fand diese. Bearbeiten: 3 bis 4 Bytes durch Ausführen der absZeichenfolgenmanipulation gespeichert . Erläuterung:

   ⪪θ-                          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
Neil
quelle
Ich wusste, dass eine Holzkohle-Antwort mit enden würde ¿‹θ⁰‖T↓. :-)
Charlie
Wenn Charcoal von 05AB1E bei einer ASCII-Kunst-Herausforderung besiegt wird O_o
Gryphon - Reinstate Monica
@Gryphon Ich habe kein Ein-Byte abs...
Neil
Es ist zwar nur seltsam, das zu sehen. Sie wundern sich, woher die Welt kommt.
Gryphon - Wiedereinsetzung von Monica
Ja, das wären 23 Bytes mit einem eingebauten Bauchmuskel. (Glückwunsch zu 48K, übrigens)
ETHproductions
2

Japt , 40 38 Bytes

-2 Bytes dank @Shaggy

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

Probieren Sie es online!

Erläuterung

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
Justin Mariner
quelle
38 Bytes .
Shaggy
2

Gaia , 21 Bytes

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

Erläuterung

:                      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
Geschäfts-Katze
quelle
1

Mathematica, 140 Bytes

(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)&
J42161217
quelle
1

Retina , 116 111 105 Bytes

das ist viel zu lang geworden: /

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

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

Probieren Sie es online!

negativer Eingang wird als bezeichnet ~n

ovs
quelle