| Bytes | Lang | Time | Link |
|---|---|---|---|
| 004 | Regenerate l | 210627T014748Z | DLosc |
| 030 | AWK | 250909T174010Z | xrs |
| 048 | Nibbles | 230515T231218Z | DLosc |
| 088 | Batch | 220423T234418Z | Youserna |
| 113 | brainfuck | 220422T201638Z | mechabub |
| 008 | K ngn/k | 220422T204212Z | coltim |
| 020 | Risky | 210730T122259Z | Adam |
| 024 | Perl 5 + n M5.10.0 | 220130T194358Z | Dom Hast |
| 011 | BQN | 211205T055906Z | zoomlogo |
| 056 | Kotlin | 211219T104827Z | random p |
| 007 | Vyxal M | 211221T203649Z | Alan Bag |
| 042 | Lua | 211219T054200Z | Visckmar |
| 057 | JavaScript Node.js | 211217T183448Z | Alan Bag |
| 014 | Dyalog APL | 210927T183912Z | Sohang C |
| 039 | Mathematica | 210930T030924Z | ntrupin |
| 010 | 51AC8 | 210622T085348Z | zoomlogo |
| 004 | 05AB1E | 210604T144959Z | ovs |
| 022 | Raku | 210624T215235Z | Sean |
| 046 | convey | 210607T123544Z | ZeroCode |
| 183 | Lolwho.Cares | 210606T170307Z | Robot |
| 060 | C clang | 210605T020656Z | Stack Ex |
| 034 | AWK | 210605T063656Z | cnamejj |
| 031 | Ruby | 210605T143201Z | gsus |
| 021 | Julia 1.0 | 210604T152357Z | MarcMush |
| 037 | C clang | 210604T161129Z | Arnauld |
| 012 | BQN | 210605T071645Z | frasiyav |
| 003 | Stax | 210605T043330Z | Razetime |
| 041 | Ruby | 210605T025503Z | Luis Est |
| 080 | Zephyr | 210605T020417Z | wasif |
| 034 | Perl 5 | 210605T010840Z | Chris |
| 040 | Factor | 210604T234628Z | chunes |
| 031 | MATLAB/Octave | 210604T174325Z | elementi |
| 039 | Bash | 210604T214728Z | Digital |
| 057 | PHP | 210604T194640Z | SlamJamm |
| 039 | R | 210604T151747Z | digEmAll |
| 035 | R | 210604T185031Z | Dominic |
| 010 | CJam | 210604T165149Z | Luis Men |
| 056 | Java | 210604T164037Z | Unmitiga |
| 025 | PowerShell | 210604T163646Z | wasif |
| 030 | Red | 210604T162039Z | 9214 |
| 042 | Python 3 | 210604T150250Z | wasif |
| 005 | Japt R | 210604T151307Z | Shaggy |
| 004 | Vyxal jṀ | 210604T151842Z | hyperneu |
| 001 | Canvas | 210604T150859Z | ovs |
| 006 | Vyxal j | 210604T145437Z | a stone |
| 013 | Retina 0.8.2 | 210604T150615Z | Neil |
| 037 | Python 2 | 210604T150530Z | ovs |
| 033 | JavaScript ES6 | 210604T145125Z | Arnauld |
| 009 | APL Dyalog Unicode | 210604T144704Z | Adá |
| 010 | J | 210604T144558Z | xash |
| 007 | Jelly | 210604T143137Z | caird co |
| 007 | Jelly | 210604T143139Z | hyperneu |
| 002 | Charcoal | 210604T144050Z | wasif |
| 046 | Python 3.8 prerelease | 210604T144044Z | zoomlogo |
Regenerate -l, 4 bytes
*\\
Takes input as the argument to the -l flag; for example,
python3 regenerate.py -r ' *\\' -l 4
\
\
\
\
Explanation
The regex *\\ matches 0 or more spaces, followed by a backslash. Regenerate finds all strings that match that regex, starting from the shortest: \, \, \, and so forth. By default, each match is printed on its own line, and the -l N option limits the output to the first N matches.
AWK, 30 bytes
{for(;$1--;)print(s=s" ")"\\"}
{for(;$1--;) # countdown from input
print # out
(s=s" ") # append space to string
"\\"} # slash
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)
Batch, 88 bytes
@setlocal enabledelayedexpansion&set s=\&for /l %%i in (1,1,%1)do @(echo !s!
set s= !s!)
Takes input from the command line.
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
]
K (ngn/k), 8 bytes
" \\"@=:
=:generate annbynidentity matrix" \\"@index into a string containing two characters - a space, and a backslash
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 += " "
}
}
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
JavaScript (Node.js), 57 bytes
f=n=>{for(i=0;++i<n+1;){console.log(" ".repeat(i)+"\\")}}
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+∘.=⍨⍳⎕
Mathematica, 39 bytes
We save some bytes by using an infix Array over Range, Table over StringRepeat, and Echo over Print.
We also cut it down to 33 bytes in TIO, because user input for Mathematica wasn't working :)
Echo[Table[" ",#]<>"\\"]&~Array~Input[]
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
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.
Raku, 22 bytes
{[~] (' 'Xx^$_)X~'\
'}
' ' Xx ^$_ generates a list of the spaces starting each line. X~ '\' appends a backslash and newline to each of those strings, then [~] concatenates them.
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):
Lolwho.Cares, 183 bytes
* >2,*1210+10 0*+ 0>,02001*101v
^ 010121 <
^ 121021<
>*+102021**+vv <
v 120210021`1012< +**120021 <
>*101*10102 2012 >*1012+201020`^
Given a decimal number N, will output a line of length N.
Explanation:
* >2,*1210+10 0*+ 0>,02001*101v
^ 010121 <
^ 121021<
This code reads from the input, and converts ascii decimal to a number. Side effect of it's working means it pushes N, followed by 0.
It does so by first reading a character, finishing when it read 0 (EOF). It then multiplies the current number by 10, and finally it subtracts 48 from the ASCII value and adds.
>*+102021**+vv <
v 120210021`1012< +**120021 <
>*101*10102 2012 >*1012+201020`^
This code takes [N, 0] pushed by the other function and draws a line accordingly. This makes use of the fact that the stack is "initialised" with an infinite amount of 0s at the bottom.
This is basically an implementation of nested for loops; It checks whether enough lines have been printed, then prints X spaces, followed by \ and newline.
The language is a custom esoteric programming language, similar to BEFUNGE (as user pointed out here).
Note: Due to the implementation of number reading, care must be taken to omit any non-decimal characters, including leading/trailing spacing/newlines.
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.
AWK, 34 bytes
{for(;$1--;x=x" ")y=y x"\\\n"}$0=y
This is pretty straightforward I suppose... Each time through the loop creates a string corresponding to one line of output. The control just counts decrements the first commandline argument until it's 0.
for(;$1--; )
The body of the loop appends the current output line to an accumulator variables, taking the current "padding" of blanks and appending \ and '\n` to it.
y=y x"\\\n"
The "end of loop" expression builds up the padding variable.
x=x" "
Then once all the lines have been generated, assigning that output string to $0 as a truthy test, without an action, causes the output to be printed.
$0=y
Ly, 24 bytes
0(10)'\<n[>&:[o]p' <1-]<
This is really a translation of the approach, just in Ly. So I'm not adding a separate entry for it.
The first task is to push a null terminated string \\n onto the stack.
0(10)'\
Then shift to a new stack and read in the number of lines.
<n
The construct to loop that many times shifts between stacks a decrements the top of the stack with the loop counter until it hits 0.
[> <1-]
The body of the loop, duplicates the stack with the output string on top of itself &: then prints up to the first \0, deletes that null and adds a space. So each iteration prints the current line and appends a space to the front in anticipation of the next iteration.
&:[o]p'
When the loop ends and all the lines have been printed, we just need to switch stacks to avoid printing what's left.
<
Ruby, 31 Bytes
gets.to_i.times{|i|p' '*i+'\\'}
Works in much the same way as the example code but more rubified
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);}
BQN, 12 bytesSBCS
↕⌽⁼⎉0‿1↑⟜"\"
↕⌽⁼⎉0‿1↑⟜"\" # tacit function which takes an int and returns a character array
↑⟜"\" # take input number of characters from the string "\", padding with spaces
⌽⁼ # rotate-inverse (right instead of left),
⎉0‿1 # rank 0‿1 (each scalar on the left pairs with a vector on the right)
↕ # by the indices 0 ... input
Stax, 3 bytes
■♦9
Explanation
m'\)
m map 1..n and print with newlines
'\) pad \ on the left with spaces to given length
Zephyr, 80 bytes
set s to" "
input n as Integer
for i from 1 to n
print s+"\"
set s to s+" "
next
Perl 5, 34 bytes
s//\\/;$==<>;s// / while(say,$=--)
This should be run from the command line like perl -E 's//\\/;$==<>;s// / while(say,$=--)' to activate the say feature without adding any bytes.
Ungolfed:
$_='\\'; #Set the default variable $_ to a single backslash
$==<>; #Take input into $=. This variable converts the input to an integer
while($=--){ #Decrement $= and loop
say; #Prints $_ and a newline
$_=" $_"; #Adds a space to the start of $_
}
Uses s// / as shorthand for $_=" $_" (i.e. to prepend a space to $_). Putting say into the while loop lets us drop the brackets from the loop body.
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.
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!
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
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)
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
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.
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.
Vyxal j, 6 bytes
ʁð*\\+
ʁð*\\+
ʁ Push range(input)
* Repeat...
ð the string ' ' that many times for each
\\+ Append a backslash to each
j flag: join on newlines
Retina 0.8.2, 13 bytes
.+
$*
$`\¶
Try it online! Note: Trailing spaces on lines 2 and 3. Explanation:
.+
$*
Convert the input to a string of spaces.
$`\¶
Output each prefix of the string followed by a \, on its own line.
This program outputs two trailing newlines, one from the code, one from Retina 0.8.2's default output. The latter can be suppressed at a cost of 2 bytes by changing the third line to \` . Alternatively the following 14-byte Retina 1 program outputs no trailing newlines:
.+
*
L$`
$`\
Try it online! Note: Trailing spaces on lines 2 and 3. Explanation: Much like the Retina 0.8.2 program, except the repetition operator is simply * and the list matches command only inserts newlines between the substitutions.
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
Ḷ⁶ẋ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
Ḷ⁶ẋp”\Y - Main link. Takes N on the left
Ḷ - Range [0, ..., N-1]
⁶ẋ - Repeat that many spaces for each
p”\ - Append "\" to each
Y - Join by newlines
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
