31
\$\begingroup\$

Your task is to write a program or a function that prints an ASCII triangle. They look like this:

|\
| \
|  \
----

Your program will take a single numeric input n, with the constraints 0 <= n <= 1000. The above triangle had a value of n=3.

The ASCII triangle will have n backslashes (\) and vertical bars (|), n+1 lines and dashes (-), and each line will have an amount of spaces equal to the line number (0-based, ie first line is line 0) besides the ultimate line.

Examples:

Input:

4

Output:

|\
| \
|  \
|   \
-----

Input:

0

Output:


In this test case, the output must be empty. No whitespace.

Input:

1

Output:

|\
--

Input & output must be exactly how I specified.

This is , so aim for the shortest code possible!

\$\endgroup\$
17
  • 4
    \$\begingroup\$ Does it need to be a program or can it be a function? \$\endgroup\$ – fəˈnɛtɪk Feb 8 '17 at 15:04
  • 8
    \$\begingroup\$ I think it would be better if case 0 can have any unexpected output since it is an edge case (especially since you requested that the number of dashes must be one more than the input number) \$\endgroup\$ – user41805 Feb 8 '17 at 15:10
  • 4
    \$\begingroup\$ @Okx There are frequently questions where the asker says program but really meant program or function. You might want to clarify that you are asking for a FULL program \$\endgroup\$ – fəˈnɛtɪk Feb 8 '17 at 16:04
  • 9
    \$\begingroup\$ I would definitely go for both program and function. That's the default rule if nothing else is specified. I would also remove the 0-edge case since it's a direct violation of "n+1 lines and dashes (-)". \$\endgroup\$ – Stewie Griffin Feb 8 '17 at 17:48
  • 3
    \$\begingroup\$ The challenge would be too simple without the size=0 exception. Part of the challenge is figuring out a way to account for this with the least amount of extra code. \$\endgroup\$ – 12Me21 Feb 8 '17 at 18:25

63 Answers 63

1 2
3
0
\$\begingroup\$

Haxe 4 (161 bytes)

class T{static function main()trace((x->x==0?"":[for(i in 0...x+1)i==x?[for(i in 0...x+2)""].join("-"):"|"+[for(i in -1...i)""].join(" ")+"\\"].join("\n"))(3));}

Try Online (slightly bigger there since playground requires class to be called "Test")

Test

Save as T.hx, compile and run interpreted mode haxe --run T

How it works

class T {
    static function main()
        trace(
            (x -> 
                x == 0 
                        ? "" 
                        : [for (i in 0...x + 1) 
                                i == x
                                        ? [for (i in 0...x + 2) ""].join("-") 
                                        : "|" + [for (i in -1...i) ""].join(" ") + "\\"
                ].join("\n")
            )(3)
        );
}
  • A class and entrypoint (static function called main) are required in Haxe.
  • (3) is the input
  • The snippet compiles to of all Haxe compiler targets (C++, JavaScript, PHP, Python, Lua, JVM, Java, etc)
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 15 bytes

F'|NúR'\J,}ƒ'-?

Try it online!

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

Zsh, 60 bytes

for ((;i<$1;))printf "|%$[i++]s\\\\\n"&&a=$a-
(($1))&&<<<$a-

Try it Online. Credit to the bash solution.

\$\endgroup\$
1 2
3

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.