I Saw That Coming

Write a program or function that takes in an integer greater than 1 or less than -1. That is, the input won't be 0, 1, or -1.

If the input is 2, the output should be:

|\_/|
|___|


If the input is 3, the output should be:

|\_/\_/|
|______|


If the input is 4, the output should be:

|\_/\_/\_/|
|_________|


The pattern continues in the same exact manner for larger inputs. For example, if the input is 10, the output should be:

|\_/\_/\_/\_/\_/\_/\_/\_/\_/|
|___________________________|


If the input is -2, the output should be:

 ____
|    |
|_/\_|


If the input is -3, the output should be:

 _______
|       |
|_/\_/\_|


If the input is -4, the output should be:

 __________
|          |
|_/\_/\_/\_|


The pattern continues in the same exact manner for smaller inputs. For example, if the input is -10, the output should be:

 ____________________________
|                            |
|_/\_/\_/\_/\_/\_/\_/\_/\_/\_|


The output can be printed or returned as a string with an optional trailing newline. The top right "empty" corner of the output for negative inputs may be a space or it may remain empty.

The shortest code in bytes wins.

• I saw what you did there. – Alex A. Oct 14 '15 at 3:32

Pyth, 45 bytes

jtW!J<Q0.b+.[YN+h*3t.aQJY.>[d.<"\_/"J\_)J" ||


Try it online: Demonstration or Test Suite

Explanation:

jtW!J<Q0.b+.[YN+h*3t.aQJY.>[d.<"\_/"J\_)J" ||  implicit: Q = input number
J<Q0                                       assign Q < 0 to J
[           )       create a list with
d                    * the string " "
.<"\_/"J            * the string "\_/" rotated to
the left by J
\_          * the string "_"
.>             J      rotate to the right by J
" ||  the string " ||"
.b                                     binary mapping, N iterates
over list, Y over string:
.[YN+h*3t.aQJ                         pad Y with N to reach a string
of length 3*(abs(Q)-1)+1-J
+             Y                        and append Y
tW!J                                           remove the first line if Q > 0
j                                               print each on separate line


CJam, 5650 49 bytes

ri_(z"\_/"*'_@0>{\4>W<_,@*SooNoS}|1$,*]${'|\'|N}/


Try it online in the CJam interpreter.

How it works

ri     e# Read an integer from STDIN and push it on the stack.
_(z    e# Push a copy, decrement it and apply absolute value.
e# For positive n, (n -> n-1) and (-n -> n+1).
"\_/"* e# Repeat the string that many times.
'_     e# Push an underscore.
@0>    e# Check if the original integer is positive.
{      e# If it isn't:
\    e#   Swap the generated string with the underscore.
4>W< e#   Discard the string's first 4 and last character.
e#   This makes the pattern of the bottom row start and end with an
e#   underscore, truncating it to the correct length in the process.
_,   e#   Push the length of a copy.
@*   e#   Repeat the underscore that many times.
So   e#   Print a space.
oNo  e#   Print the underscores, then a linefeed.
S    e#   Push a space.
}|     e#
1$, e# Retrieve the strings length. * e# Repeat the underscore or space that many times. ]$     e# Wrap the two generated strings in an array and sort it.
{      e# For each string:
'|\  e#   Push a vertical bar and swap the string on top of it.
'|N  e#   Push a vertical bar and a linefeed.
}/     e#


Pyth, 56 54 bytes

I'm golfing Pyth on a phone with the online interpreter. That's a totally great idea.

Update 2015-10-15: I rewrote the thing (still on my phone, lol) and saved 2 bytes, of which one could've been done with the original too.

J<Q0Ljb"||"jPW!J_WJ[y<>*K+J*3t.aQ"\_/"JKy*K?Jd\_+d*K\_


Try it online.

Minkolang 0.8, 100 bytes

"|"nd0u!vbd3*["_"]"|"25*"|"1g["\_/"]"|"(O).
"[d~g1"_"<.)O(" "D*3R~1"_"*52"|"D*3R1dg2"| "*52"|"]"\/_


Just builds up the stack and then prints it all out at once. I'm sure this could be golfed but I've already spent a lot of time on this...

JavaScript (ES6), 111 98 bytes

Optimal technique discovered! Turns out removing all those interpolators from the template strings saves a lot of bytes. Perhaps it could still be made shorter, perhaps not. In any case, ES6 template strings (and arrow functions) are awesome. :)

x=>(x>0?|\\_/A|
|___A|: ___A_
|   A |
|_/\\A_|).replace(/(...)A/g,(_,y)=>y.repeat(x>0?x-1:~x))


Python 2.7, 144 bytes

This took more bytes than expected. Here's the code.

c=int(input())
p,w,n,u=list('| \n_')
a=abs(c)-1
d=3*a
if c>0:
s=p+"\\_/"*a+p+n+p+u*d+p
else:
d=d+1
s=w+u*d+n+p+w*d+p+n+p+"_/\\"*a+u+p
print s


Java, 272 Bytes

String f(int i) {
String p = i>0?"\\_/":"_/\\_",x = "|"+new String(new char[(i<0?-i:i)-1]).replace("\0",p)+"|",
l=new String(new char[x.length()-2]).replace("\0","_");
return i>0?x+"\n|"+l+"|":" "+l+" \n|"+new String(new char[x.length()-2]).replace("\0"," ")+"|\n"+x;
}


SpecBAS - 167 bytes

1 INPUT n: DIM s$="\_/","_/\": LET t$=s$(2-(n>0))*(ABS n-1)+("_"*(n<0)),u$="_"*LEN t$2 TEXT IIF$(n>0,"|"+t$+"|"#13"|"+u$+"|"," "+u$+#13"|"+" "*LEN t$+"|"#13"|"+t$+"|")  IIF$ is an inline IF statement, #13 is a way of embedding newlines into a string (and doesn't always need a "+" if it's between hardcoded strings).

Since a few releases ago, SpecBAS lets you have multiple assignments to one LET statement, which helps save some characters.

Python 2.7, 118 bytes

n=input()*3-3
a=-n-6
s=' %s_\n| %s|\n|%s_|'%(a*'_',a*' ',a/3*'_/\\')
if n>0:s='|%s|\n|%s|'%(n/3*'\\_/',n*'_')
print s


Getting down from 120 to 118 was fun!

Ruby - 113 bytes

Seems too long. I'll try to golf this down a bit more.

n=gets.to_i;p,h=n.abs-1,?|;n>0 ? (puts h+'\\_/'*p+h,h+'___'*p+h):(k=p*3+1;puts' '+?_*k,h+' '*k+h,'|_'+'/\\_'*p+h)


C#, 185 bytes

C# struggles with golfing repeating strings.

Completely golfed:

string S(int n){int m=n>0?n:-n;return string.Format(n>0?"|{0}\n|{1}|":" {1}\n|{2}|\n|_{0}","|".PadLeft(m).Replace(" ",n>0?@"\_/":@"/\_"),"".PadLeft(m=m*3-(n>0?3:2),'_'),"".PadLeft(m));}


Indentation and new lines added for clarity:

string S(int n){
int m=n>0?n:-n;
return string.Format(n>0?"|{0}\n|{1}|":" {1}\n|{2}|\n|_{0}",
);
}


Powershell - 200190186168 154

Golfed out the equation (4-(($n-2)3)) to (3$n-6) along with some extraneous parens and semicolons.

Found that n is the equivalent of [Environment]::NewLine and that $s -f [args] is the equivalent of [String]::Format: $n=$args;if($n-gt0){$s="|{1}|{0}|{2}|";$a=$n;$b=$n*3}else{$n*=-1;$s=" {2}{0}|{3}|{0}|_/{1}\_|";$a=$n-2;$b=$c=3*$n-2};$s-f"n",("\_/"*$a),("_"*$b),(" "*$c)


Explanation retains clarifying parentheses:

$n=$args;

// Basically a way of coming up with a string to format and the
// necessary counts of repeated characters
if($n-gt0){ // Placeholder format$s="|{1}|{0}|{2}|{3}";
// Number of repeated "\_/" instances
$a=$n;
// Number of repeated "_" instances
$b=$n*3
} else {
$n*=-1;$s=" {2}{0}|{3}|{0}|_/{1}\_|";
$a=($n-2);
$b=(4+(($n-2)*3));
// Number of repeated " " instances .. not needed for "positive" saw
$c=$b;
};
[String]::Format($s,[Environment]::NewLine,"\_/"*$a,"_"*$b," "*$c)
`