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.

  • 12
    \$\begingroup\$ I saw what you did there. \$\endgroup\$
    – Alex A.
    Oct 14, 2015 at 3:32

13 Answers 13


CJam, 56 50 49 bytes


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.


Try it online.


Minkolang 0.8, 100 bytes

"[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...


Pyth, 45 bytes

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

Try it online: Demonstration or Test Suite


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

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. :)

|___A|`:` ___A_
|   A |

Vyxal, 67 bytes


Try it Online!



Python 2.7, 144 bytes

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

p,w,n,u=list('| \n_')
if c>0:
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

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}",
        "|".PadLeft(m).Replace(" ",n>0?@"\_/":@"/\_"),

Powershell - 200 190 186 168 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:


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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