25
\$\begingroup\$

Version 2 here.

Simple challenge: given an integer, draw a house of cards with the given number of stories. If the number is negative, draw the house upside-down. Examples:

Input: 2
Output:

 /\
 --
/\/\

Input: 5
Output:

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

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

Input: -3
Output:

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

Input can be numeric or a string. Output must be exactly as shown, with leading and/or trailing spaces and newlines allowed.

This is , so may the shortest program/function for each language win!

\$\endgroup\$
8
  • \$\begingroup\$ This comes from the sandbox. \$\endgroup\$
    – Charlie
    Jul 10, 2017 at 11:30
  • \$\begingroup\$ Are leading newlines allowed? \$\endgroup\$
    – Shaggy
    Jul 10, 2017 at 11:35
  • \$\begingroup\$ @Shaggy yes, you can also have leading whitespaces and newlines, as long as you draw the house of cards exactly as shown. I don't mind if it is not aligned to the left of the screen. \$\endgroup\$
    – Charlie
    Jul 10, 2017 at 11:36
  • \$\begingroup\$ Can we thrown and error on input=0? \$\endgroup\$
    – Rod
    Jul 10, 2017 at 11:45
  • \$\begingroup\$ @Rod If that produces empty output it's allowed by default \$\endgroup\$
    – Luis Mendo
    Jul 10, 2017 at 11:47

11 Answers 11

14
\$\begingroup\$

Python 2, 97 95 94 92 bytes

-2 bytes thanks to Luka
This version produces an exception on n=0,but without printing anything

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)

Try it online!

Non-error 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)

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ x=n>0 or-1 => x=n>0or-1 \$\endgroup\$
    – Adalynn
    Jul 10, 2017 at 17:38
  • \$\begingroup\$ @Zacharý doesn't work, 0or will be interpreted as an octa number \$\endgroup\$
    – Rod
    Jul 10, 2017 at 17:42
  • \$\begingroup\$ Cut 2 more bytes: m=abs(n). Then, instead of x put n/m, instead of x*n put m \$\endgroup\$
    – Luka
    Jul 10, 2017 at 20:44
9
\$\begingroup\$

05AB1E, 30 29 24 bytes

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

Try it online!

Explanation

Ä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
\$\endgroup\$
7
\$\begingroup\$

PHP, 125 bytes

input negative leading newline

input positive trailing 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);

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

Try it online!

\$\endgroup\$
0
5
\$\begingroup\$

MATL, 39 bytes

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

Try it online!

Explanation

|         % 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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Man, that was fast!! I hope version 2 won't be so easy... :-) \$\endgroup\$
    – Charlie
    Jul 10, 2017 at 11:41
4
\$\begingroup\$

C (gcc), 169 171 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 for negative case bug.

Try it online!

Ungolfed (207 bytes after removing all spaces and newline):

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("--");;
  }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ @officialaimm fixed! thanks \$\endgroup\$
    – Keyu Gan
    Jul 10, 2017 at 15:55
  • \$\begingroup\$ Suggest L"尯⽜"+x instead of x?"\\/":"/\\" \$\endgroup\$
    – ceilingcat
    Jul 10, 2020 at 16:54
4
\$\begingroup\$

Charcoal, 31 28 27 bytes

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

Try it online! Link is to verbose version of code. I had about 4 different 32 byte answers then found this. Edit: Saved 3 4 bytes by performing the abs using string manipulation. Explanation:

   ⪪θ-                          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
\$\endgroup\$
6
  • \$\begingroup\$ I knew that a Charcoal answer would end with ¿‹θ⁰‖T↓. :-) \$\endgroup\$
    – Charlie
    Jul 10, 2017 at 12:12
  • \$\begingroup\$ When Charcoal is beaten by 05AB1E on an ASCII-art challenge O_o \$\endgroup\$
    – Gryphon
    Jul 10, 2017 at 13:16
  • \$\begingroup\$ @Gryphon I don't have a one-byte abs... \$\endgroup\$
    – Neil
    Jul 10, 2017 at 13:20
  • \$\begingroup\$ True, it's just weird to see this though. Makes you wonder what the world's coming to. \$\endgroup\$
    – Gryphon
    Jul 10, 2017 at 13:33
  • \$\begingroup\$ Yeah, this'd be 23 bytes with an abs builtin. (Congrats on 48K, btw) \$\endgroup\$ Jul 10, 2017 at 16:24
2
\$\begingroup\$

Japt, 40 38 bytes

-2 bytes thanks to @Shaggy

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

Try it online!

Explanation

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
\$\endgroup\$
1
  • \$\begingroup\$ 38 bytes. \$\endgroup\$
    – Shaggy
    Jul 11, 2017 at 16:37
2
\$\begingroup\$

Gaia, 21 bytes

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

Explanation

:                      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
\$\endgroup\$
1
\$\begingroup\$

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)&
\$\endgroup\$
1
\$\begingroup\$

Retina, 116 111 105 bytes

this got way too long :/

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

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

Try it online!

negative input is denoted as ~n

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 100 + 1 (-n) = 101 bytes

$/=$_>0?'/\\':'\\/';push@r,$_=$"x--$q.$/x$_,y|/\\|-|r for 1..($q=abs);pop@r;say for$_<0?reverse@r:@r

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.