22
\$\begingroup\$

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 + '\\')

Try it online!

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.
asked Jun 4, 2021 at 14:30
\$\endgroup\$
9
  • \$\begingroup\$ This was on Sandbox for a couple of days :) \$\endgroup\$ Commented Jun 4, 2021 at 14:32
  • \$\begingroup\$ Does the explanation count towards the byte count :? \$\endgroup\$ Commented Jun 4, 2021 at 14:34
  • \$\begingroup\$ The explanation is only for how the program works \$\endgroup\$ Commented 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\$ Commented 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\$ Commented Jun 4, 2021 at 17:10

54 Answers 54

1
2
26
\$\begingroup\$

Canvas, 1 byte

Better tool for the job ;)

\

Try it here!

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.

answered Jun 4, 2021 at 15:08
\$\endgroup\$
3
  • 9
    \$\begingroup\$ lol, wow. definitely the perfect tool to pick :p \$\endgroup\$ Commented Jun 4, 2021 at 15:15
  • \$\begingroup\$ 13 hours too late for this :p \$\endgroup\$ Commented Jun 5, 2021 at 4:31
  • \$\begingroup\$ @Razetime 5 minutes too late to comment this... \$\endgroup\$ Commented Jun 5, 2021 at 4:37
10
\$\begingroup\$

Charcoal, 2 bytes

↖N

Try it online!

right tool for the job

answered Jun 4, 2021 at 14:40
\$\endgroup\$
1
7
\$\begingroup\$

APL (Dyalog Unicode), 9 bytes (SBCS)

Anonymous tacit prefix function. Returns a list of string, as per meta consensus.

'\'↑⍨ ̈-∘⍳

Try it online!

∘⍳ 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

answered Jun 4, 2021 at 14:47
\$\endgroup\$
6
\$\begingroup\$

Jelly, 7 bytes

=þị)\ Y

Try it online!

Ḷ6ẋp"\Y

Try it online!

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
answered Jun 4, 2021 at 14:31
\$\endgroup\$
6
\$\begingroup\$

Japt -R, 5 bytes

õ!ù'\

Try it

õ!ù'\ :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ç

Try it

'\iUç :Implicit map of each U in the range [0,input)
'\i :Prepend to "\"
 Uç : Space repeated U times
 :Implicit output joined with newlines
answered Jun 4, 2021 at 15:13
\$\endgroup\$
6
\$\begingroup\$

51AC8, 10 bytes

R[\ ×ばつ\\+t]

Try it Online!

-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
answered Jun 22, 2021 at 8:53
\$\endgroup\$
5
\$\begingroup\$

Python 2, 37 bytes

x='\\'
exec'print x;x=" "+x;'*input()

Try it online!

answered Jun 4, 2021 at 15:05
\$\endgroup\$
5
\$\begingroup\$

C (clang), 37 bytes

f(n){printf("%*c\n",n--,92,n&&f(n));}

Try it online!


C (gcc), 39 bytes

A recursive version suggested by @att.

f(n){--n&&f(n);printf("%*c\n",n+1,92);}

Try it online!


C (gcc), 44 bytes

i;f(n){for(i=0;i++<n;)printf("%*c\n",i,92);}

Try it online!

answered Jun 4, 2021 at 16:11
\$\endgroup\$
2
  • \$\begingroup\$ 39 bytes \$\endgroup\$ Commented Jun 5, 2021 at 1:53
  • \$\begingroup\$ @att Nice. That leads to a 37-byte version with clang. Undefined behavior for the win! ;-) \$\endgroup\$ Commented Jun 5, 2021 at 10:31
5
\$\begingroup\$

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+∘.=⍨⍳⎕
answered Sep 27, 2021 at 18:39
\$\endgroup\$
3
  • \$\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\$ Commented 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\$ Commented 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\$ Commented Oct 1, 2021 at 21:14
5
\$\begingroup\$

brainfuck, 113 bytes

,>>+++++++[<+++++++++++++>-]<+>++++++++++>>++++[<++++++++>-]<<<<[>>>>[->+>+<<]>[-<+>]>[<<<.>>>-]<<<<<.>.>>+<<<<-]

Try it online.

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
]
answered Apr 22, 2022 at 20:16
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Commented Apr 22, 2022 at 20:18
4
\$\begingroup\$

Jelly, 7 bytes

Ṭ€ị)\ Y

Try it online!

(削除) 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
answered Jun 4, 2021 at 14:31
\$\endgroup\$
5
  • \$\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\$ Commented 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\$ Commented Jun 4, 2021 at 14:42
  • 1
    \$\begingroup\$ @MaanasB, I have (almost) all of the characters Japt uses on my phone's keyboard. \$\endgroup\$ Commented Jun 4, 2021 at 15:20
  • \$\begingroup\$ :/ two byte character literals. I'm disappointed in you, jelly :P \$\endgroup\$ Commented Jun 4, 2021 at 17:14
  • \$\begingroup\$ @Wzl ? (filler filler) \$\endgroup\$ Commented Jun 4, 2021 at 17:15
4
\$\begingroup\$

J, 10 bytes

' \'{~=@i.

Try it online!

answered Jun 4, 2021 at 14:45
\$\endgroup\$
4
\$\begingroup\$

R, 35 bytes

cat(sep="\\
",strrep(" ",0:scan()))

Try it online!

answered Jun 4, 2021 at 18:50
\$\endgroup\$
4
\$\begingroup\$

C (clang), (削除) 90 (削除ここまで) (削除) 77 (削除ここまで) 60 bytes

i;main(n){for(scanf("%i",&n);i<n;printf("%*s\\\n",i++,""));}

Try it online!

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.

\$\endgroup\$
2
  • \$\begingroup\$ -13 using for instead of while \$\endgroup\$ Commented Jun 5, 2021 at 2:36
  • \$\begingroup\$ 58 bytes \$\endgroup\$ Commented Jun 7, 2021 at 6:59
4
\$\begingroup\$

convey, 46 bytes

[0>>,+1
 v"^
{,"=@#]}
 >^}"~v#'\\'
' '!""~/}

Try it online!

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

enter image description here

answered Jun 7, 2021 at 12:35
\$\endgroup\$
4
\$\begingroup\$

Vyxal M, 7 bytes

(nI\\+,

Try it Online!

Explanation:

( # range from 0 to implict input
 n # loop variable
 I # push that many spaces
 \\ # backslash literal
 + # concatenate the spaces with the backslash
 , # print
answered Dec 21, 2021 at 20:36
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 33 bytes

f=(n,s=`\\
`)=>--n?s+f(n,' '+s):s

Try it online!

answered Jun 4, 2021 at 14:51
\$\endgroup\$
3
\$\begingroup\$

R, (削除) 42 (削除ここまで), 39 bytes

  • -3 bytes thanks to @Dominic van Essen
write(strrep("\\",diag(x<-scan())),1,x)

Try it online!

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
answered Jun 4, 2021 at 15:17
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 39 bytes using strrep... but I've found another approach that's still shorter (so far)... \$\endgroup\$ Commented Jun 4, 2021 at 19:00
  • \$\begingroup\$ @DominicvanEssen Thanks! updated \$\endgroup\$ Commented Jun 4, 2021 at 20:05
3
\$\begingroup\$

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.

answered Jun 4, 2021 at 17:43
\$\endgroup\$
0
3
\$\begingroup\$

Stax, 3 bytes

しかく♦9

Run and debug it

Explanation

m'\)
m map 1..n and print with newlines
 '\) pad \ on the left with spaces to given length
answered Jun 5, 2021 at 4:33
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 4 bytes

'3円Λ

Try it online!

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»

Try it online!

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<'\ú»

Try it online!

For each number in the range L< == [0..input-1], pad the string "\" with that many leading spaces.

answered Jun 4, 2021 at 14:49
\$\endgroup\$
3
  • \$\begingroup\$ n o o o friggin ninjas \$\endgroup\$ Commented Jun 4, 2021 at 15:42
  • \$\begingroup\$ Alternative 6-byter using ú: ݨ'\ú»/L<'\ú». \$\endgroup\$ Commented Jul 29, 2021 at 9:48
  • \$\begingroup\$ @KevinCruijssen thanks, added the second suggestion. It would be really nice if j would automatically swap arguments for integer lists as well. \$\endgroup\$ Commented Jul 30, 2021 at 6:38
3
\$\begingroup\$

Kotlin, 56 Bytes

Try it online!

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 += " "
 }
}
answered Dec 19, 2021 at 10:48
\$\endgroup\$
4
  • \$\begingroup\$ the `\`s are spaced apart :/ \$\endgroup\$ Commented Dec 19, 2021 at 13:20
  • \$\begingroup\$ And you can use the header and footer on TIO like this: Try it online! \$\endgroup\$ Commented 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\$ Commented 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\$ Commented Jan 28, 2022 at 10:45
3
\$\begingroup\$

Nibbles, (削除) 5 (削除ここまで) 4 bytes (8 nibbles)

-2 nibbles thanks to a hint from Darren Smith

$&" "$"\\"

Attempt This Online!

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)
answered May 15, 2023 at 23:12
\$\endgroup\$
2
\$\begingroup\$

Python 3.8 (pre-release), 46 bytes

for i in range(int(input())):print(' '*i+'\\')

Try it online!

answered Jun 4, 2021 at 14:40
\$\endgroup\$
2
  • \$\begingroup\$ you should not answer your own question so soon \$\endgroup\$ Commented Jun 4, 2021 at 14:41
  • \$\begingroup\$ welp sorry didnt know that \$\endgroup\$ Commented Jun 4, 2021 at 14:43
2
\$\begingroup\$

Vyxal jṀ, 4 bytes

ƛ\\꘍

Try it Online!

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.

answered Jun 4, 2021 at 15:18
\$\endgroup\$
0
2
\$\begingroup\$

Red, 30 bytes

repeat i n[print pad/left"\"i]

Try it online!

answered Jun 4, 2021 at 16:20
\$\endgroup\$
2
\$\begingroup\$

Java, 56 bytes

n->{for(var s="\\";n-->0;s=" "+s)System.out.println(s);}

Try it online!

answered Jun 4, 2021 at 16:40
\$\endgroup\$
2
\$\begingroup\$

CJam, 10 bytes

ri{S*'\N}%

Try it online!

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)
answered Jun 4, 2021 at 16:51
\$\endgroup\$
2
\$\begingroup\$

PHP, (削除) 58 (削除ここまで) 57 bytes

for($i=0;$i<$argv[1];$i++)echo str_repeat(' ',$i)."\\\n";

Try it here!

This is my first golf, so feel free to mention anything I can do to improve this!

answered Jun 4, 2021 at 19:46
\$\endgroup\$
4
  • 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\$ Commented Jun 4, 2021 at 19:57
  • \$\begingroup\$ @cairdcoinheringaahing Thanks! I'll update it with a new version that takes input \$\endgroup\$ Commented 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\$ Commented Jun 4, 2021 at 22:32
  • 2
    \$\begingroup\$ ...and down to 35 bytes: Try it online! \$\endgroup\$ Commented Jun 5, 2021 at 4:50
2
\$\begingroup\$

Factor, 40 bytes

[ iota [ [ bl ] times "\\"print ] each ]

Try it online!

answered Jun 4, 2021 at 23:46
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.