30
5
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 code-golf, so aim for the shortest code possible!
related – Rod – 2017-02-08T14:53:48.457
4Does it need to be a program or can it be a function? – fəˈnɛtɪk – 2017-02-08T15:04:15.223
8I 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) – user41805 – 2017-02-08T15:10:06.103FYI: You can use
<pre><br></pre>
to have an empty code block (so you don't need an(empty)
– Nathan Merrill – 2017-02-08T15:31:56.580@TuukkaX as I said, output must be exactly as specified. – Okx – 2017-02-08T15:48:11.380
@LliwTelracs you can see in the top that I said a program. – Okx – 2017-02-08T15:49:37.137
4@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 – fəˈnɛtɪk – 2017-02-08T16:04:08.933
@LliwTelracs changed. Do you think I should allow functions? – Okx – 2017-02-08T16:17:59.710
@Okx I feel it is your choice but would prefer that there wasn't the additional restriction. – fəˈnɛtɪk – 2017-02-08T16:19:10.883
9I 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 (-)". – Stewie Griffin – 2017-02-08T17:48:13.173
3The 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. – 12Me21 – 2017-02-08T18:25:07.563
1I second @12Me12 - even if not intended, the edge case gives an... edge to the challenge. And changing challenges posted after answers are submitted is bad form. – Mindwin – 2017-02-08T19:04:29.917
@StewieGriffin a single dash wouldn't be a triangle, would it? :P – Okx – 2017-02-08T19:05:41.437
Is it acceptable to return the string? Or is printing mandatory? Quite a few answers here (including mine) are returning a string to save a few bytes. – adrianmp – 2017-02-09T07:43:52.507
1@adrianmp Since people have been doing both, I'd say you can return the string – Okx – 2017-02-09T10:43:38.800
1I assume that PETSCII substitutes for the ASCII characters are okay if it's for a C64/PET/VIC-20 etc...? – Shaun Bebbers – 2017-02-10T16:20:17.957
My original idea for suppressing output for
x = 0
was originallyx and x+1
but apparently that's not very efficient... – HyperNeutrino – 2017-03-05T02:46:14.637