Output an upside-down tent

27

1

Given an integer, output an upside-down tent.

The input determines both the size of the tent (absolute value) and whether the entrance is on the left side (negative numbers) or the right side (positive numbers).

If input = -1:
____
\/_/

If input = -2:
________
\  /   /
 \/___/

If input = -3:
____________
\    /     /
 \  /     /
  \/_____/

If input = 1:
____
\_\/

If input = 2:
________
\   \  /
 \___\/

If input = 3:
____________
\     \    /
 \     \  /
  \_____\/

et cetera

Note that the top of the tent (i.e. the last line) has 2 * abs(input) - 1 underscores.

There can be no leading spaces, such that the first line directly starts with an underscore.

Assume that the input will never be 0.

Your code should be as short as possible.

This challenge is based on a chat mini challenge by Helka Homba, which is allowed to be used in real challenges under the conditions of Calvin's Hobbies Public License.

user48538

Posted 2016-09-14T12:23:24.223

Reputation: 1 478

1Are trailing spaces OK? Meaning, can we output four strings of 12 length (a rectangle) for input 3 for example? – AdmBorkBork – 2016-09-14T12:50:09.710

1@TimmyD They are allowed. – user48538 – 2016-09-14T12:51:03.297

I first misread the title for Upside-Down-Ternet. Which might yield another interesting challenge...

– Tobias Kienzler – 2016-09-15T09:06:12.097

1@Nathaniel I'm curious why you haven't close-voted Turtles, given the same reasoning. – AdmBorkBork – 2016-09-15T12:32:00.900

2@TimmyD sure, that one's probably a dupe of something else too, I'm not really sure where the chain starts. I just think we've seen enough of these by now. – Nathaniel – 2016-09-15T13:08:18.923

5I fail to see how the questions are even remotely similar. Sure, they're both [tag:ascii-art] challenges that take an number and have you output the n-th iteration of something, but that's where the similarity ends. If that's enough to close as dupe, we shouldn't take any more [tag:ascii-art] challenges at all. – James – 2016-09-15T16:25:32.500

2

@Nathaniel Our accepted guideline for two challenges being duplicates is whether answers from one can be reused (competitively) on the other with little or no modification. Whether challenges bring something novel to the table is not part of that guideline. Please use downvotes for challenges you want to discourage or just ignore them if they don't think they're interesting and let those who do enjoy them.

– Martin Ender – 2016-09-15T16:41:22.080

1@MartinEnder I've asked a half-dozen times on meta why my questions were closed and was never once referred to an official guideline, despite explicitly asking for that. Instead I was told each time that I should just take my chances and suffer the consequences if five people happened not to like the idea. I am extremely in favour of having clear guidelines and enforcing close votes to meet them. If you're willing to delete my close votes on the basis of being against a guideline, would you please in future do the same when others VTC on my questions as well? Thanks. – Nathaniel – 2016-09-16T00:48:39.777

@MartinEnder by the way, I don't want to give the impression that I VTC'd this just to make these points. I did it because I'd been led to believe, based on my previous enquiries, that close-voting is the expected and encouraged action if you (a) don't much care for a challenge type, and (b) have a sort of general feeling that it's a bit duplicatey. I was trying to play along despite not feeling that's a fair way to do things, and I'm genuinely happy to hear that it's not encouraged after all. If this turns out not to be a double standard then I might be able to enjoy contributing after all. – Nathaniel – 2016-09-16T01:27:20.720

Answers

11

MATL, 55 53 52 51 bytes

|95cy4*Y"DXytPEt0*yvG0>?P_]!'\/ 'w)95JG|G0<yEq:++&(

Try it online!

Explanation

Let N denote the input. The code proceeds in three steps.

First, the first line of 4*N underscores is built as a string and is displayed (which removes it from the stack).

Second, the "frame" of the tent is built using the two types of slashes. To do this, a numerical 2D array is created which contains 1 and 2 corresponding to the two types of slashes, and 0 for space.

This is done concatenating four matrices:

  1. An identity matrix of size abs (N);
  2. A matrix of the same size containing 2 in the antidiagonal;
  3. A null matrix of the same size;
  4. A copy of matrix 2.

Concatenating these four matrices vertically gives, using N=3 as an example, the following 4*N × N matrix:

1 0 0
0 1 0
0 0 1
0 0 2
0 2 0
2 0 0
0 0 0
0 0 0
0 0 0
0 0 2
0 2 0
2 0 0

(which, transposed, begins to look like the tent).

We now take care of the sign of the input. If it is positive we simply transpose the above matrix and index into the string '\/ '. Indexing is 1-based and modular, so 1 becomes '\', 2 becomes '/' and 0 becomes ' ', producing the 2D char array

\    /     /
 \  /     / 
  \/     /  

On the other hand, if the input is negative we vertically flip and arithmetically negate the 4*N × N matrix, producing

-2  0  0
 0 -2  0
 0  0 -2
 0  0  0
 0  0  0
 0  0  0
-2  0  0
 0 -2  0
 0  0 -2
 0  0 -1
 0 -1  0
-1  0  0

Index -1 now refers to '/' and -2 to '\'. That is, the two types of slashes have been interchanged, as required. Again transposing and indexing into the string '\/ ' thus gives the reversed tent:

\     \    /
 \     \  / 
  \     \/  

Third, underscores need to be filled into part of the last row of the 2D char array. The horizontal position of this line depends on the sign of the input, and its lenght is abs(N).

Once the corresponding spaces have been replaced by underscores, the result is implicitly displayed, below the initial line of underscores that was already displayed in the first step.

Luis Mendo

Posted 2016-09-14T12:23:24.223

Reputation: 87 464

Shouldn't those underscores at the beginning be spaces? – James – 2016-09-14T16:31:46.130

@DJMcMayhem Sorry, what do you mean? – Luis Mendo – 2016-09-14T16:50:26.107

When I run your code the first line is all underscores. The output zyabin gave doesn't have that. – James – 2016-09-14T16:51:24.907

@DJMcMayhem I don't follow. The first line in the test cases is underscores, isn't it? And other answers (not yours) do that too? – Luis Mendo – 2016-09-14T16:54:57.223

Woah. That's so weird. When I look at the source HTML for the challenge I can see the underscores, but they are not rendering on the challenge itself.

– James – 2016-09-14T16:57:30.297

1@DJMcMayhem :-D Weird indeed. Try a different browser? – Luis Mendo – 2016-09-14T16:59:02.223

9

Javascript (ES6), 139 bytes

Builds the tent recursively:

f=(N,n=N>0?N:-N,i=0,r=(j,i)=>' _'[i||0].repeat(j),a=`\\${r(i)}/`,b=r(n*2+i-1,+!i))=>n--?f(N,n,i+2)+`
`+r(n)+(N<0?a+b+'/':'\\'+b+a):r(i*2,1)

Ungolfed and commented

f = (
  N,                                  // N is the original parameter (remains unchanged)
  n = N > 0 ? N : -N,                 // n is initialized to abs(N)
  i = 0,                              // i is the row counter (*2)
  r = (j, i) => ' _'[i||0].repeat(j), // helper function to repeat ' ' or '_' j times
  a = `\\${r(i)}/`,                   // a = '\ /' pattern
  b = r(n*2+i-1, +!i)                 // b = padding pattern filled with ' ' or '_'
) =>
  n-- ?                               // if we haven't made it yet to the top row:
    f(N, n, i+2) + `\n` +             //   - compute next row(s) / append line break
    r(n) +                            //   - append leading spaces
    (N < 0 ? a+b+'/' : '\\'+b+a)      //   - append a/b patterns according to N sign
  :                                   // else:
    r(i*2, 1)                         //   - return top row, made of '_' characters

Examples

var f=(N,n=N>0?N:-N,i=0,r=(j,i)=>' _'[i||0].repeat(j),a=`\\${r(i)}/`,b=r(n*2+i-1,+!i))=>n--?f(N,n,i+2)+`
`+r(n)+(N<0?a+b+'/':'\\'+b+a):r(i*2,1)

console.log(f(3));
console.log(f(-4));

Arnauld

Posted 2016-09-14T12:23:24.223

Reputation: 111 334

6

Python 2, 143 141 139 138 137 bytes

-2 bytes thanks to @Sp3000 (no need to parenthesise exec in Python 2)
-1 byte thanks to @Sp3000 (use cmp)

def f(n):d=cmp(n,0);a,b='\/'[::-d];s=n*d;x=2*s-1;y=4*s;print'_'*y;i=0;exec"print' '*i+(b+' '*(y-3-x-i-i)+a+'_ '[s-i>1]*x+a)[::d];i+=1;"*s

Test it at ideone

First we see if n is negative and make d +1 if it is and -1 if not.
Then we select the two slashes, a and b, using d such that a='\', b='/' when n is positive and a='/', b='\' when n is negative.
Next we set s=abs(n) which may be achieved by s=n*d.
Then we calculate the number of _ at the top (bottom of the picture), which is also the number of in the side of the tent as x=2*s-1.
Then we calculate the number of _ at the base of the tent (top of the picture), and store it as y=4*s since it will be used in the loop to create the rest of the tent.
Now we print the base of the tent using print'_'*y.
Then we print the rest of the tent by executing s print statements with a looping variable i initialised to 0 which increments by 1 for each print statement.
The rest of the tent then has y-3-x-i-i spaces in the door and x spaces in the body until the top is reached, when s-i>1 evaluates to False, picking the _ from '_ '.
For a positive, left-door, tent the whole of the tent, excluding the leading spaces is back-to-front, so it is reversed while the positive, 'right-door', tent is not with [::d].

Jonathan Allan

Posted 2016-09-14T12:23:24.223

Reputation: 67 804

@Sp3000 unfortunately cmp(0,0) returns 0 – Jonathan Allan – 2016-09-14T15:44:08.680

5

Python 2, 121 bytes

def f(n):i=k=abs(n);print'_'*k*4;exec"print' '*(k-i)+r'\\\%%s%\*%c%%*sc/'[n<0::2]%(' _'[i<2]*(2*k-1))%(2*i-1,47);i-=1;"*k

Just a lot of string formatting.

Sp3000

Posted 2016-09-14T12:23:24.223

Reputation: 58 729

5

C#, 215 214 bytes

string t(int N){var n=N<0;N=n?-N:N;var t=new string('_',4*N);for(int i=0;i<N;){string f=new string(i<N-1?' ':'_',2*N-1),p=new string(' ',2*N-2*i-2);t+='\n'+new string(' ',i++)+'\\'+(n?p+'/'+f:f+'\\'+p)+'/';}return t;}

There's a possibility to save a few bytes when using using s=string; beforehand.

s t(int N){var n=N<0;N=n?-N:N;var t=new s('_',4*N);for(int i=0;i<N;){s f=new s(i<N-1?' ':'_',2*N-1),p=new s(' ',2*N-2*i-2);t+='\n'+new s(' ',i++)+'\\'+(n?p+'/'+f:f+'\\'+p)+'/';}return t;}

which would be 15 (using) + 184 (method) = 199 bytes.

BackFromExile

Posted 2016-09-14T12:23:24.223

Reputation: 51

5Welcome to PPCG, BackFromExile! – Erik the Outgolfer – 2016-09-15T15:14:57.783

Indeed, welcome to PPCG! A very nice first answer +1. I tried to find anything to golf (quite a while since I programmed in C#), and in the end was only able to find one thing for -1 byte: If you change the first var inside the for-loop to string, you can remove the second var (including space to save the byte). So var f becomes string f, and ;var p= becomes ,p=. – Kevin Cruijssen – 2016-09-16T11:38:28.597

4

PowerShell v2+, 217 205 190 187 184 bytes

param($b)"_"*(($a=[math]::Abs($b))*4);$z,$y='/\'[($b=$b-lt0),!$b]
((($x=1..$a|%{($w=" "*($_-1))+$z+" "*(2*($a-$_))+$y+(' ','_')[$_-eq$a]*($a*2-1)+$y+$w})|%{-join$_[($a*4)..0]}),$x)[$b]

Takes input $b as an integer. Note that if $b is negative, you need to explicitly surround it with parens to cast it appropriately (see examples), else PowerShell will think it's a string.

Regardless of which direction the tent is facing, the first line is the same, a bunch of underscores; exactly 4*abs(input) many of them, actually. That number is also stored into $a for use later. Additionally, now that we have the absolute value of $b stored into $a, we turn $b into a Boolean for its sign, and choose our slashes stored into $y and $z.

The next line is constructing and formulating the output, and it's a doozy, so let's break it down.

We're essentially indexing into an array of two elements, (big long calculations saved into $x) or $x, based on $b.

The calculations are where the tent body is constructed. We loop from 1..$a|%{...}. Each iteration we're constructing a line of the tent body. We start with a number of spaces equal to the line # we're on -1, so that it's appropriately left-aligned. That's stored into $w for later, and concatenated with the appropriate slash ($z, based on $b), then the doorframe number of spaces, then the other slash $y, then either underscores or spaces depending upon if we're on the bottom line or not, then another slash $y, and finally the appropriate number of trailing spaces ($w) to construct a rectangular string. That resulting array of strings is stored into $x.

If the left half of the array is selected (i.e., $b is False since the input was positive), then we need to loop through $x and reverse each line item -- this is where the trailing spaces come into play; it allows us to simply reverse the lines rather than re-calculate distances.

If $b is True, then the right half of the array $x is selected instead.

In either case, the pipeline now contains an array of strings. Implicit output via Write-Output happens at program completion, with default newline between elements.

Examples

PS C:\Tools\Scripts\golfing> .\print-upside-down-tent.ps1 (-5)
____________________
\        /         /
 \      /         / 
  \    /         /  
   \  /         /   
    \/_________/    

PS C:\Tools\Scripts\golfing> .\print-upside-down-tent.ps1 (4)
________________
\       \      /
 \       \    / 
  \       \  /  
   \_______\/   

AdmBorkBork

Posted 2016-09-14T12:23:24.223

Reputation: 41 581

4

TSQL, 195 bytes

Golfed:

DECLARE @ INT=-2
DECLARE @b INT=ABS(@),@i INT=0PRINT REPLICATE('_',4*@b)z:SET @i+=1PRINT SPACE(@i-1)+'\'+STUFF(REPLICATE(IIF(@i<@b,' ','_'),4*@b-2*@i),@b*2-IIF(@<0,@i*2-1,0),1,IIF(@<0,'/','\'))+'/'IF @i<@b GOTO z

Ungolfed:

DECLARE @ INT=-2

DECLARE @b INT=ABS(@),@i INT=0

PRINT REPLICATE('_',4*@b)
z:
  SET @i+=1
  PRINT 
    SPACE(@i-1)+'\'
    +STUFF(REPLICATE(IIF(@i<@b,' ','_'),
      4*@b-2*@i),@b*2-IIF(@<0,@i*2-1,0),1,IIF(@<0,'/','\'))
    +'/'
IF @i<@b GOTO z

Fiddle

t-clausen.dk

Posted 2016-09-14T12:23:24.223

Reputation: 2 874

4

V, 66 bytes

é /ä
"aDoÀñá_>ñ^hr\A\/ò^hÄX$2é_Ó_/ òÄÒ_ñ/-
ddÍܨ[ _]*©Ü¨ *©/ܲ¯±

Try it online!

This is a pretty naive approach, so I'll try to golf it down further later today. This solution contains unprintable characters, so here is a hexdump:

0000000: e920 2fe4 0a22 6144 6f1b c0f1 e15f 3ef1  . /.."aDo...._>.
0000010: 5e68 725c 415c 2f1b f25e 68c4 5824 32e9  ^hr\A\/..^h.X$2.
0000020: 5fd3 5f2f 20f2 c4d2 5ff1 2f2d 0a64 64cd  _._/ ..._./-.dd.
0000030: dca8 5b20 5f5d 2aa9 dca8 202a a92f dcb2  ..[ _]*... *./..
0000040: afb1                                     ..

James

Posted 2016-09-14T12:23:24.223

Reputation: 54 537

4

05AB1E, 52 bytes

Ä©'_4®*×,FNð×'\®·<N>®Qi'_ëð}×®N>-·ð×®¹Qi'\ës'/}s'/J,

Explanation

                                                     # implicit input, call this A
Ä©                                                   # store abs(A) in register for later use
  '_4®*×,                                            # print 4*A underscores (tent floor)
         F                                           # for each non-floor section in range(N)
          Nð×'\                                      # push N spaces at the beginning of the 
                                                     # row followed by a backslash
                  N>®Qi'_ëð}                         # if we're on the last row push an
                                                     # underscore, else a space
               ®·<          ×                        # repeat that char abs(A)*2-1 times
                             ®N>-·ð×                 # push 2*(abs(A)-(N+1)) spaces
                                    ®¹Qi'\ës'/}      # if input is positive push backslash
                                                     # else push a slash
                                               s'/   # move that char between the 2 sections
                                                     # of spaces
                                                  J, # join the row and print

Try it online!

Emigna

Posted 2016-09-14T12:23:24.223

Reputation: 50 798

3

Haskell, 187 184 183 bytes

f x=unlines$[(n*4)%'_']++((' '#)<$>[0..n-2])++['_'#(n-1)]where m#i=i%' '++'\\':m!i++"/";m!i|x>0=(2*n-1)%m++'\\':(2*(n-i-1))%' '|q<-2*(n-i-1)=q%' '++'/':(2*n-1)%m;n=abs x;m%c=c<$[1..m]

I feel like this is not a great score for Haskell, so any ideas for improvement are welcome.

Ungolfed

tent :: Int -> String
tent x = unlines $ [replicate (n*4) '_'] ++ (row ' '<$>[0..n-2]) ++ [row '_' (n-1)]
    where row m i = replicate i ' ' ++ "\\" ++ dir m i ++ "/"
          -- direction selector
          dir m i | x > 0 = side m ++ "\\" ++ entrance i ' '
                  | 1 > 0 = entrance i ' ' ++ "/" ++ side m
          side = replicate (2*n-1)
          entrance i = replicate (2*(n-i-1))
          n = abs x

sudee

Posted 2016-09-14T12:23:24.223

Reputation: 551

Better than my 290 bytes I was about to post... – Myridium – 2016-09-14T21:25:57.687

Should you not add a main so that it accepts stdin as the input? – Myridium – 2016-09-14T21:27:58.057

You can post a single function unless the question specifies otherwise. There's a meta thread for the general rules on answering, I'll try to find it for you. – sudee – 2016-09-14T21:33:40.523

I was just looking for such threads, and looking through the Documentation Center but couldn't find the rules for this SE. – Myridium – 2016-09-14T21:34:13.663

Here are some useful ones: Full program / function; Forbidden loopholes; Default I/O

– sudee – 2016-09-14T21:48:16.357

1You can save 2 bytes by changing where you prepend a single character to use the : character. i.e. change "\\" ++ entrance... to '\\':entrance. – Myridium – 2016-09-14T21:58:21.010

1Don't waste the "otherwise" guard: you can change |1>0=(2*(n-i-1))%' ' to |q<-2*(n-i-1)=q%' '. – nimi – 2016-09-14T22:38:42.057

You still have ' '++'\\' and ' '++'/' that you can change to ' ':'\\' and ' ':'/' respectively. You can prepend as many elements as you like in Haskell to the start of a list, i.e. a:b:c:[d] == [a,b,c,d]. – Myridium – 2016-09-15T05:00:45.263

Actually, " /"++ is shorter than ' ':'/':, and likewise " \\"++ is shorter than ' ':'\\':. To avoid confusion, here's my full suggestion:

f x=unlines$[(n*4)%'_']++((' '#)<$>[0..n-2])++['_'#(n-1)]where m#i=i%" \\"++m!i++"/";m!i|x>0=(2*n-1)%m++'\\':(2*(n-i-1))%' '|q<-2*(n-i-1)=q%" /"++(2*n-1)%m;n=abs x;m%c=c<$[1..m] – Myridium – 2016-09-15T05:03:45.737

I can't really use this trick, because the left side characters are actually part of a function application. Note the % operator, shortcut of replicate, defined at the end of the golfed code. – sudee – 2016-09-15T05:14:43.803

2

C, 240 207 193 Bytes

#define P putchar
a,j,l,m;g(x,y,z){for(m=y+z+1;m--;P(m^z?l?32:95:x));}f(n){g(32,(a=abs(n))*4,0);for(P(10),j=2*(l=a)-1;l--;){for(m=a;--m>l;P(32));P(92);m=n>0?g(92,j,l*2):g(47,l*2,j);puts("/");}}

This time I optimized the function g(...) to use a single for loop.

#define P putchar
a,j,l,m;g(x,y,z){for(;y--;P(l?32:95));for(P(x);z--;P(l?32:95));}f(n){g(32,(a=abs(n))*4,0);l=a;j=2*a-1;P(10);for(;l--;){for(m=a;--m>l;P(32));P(92);m=n>0?g(92,j,l*2):g(47,l*2,j);puts("/");}}

This time macro X is better off as a function g(...) and since y and z are parameters in a new scope I can just decrement them in the scope of g.

#define P putchar
#define X(x,y,z){for(k=0;k++<y;P(l?32:95));P(x);for(k=0;k++<z;P(l?32:95));}
a,i,j,k,l,m;f(n){for(l=a=abs(n);i++<a*4;P(95));j=2*a-1;P(10);while(l--){for(m=a;--m>l;P(32));P(92);if(n>0)X(92,j,l*2)else X(47,l*2,j)puts("/");}}

Test with this main function; This should golf down much smaller.

main(c,v)char**v;
{
    f(atoi(v[1]));
}

cleblanc

Posted 2016-09-14T12:23:24.223

Reputation: 3 360

2

C# 241 231 Bytes

Saved 10 bytes thanks to @Kevin Cruijssen

using s=System.String;s f(int N){var f=N<0;N=N>0?N:-N;var o=new s('_',N*4);for(int j=0,z;j<N;){z=-2*j+2*N-2;var O=j>N-2?'_':' ';o+='\n'+new s(' ',j)+'\\'+new s(' ',z)+(f?'/':O)+new s(O,j++*2)+(f?O:'\\')+new s(' ',z)+'/';}return o;}

Old version:

string f(int N){var f=N<0;N=N>0?N:-N;var o=new string('_',N*4);for(int j=0;j<N;){int z=-2*j+2*N-2;var O=j>N-2?'_':' ';o+='\n'+new string(' ',j)+'\\'+new string(' ',z)+(f?'/':O)+new string(O,j++*2)+(f?O:'\\')+new string(' ',z)+'/';}return o;}

Originally had the new string(...) as a Func<char,int,string> but saved one byte using the constructor. I wish int->char was implicit

Pretty sure my math can be fixed somehow too, but I can't see it

pinkfloydx33

Posted 2016-09-14T12:23:24.223

Reputation: 308

1You can golf it some more. First of all you can remove the int before z= by adding it to the for-loop: int j=0,z. And since you use string quite a lot, you can alias it with using s=System.String; So the total becomes: using s=System.String;s f(int N){var f=N<0;N=N>0?N:-N;var o=new s('_',N*4);for(int j=0,z;j<N;){z=-2*j+2*N-2;var O=j>N-2?'_':' ';o+='\n'+new s(' ',j)+'\\'+new s(' ',z)+(f?'/':O)+new s(O,j++*2)+(f?O:'\\')+new s(' ',z)+'/';}return o;} (231 bytes) – Kevin Cruijssen – 2016-09-16T11:52:11.853

1

Canvas, 25 bytes

⤢\║l╵:╷:«_×╋l/+L_×;∔⁸0>?↔

Try it here!

dzaima

Posted 2016-09-14T12:23:24.223

Reputation: 19 048

1

Swift 2.2 421 bytes

Well... This was an attempt.

Golfed:

let t={(s:String,n:Int)->String in return String(count:n,repeatedValue:Character(s))};let e={(n:Int)in var w=[String]();w.append(t("_",abs(n)*4));let c = abs(n);let d = n>0 ? "/": "\\";let f = n>0 ? "\\": "/";for var i in 0...abs(n)-1 {w.append(t(" ",i)+d+t(" ",c*2-2-(2*i))+f+(i<c-1 ?t(" ",2*c-1)+f:t("_",2*c-1)+f)+(n>0 ?t(" ",i):""));};w=n<0 ?w:w.map(){String($0.characters.reverse())};print(w.joinWithSeparator("\n"))}

UnGolfed:

let t={(s:String,n:Int) -> String in
    return String(count:n,repeatedValue:Character(s))
};
let e={(n:Int) in
    var w=[String]();
    w.append(t("_",abs(n)*4));
    let c = abs(n);
    let d = n>0 ? "/": "\\";
    let f = n>0 ? "\\": "/";
    for var i in 0...abs(n)-1 {
        w.append(t(" ",i)+d+t(" ",c*2-2-(2*i))+f+(i<c-1 ?t(" ",2*c-1)+f:t("_",2*c-1)+f)+(n>0 ?t(" ",i):""));
    };
    w=n<0 ?w:w.map(){String($0.characters.reverse())};
    print(w.joinWithSeparator("\n"))
}

Danwakeem

Posted 2016-09-14T12:23:24.223

Reputation: 141

1

PHP, 143 bytes

$t=str_repeat;echo$t(_,4*$s=$k=abs($n=$argv[1]));for(;$k--;$p.=" "){$f=$t("  ",$k);$r=$t($k?" ":_,2*$s-1);echo"
$p\\",$n<0?"$f/$r/":"$r\\$f/";}

run with php -r '<code>' <parameter>

breakdown

$t=str_repeat;  // function name to variable saves 10-1 bytes
echo$t(_,4*$s=$k=abs($n=$argv[1])); // print bottom
for(
    ;
    $k--;   // $k from abs($n-1) to 0
    $p.=" "                 // create padding
)
{
    $f=$t("  ",$k);         // create front
    $r=$t($k?" ":_,2*$s-1); // create side/roof
    echo"\n$p\\",$n<0
        ?"$f/$r/"   // print, entrance left
        :"$r\\$f/"  // print, entrance right
    ;
}

Titus

Posted 2016-09-14T12:23:24.223

Reputation: 13 814

1

Batch, 289 bytes

@echo off
set/pn=
set u=
for /l %%i in (2,1,%n:-=%)do call set u=_%%u%%_
echo _%u%__%u%_
set l=
set m=%u%/_%u%
if %n% gtr 0 set m=%u%_\%u%
set m=%m:_= %
for /l %%i in (2,1,%n:-=%)do call:l
set m=%m: =_%
:l
echo %l%\%m%/
set l= %l%
if %n% gtr 0 set m=  %m:~0,-2%
set m=%m:~2%

Takes input on STDIN. Starts by creating a string of 2*(abs(n)-1) underscores. This is then repeated plus an additional 4 underscores for the base of the tent. The rest of the tent then consists of an indent (which increases by 1 on each line), a \, the middle of the tent, and a /. The middle of the tent starts as 2*(abs(n)-1) spaces, plus either \ or / plus a space (which I can't represent in Markdown), plus another 2*(abs(n)-1) spaces. I reuse the underscore string and change them to spaces for convenience, but then change the spaces back to underscores for the last line. Each line removes two spaces from one side of the middle of the tent, although it's slightly golfier to move the two spaces to the start of the string first if necessary.

Neil

Posted 2016-09-14T12:23:24.223

Reputation: 95 035