House of cards (version 1)

25

0

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!

Charlie

Posted 2017-07-10T11:29:01.347

Reputation: 11 448

This comes from the sandbox.

– Charlie – 2017-07-10T11:30:47.713

Are leading newlines allowed? – Shaggy – 2017-07-10T11:35:10.950

@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. – Charlie – 2017-07-10T11:36:33.853

Can we thrown and error on input=0? – Rod – 2017-07-10T11:45:47.470

@Rod If that produces empty output it's allowed by default

– Luis Mendo – 2017-07-10T11:47:32.317

@Rod if it's allowed by default, I won't complaint then. – Charlie – 2017-07-10T11:50:27.347

@CarlosAlejo managed to work around it c: – Rod – 2017-07-10T11:53:19.853

Radiohead song... – Melkor – 2017-07-11T18:56:36.960

Answers

14

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!

Rod

Posted 2017-07-10T11:29:01.347

Reputation: 17 588

x=n>0 or-1 => x=n>0or-1 – Zacharý – 2017-07-10T17:38:22.867

@Zacharý doesn't work, 0or will be interpreted as an octa number – Rod – 2017-07-10T17:42:29.820

Cut 2 more bytes: m=abs(n). Then, instead of x put n/m, instead of x*n put m – Luka – 2017-07-10T20:44:16.830

9

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

Emigna

Posted 2017-07-10T11:29:01.347

Reputation: 50 798

7

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!

Jörg Hülsermann

Posted 2017-07-10T11:29:01.347

Reputation: 13 026

5

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

Luis Mendo

Posted 2017-07-10T11:29:01.347

Reputation: 87 464

1Man, that was fast!! I hope version 2 won't be so easy... :-) – Charlie – 2017-07-10T11:41:47.637

4

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

Neil

Posted 2017-07-10T11:29:01.347

Reputation: 95 035

I knew that a Charcoal answer would end with ¿‹θ⁰‖T↓. :-) – Charlie – 2017-07-10T12:12:49.563

When Charcoal is beaten by 05AB1E on an ASCII-art challenge O_o – Gryphon – 2017-07-10T13:16:20.227

@Gryphon I don't have a one-byte abs... – Neil – 2017-07-10T13:20:38.820

True, it's just weird to see this though. Makes you wonder what the world's coming to. – Gryphon – 2017-07-10T13:33:56.510

Yeah, this'd be 23 bytes with an abs builtin. (Congrats on 48K, btw) – ETHproductions – 2017-07-10T16:24:21.930

@ETHproductions I can't see how it would be less than 24; for (Abs(InputNumber)) { could only be 4 bytes shorter at best. – Neil – 2017-07-10T19:19:29.667

4

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("--");;
  }
}

Keyu Gan

Posted 2017-07-10T11:29:01.347

Reputation: 2 028

1@officialaimm fixed! thanks – Keyu Gan – 2017-07-10T15:55:48.083

2

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

Business Cat

Posted 2017-07-10T11:29:01.347

Reputation: 8 927

2

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

Justin Mariner

Posted 2017-07-10T11:29:01.347

Reputation: 4 746

38 bytes. – Shaggy – 2017-07-11T16:37:42.587

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

Posted 2017-07-10T11:29:01.347

Reputation: 15 931

1

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

ovs

Posted 2017-07-10T11:29:01.347

Reputation: 21 408

1

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!

Xcali

Posted 2017-07-10T11:29:01.347

Reputation: 7 671