Information
Create a diagonal line given its length (let’s call the variable, say, \$n\$ where \$n > 0\$)
- The diagonal line starts from the top left and goes to the bottom right.
- You must use the ASCII character
\for the line. - The input number can be given from STDIN or if your program doesn’t support that, use a hardcoded value in your code or pass it as an argument while running the program.
- For padding, only use the space character
0x20 - Trailing spaces and/or newlines are allowed.
- The length of the line is the non whitespace characters
Here is an example program (59 bytes):
n = int(input())
for i in range(n):
print(' ' * i + '\\')
Example:
Input: 4
Output
\
\
\
\
Rules
- Use standard I/O when possible.
- Standard loopholes are forbidden.
- This is code golf, so the shortest answer in bytes wins.
- Please explain the code that you wrote.
-
\$\begingroup\$ This was on Sandbox for a couple of days :) \$\endgroup\$zoomlogo– zoomlogo2021年06月04日 14:32:16 +00:00Commented Jun 4, 2021 at 14:32
-
\$\begingroup\$ Does the explanation count towards the byte count :? \$\endgroup\$Wezl– Wezl2021年06月04日 14:34:58 +00:00Commented Jun 4, 2021 at 14:34
-
\$\begingroup\$ The explanation is only for how the program works \$\endgroup\$zoomlogo– zoomlogo2021年06月04日 14:37:12 +00:00Commented Jun 4, 2021 at 14:37
-
5\$\begingroup\$ I suggest removing the rule "Use Standard I/O when possible". We have well-accepted default rules for I/O, and it would be unfair to force the use of standard I/O just because it is possible for a given language \$\endgroup\$Luis Mendo– Luis Mendo2021年06月04日 17:00:24 +00:00Commented Jun 4, 2021 at 17:00
-
9\$\begingroup\$ I'd suggest not accepting an answer (or at least not accepting one so quickly) in the future, as it may discourage others from answering. \$\endgroup\$user– user2021年06月04日 17:10:59 +00:00Commented Jun 4, 2021 at 17:10
54 Answers 54
Canvas, 1 byte
Better tool for the job ;)
\
Given an integer, this draws a diagonal of that string. If you pass a string instead, this prints the string along the diagonal. There is matching anti-diagonal builtin / as well.
-
9\$\begingroup\$ lol, wow. definitely the perfect tool to pick :p \$\endgroup\$2021年06月04日 15:15:17 +00:00Commented Jun 4, 2021 at 15:15
-
\$\begingroup\$ 13 hours too late for this :p \$\endgroup\$Razetime– Razetime2021年06月05日 04:31:22 +00:00Commented Jun 5, 2021 at 4:31
-
\$\begingroup\$ @Razetime 5 minutes too late to comment this... \$\endgroup\$hakr14– hakr142021年06月05日 04:37:46 +00:00Commented Jun 5, 2021 at 4:37
-
4\$\begingroup\$ Also 2 bytes \$\endgroup\$2021年06月04日 14:56:02 +00:00Commented Jun 4, 2021 at 14:56
APL (Dyalog Unicode), 9 bytes (SBCS)
Anonymous tacit prefix function. Returns a list of string, as per meta consensus.
'\'↑⍨ ̈-∘⍳
∘⍳ indices one through \$n\$, then:
- negate those
̈ for each:
↑⍨ take (when negative: from the rear) that many characters (padding with spaces) from:
'\' the backslash character
Jelly, 7 bytes
=þị)\ Y
Ḷ6ẋp"\Y
How they work
=þị)\ Y - Main link. Takes N on the left
=þ - Yield the identity matrix of size N
ị)\ - Index into "\ ", replacing 1 with "\" and 0 with " "
Y - Join by newlines
Ḷ6ẋp"\Y - Main link. Takes N on the left
Ḷ - Range [0, ..., N-1]
6ẋ - Repeat that many spaces for each
p"\ - Append "\" to each
Y - Join by newlines
Japt -R, 5 bytes
õ!ù'\
õ!ù'\ :Implicit input of integer
õ :Range [1,input]
!ù'\ :For each, left pad "\" to that length with spaces
:Implicit output joined with newlines
Japt -mR, 5 bytes
'\iUç
'\iUç :Implicit map of each U in the range [0,input)
'\i :Prepend to "\"
Uç : Space repeated U times
:Implicit output joined with newlines
51AC8, 10 bytes
R[\ ×ばつ\\+t]
-2 bytes due to an update.
-7 bytes due to an update introducing for_each loops and range.
-1 byte online interpreter and implicit input.
Explanation
# Implicit Input (STDIN) and push to stack
R # Range from 0 to input (exclusive)
[ # Start for each
\ # Push ' ' to the stack
×ばつ # Multiply the top 2 elements on the stack
\\ # Push '\'
+ # Add top 2 elements
t # Pop and print top of stack
] # End of while loop
C (clang), 37 bytes
f(n){printf("%*c\n",n--,92,n&&f(n));}
C (gcc), 39 bytes
A recursive version suggested by @att.
f(n){--n&&f(n);printf("%*c\n",n+1,92);}
C (gcc), 44 bytes
i;f(n){for(i=0;i++<n;)printf("%*c\n",i,92);}
-
-
\$\begingroup\$ @att Nice. That leads to a 37-byte version with clang. Undefined behavior for the win! ;-) \$\endgroup\$Arnauld– Arnauld2021年06月05日 10:31:14 +00:00Commented Jun 5, 2021 at 10:31
Dyalog APL, (削除) 16 (削除ここまで) 14 bytes
Solution - Takes in number of lines as input from user, and returns a string of diagonal line.
' \'[1+∘.=⍨⍳⎕]
Explanation
⎕ ⍝ ⎕ takes input from the user (number of lines)
⍝ In the below explanation, I have assumed ⎕ = 9 as input
⍳⎕ ⍝ 1 to 9 : [1,2,3,4,5,6,7,8,9]
∘.= ⍝ Outer Product with Equality
⍨ ⍝ Apply a function with same argument on both sides
∘.=⍨⍳⎕ ⍝ ∘.=⍨ function applied to array ⍳⎕ = [1,2..9]
⍝ This is same as (⍳9) ∘.= (⍳9), which produces Identity Matrix of size 9
1+∘.=⍨⍳⎕ ⍝ Add 1 to each element of previous matrix (since APL uses 1-based index)
' \'[1+∘.=⍨⍳⎕] ⍝ From the string ' \', select characters specified by indices in 1+∘.=⍨⍳⎕
-
\$\begingroup\$
⎕actually reads from the input section on TIO, but the way the program is run it doesn't implicitly output, you would need to prepend⎕←to get output. The way to "enable" implicit output is to wrap the program into a tradfn and call that: Try it online! \$\endgroup\$ovs– ovs2021年09月30日 14:08:13 +00:00Commented Sep 30, 2021 at 14:08 -
\$\begingroup\$ @ovs How exactly is a tradfn different from a normal function definition? And how do you "enable" implicit I/O in tryapl.org (or, if this is not possible from the site, from Dyalog APL Windows Editor) ? \$\endgroup\$Sohang Chopra– Sohang Chopra2021年10月01日 17:55:25 +00:00Commented Oct 1, 2021 at 17:55
-
\$\begingroup\$ Oh sorry I misread your post, tryapl.org indeed does not support
⎕. If you want to add a link for others to try this online, you could use the TIO one I sent above. I don't have Windows but I would assume that you can run' \'[1+∘.=⍨⍳⎕]and then enter the number (like this). And tradfn is short for traditional function \$\endgroup\$ovs– ovs2021年10月01日 21:14:24 +00:00Commented Oct 1, 2021 at 21:14
brainfuck, 113 bytes
,>>+++++++[<+++++++++++++>-]<+>++++++++++>>++++[<++++++++>-]<<<<[>>>>[->+>+<<]>[-<+>]>[<<<.>>>-]<<<<<.>.>>+<<<<-]
First time poster. This was a fun exercise! Please provide criticism, I pretty much went in cold when writing this.
, how long the line should be via char code
>
initialize cell 1 with "\"
>+++++++[<+++++++++++++>-]<+
>
initialize cell 2 with "\n"
++++++++++
>
initialize cell 3 with " "; go back to beginning
>++++[<+++++>-]<
<<<
start loop at cell 0
[
>>>> go to cell 4
[
->+>+<< copy the pad value to cells 5 and 6
]
> now we move cell 5 to cell 4
[
-<+> cell 4 keeps track how much padding we'll need for our next iteration
]
>
cell 6 keeps track of how many spaces we need to print currently
[
<<< go to space char
. print it
>>>- decrease counter
]
<<<<< move to line char
.>. print line and newline
>>+ move to cell 4 and increase our padding by 1
<<<<- back to cell 0; subtract line counter
]
-
2\$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$2022年04月22日 20:18:57 +00:00Commented Apr 22, 2022 at 20:18
Jelly, 7 bytes
Ṭ€ị)\ Y
(削除) Working on golfing. This is longer than I remember it being possible. (削除ここまで) The JHT exercise allows other characters so I can't get this to 5 bytes because of that :/
Ṭ€ị)\ Y Main Link
€ For each (implicit range)
Ṭ Generate a boolean list with 1s at the indices
ị Index that into
)\ "\ "
Y and join on newlines
-
\$\begingroup\$ How did you create the answer so fast?? I think you guys have some sort of unicode keyboard to enter theses characters very quickly :P \$\endgroup\$zoomlogo– zoomlogo2021年06月04日 14:34:24 +00:00Commented Jun 4, 2021 at 14:34
-
2\$\begingroup\$ @MaanasB I have the wiki pages bookmarked and then I just copy-paste, lol. This is a standard exercise from the Jelly Hyper-Training room and it's a pretty simple challenge (not that that's bad, of course!) \$\endgroup\$2021年06月04日 14:42:09 +00:00Commented Jun 4, 2021 at 14:42
-
1\$\begingroup\$ @MaanasB, I have (almost) all of the characters Japt uses on my phone's keyboard. \$\endgroup\$Shaggy– Shaggy2021年06月04日 15:20:27 +00:00Commented Jun 4, 2021 at 15:20
-
\$\begingroup\$ :/ two byte character literals. I'm disappointed in you, jelly :P \$\endgroup\$Wezl– Wezl2021年06月04日 17:14:15 +00:00Commented Jun 4, 2021 at 17:14
-
\$\begingroup\$ @Wzl ? (filler filler) \$\endgroup\$2021年06月04日 17:15:51 +00:00Commented Jun 4, 2021 at 17:15
C (clang), (削除) 90 (削除ここまで) (削除) 77 (削除ここまで) 60 bytes
i;main(n){for(scanf("%i",&n);i<n;printf("%*s\\\n",i++,""));}
My first work without int in it while still using it. I'm doing better now, aren't I?
Thanks to att for golfing 13 bytes. Thanks to ceilingcat for golfing 17 bytes.
-
-
\$\begingroup\$ 58 bytes \$\endgroup\$ceilingcat– ceilingcat2021年06月07日 06:59:29 +00:00Commented Jun 7, 2021 at 6:59
convey, 46 bytes
[0>>,+1
v"^
{,"=@#]}
>^}"~v#'\\'
' '!""~/}
Visualization (i use '_' instead of space because the gif doesnt show the space char if i use it, but in the official page the output works whit spaces):
Vyxal M, 7 bytes
(nI\\+,
Explanation:
( # range from 0 to implict input
n # loop variable
I # push that many spaces
\\ # backslash literal
+ # concatenate the spaces with the backslash
, # print
R, (削除) 42 (削除ここまで), 39 bytes
- -3 bytes thanks to @Dominic van Essen
write(strrep("\\",diag(x<-scan())),1,x)
Explanation:
- take x from standard input,
- create a diagonal matrix of size x
- repeat the character
'\'one time for each 1 of the matrix and 0-times for each 0 (= empty string) - print the matrix separating each character with a space
-
1\$\begingroup\$ 39 bytes using
strrep... but I've found another approach that's still shorter (so far)... \$\endgroup\$Dominic van Essen– Dominic van Essen2021年06月04日 19:00:36 +00:00Commented Jun 4, 2021 at 19:00 -
\$\begingroup\$ @DominicvanEssen Thanks! updated \$\endgroup\$digEmAll– digEmAll2021年06月04日 20:05:35 +00:00Commented Jun 4, 2021 at 20:05
MATLAB/Octave, (削除) 32 (削除ここまで) 31 bytes
-1 byte thanks to Luis Mendo
disp([60*eye(input(''))+32,''])
Try it online!
Reads the length from standard input, writes to standard output.
Makes use of identity matrix eye(x).
Alternatively, using function input/output, (削除) 22 (削除ここまで) 21 bytes:
@(x)[60*eye(x)+32,'']
Try it online!
Anonymous function, outputs character array.
05AB1E, 4 bytes
'3円Λ
Using the input as length, draw \ in direction 3 (down-right) with the canvas builtin Λ. See Kevin's tip for details on how the canvas works
6 bytes without the canvas builtin:
'\ILj»
For each number in the range IL == [1..input], pad the string "\" with leading spaces to this length (j). » joins the results by newlines.
Another 6 bytes solution suggested by Kevin Cruijssen:
L<'\ú»
For each number in the range L< == [0..input-1], pad the string "\" with that many leading spaces.
-
\$\begingroup\$ n o o o friggin ninjas \$\endgroup\$Makonede– Makonede2021年06月04日 15:42:16 +00:00Commented Jun 4, 2021 at 15:42
-
\$\begingroup\$ Alternative 6-byter using
ú:ݨ'\ú»/L<'\ú». \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2021年07月29日 09:48:20 +00:00Commented Jul 29, 2021 at 9:48 -
\$\begingroup\$ @KevinCruijssen thanks, added the second suggestion. It would be really nice if
jwould automatically swap arguments for integer lists as well. \$\endgroup\$ovs– ovs2021年07月30日 06:38:22 +00:00Commented Jul 30, 2021 at 6:38
Kotlin, 56 Bytes
fun d(a:Int){var t="";repeat(a){println(t+'\\');t+=" "}}
ungolfed version
I think this code is obvious enough
fun d(a:Int)
{
var t = ""
repeat(a)
{
println(t + '\\')
t += " "
}
}
-
\$\begingroup\$ the `\`s are spaced apart :/ \$\endgroup\$zoomlogo– zoomlogo2021年12月19日 13:20:10 +00:00Commented Dec 19, 2021 at 13:20
-
\$\begingroup\$ And you can use the header and footer on TIO like this: Try it online! \$\endgroup\$zoomlogo– zoomlogo2021年12月19日 13:24:11 +00:00Commented Dec 19, 2021 at 13:24
-
\$\begingroup\$ @PyGamer0 wait, the spaces are meant to incerment by 1 and not 2? well i can fix that, tomorrow. thx btw \$\endgroup\$random person– random person2021年12月19日 16:33:05 +00:00Commented Dec 19, 2021 at 16:33
-
\$\begingroup\$ It's been more than a month but I finally fixed it. @PyGamer0 Also thanks for tip \$\endgroup\$random person– random person2022年01月28日 10:45:15 +00:00Commented Jan 28, 2022 at 10:45
Nibbles, (削除) 5 (削除ここまで) 4 bytes (8 nibbles)
-2 nibbles thanks to a hint from Darren Smith
$&" "$"\\"
Explanation
$&" "$"\\"
$ Input number
(Implicit) Map over range from 1 to that number:
& Left-justify
"\\" string containing a backslash
$ to a width given by the function argument
" " by padding with spaces
Output each element of that list on a separate line (implicit)
Vyxal jṀ, 4 bytes
ƛ\\꘍
Flags go brrr
ƛ\\꘍ Full Program
ƛ For each (implicity loops from 0 to n - 1)
\\ push '\'
꘍ prepend x spaces to '\'
Ṁ is equivalent to mM, which makes implicit range start at 0 instead of 1 and end at n - 1 instead of n.
j joins the top of the stack on newlines at the end.
CJam, 10 bytes
ri{S*'\N}%
Explanation
r e# Read input
i e# Evaluate as an integer, n
{ }% e# Do the following for each k in [0 1 ... n-1]
e# Push k (implicit)
S e# Push space
* e# Repeat. Gives a string with k spaces
'\ e# Push character "\"
N e# Push newline
e# Output the stack (implicit)
PHP, (削除) 58 (削除ここまで) 57 bytes
for($i=0;$i<$argv[1];$i++)echo str_repeat(' ',$i)."\\\n";
This is my first golf, so feel free to mention anything I can do to improve this!
-
1\$\begingroup\$ Welcome to Code Golf, and nice first answer! Be sure to check out our Tips for golfing in PHP page for ways you can golf your program. Note that assuming the input is saved in a variable (e.g.
$n) is not an acceptable method of input. Instead, you should take input via command line arguments, as a function argument or via STDIN \$\endgroup\$2021年06月04日 19:57:28 +00:00Commented Jun 4, 2021 at 19:57 -
\$\begingroup\$ @cairdcoinheringaahing Thanks! I'll update it with a new version that takes input \$\endgroup\$SlamJammington– SlamJammington2021年06月04日 20:09:19 +00:00Commented Jun 4, 2021 at 20:09
-
2\$\begingroup\$ I'm not a PHP expert, but I can help improve your answer to 49 bytes. \$\endgroup\$dingledooper– dingledooper2021年06月04日 22:32:23 +00:00Commented Jun 4, 2021 at 22:32
-
2\$\begingroup\$ ...and down to 35 bytes: Try it online! \$\endgroup\$Sisyphus– Sisyphus2021年06月05日 04:50:40 +00:00Commented Jun 5, 2021 at 4:50