78
\$\begingroup\$

Given no input, your task is to generate the following:

a
 b
  c
   d
    e
     f
      g
       h
        i
         j
          k
           l
            m
             n
              o
               p
                q
                 r
                  s
                   t
                    u
                     v
                      w
                       x
                        y
                         z

Nonvisually, your task is to generate each letter in the alphabet, with spaces before it equal to its position in the alphabet minus one.

If you print this, it must appear like the above. Extraneous whitespace that does not affect appearance, as well as a trailing newline, is allowed. You can use all lowercase, or all uppercase.

You may also return this from a function as per usual rules, either as a string with newlines, or a list of strings.

This is , so shortest answer in bytes wins!

\$\endgroup\$
6
  • \$\begingroup\$ Do the spaces need to be real ASCII spaces, or can I give output like a<VERTICAL-TAB>b<VERTICAL-TAB>c...? How about if there are some backspace characters in there too? As long as the visual result is the same? \$\endgroup\$ Commented Jun 8, 2017 at 23:30
  • \$\begingroup\$ @DigitalTrauma as long as it appears the same, I don't care what kind of whitespace you use. \$\endgroup\$
    – Stephen
    Commented Jun 8, 2017 at 23:31
  • \$\begingroup\$ Can I use tabs instead of spaces? \$\endgroup\$
    – user69335
    Commented Jun 9, 2017 at 23:28
  • \$\begingroup\$ @yamboy1 hmm, probably not. Most tabs are set to a large number of spaces - if your diagonal looks like it has 4 spaces before the b, it won't look very diagonal. If it looks like the slope is ~-1 then it's fine. \$\endgroup\$
    – Stephen
    Commented Jun 9, 2017 at 23:32
  • \$\begingroup\$ does not affecting appearance include having an extra leading space or 2? \$\endgroup\$ Commented Sep 11, 2017 at 17:08

170 Answers 170

3
\$\begingroup\$

Python 3, 41 bytes

for n in range(26):print(" "*n+chr(n+97))
\$\endgroup\$
1
  • \$\begingroup\$ You can save 2 bytes by using Python 2 \$\endgroup\$
    – Beefster
    Commented Apr 17, 2018 at 17:56
3
\$\begingroup\$

i386 Bootsector, 19 bytes (Technically 512 due to padding)

Here is a hexdump of the program without padding:

0000-0010:  b4 0e b3 61-88 d8 cd 10-b0 0a cd 10-fe c3 80 fb  ...a.... ........
0000-0013:  7b 75 f1                                         {u.

And here is the original assembly:

mov ah, 0x0e
mov bl, 'a'
l:
    mov al, bl
    int 0x10
    mov al, 0xa
    int 0x10
    inc bl
    cmp bl, '{'
    jne l

Very simple. Diagonal works by printing a newline without a carriage return.

\$\endgroup\$
3
\$\begingroup\$

Rust, 59 bytes

for(i,l)in('a'..'{').enumerate(){println!("{l:>i$}",i=i+1)}
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$
    – rydwolf
    Commented Jan 22, 2023 at 21:35
  • \$\begingroup\$ 56 bytes \$\endgroup\$ Commented Jan 23, 2023 at 7:52
3
\$\begingroup\$

Vyxal 3 j, 5 bytes

nƛmꜝ«

Try it Online!

Woah a Vyxal 3 Answer!

\$\endgroup\$
2
\$\begingroup\$

QBIC, 27 bytes

[26|?space$(a-1)+chr$(a+96)

Explanation

[26|          FOR a = 1 to 26
?space$(a-1)  PRINT a-1 spaces (SPACES$ is a QBasic function that prints x spaces)
+chr$(a+96)      and a char representation of a+96
\$\endgroup\$
2
\$\begingroup\$

Tcl, 52 bytes

set i 96;time {puts [format %[incr j]c [incr i]]} 26

time is really a tool for measuring time spent, but it's also handy for repetition of code. Indentation and letter are advanced in each iteration. Ascii code i needs preset to one-before-first-letter, j will be auto-set to 1 on first incr.

If run in an interactive tclsh, the time would also output timings, but if the line is in a script, then it's silent, except for the diagonal.

\$\endgroup\$
2
\$\begingroup\$

ZX Spectrum BASIC, 29 bytes

FOR i=NOT PI TO VAL "25": LPRINT TAB i;CHR$ (i+VAL "97"): NEXT i

Numeric literals carry a 6-byte penalty, so using VAL saves me 3 bytes (VAL is a 1-byte token). Note: The ZX Spectrum's output area is only 22 lines high, so I'm sending the output to the printer instead.

\$\endgroup\$
2
\$\begingroup\$

T-SQL, 63 bytes

DECLARE @ INT=0a:PRINT SPACE(@)+CHAR(@+65)SET @+=1IF @<26GOTO a

Written more conventionally, that would be:

DECLARE @ INT=0
a:
    PRINT SPACE(@) + CHAR(@+65)
    SET @ += 1
IF @<26 GOTO a

Tested and working on SQL Server 2012.

\$\endgroup\$
2
\$\begingroup\$

Japt, 10 9 bytes

Saved 1 byte thanks to @Shaggy

;C£RiXiYî

Run it online!

Explanation:

;C£RiXiYî
;C          // Lowercase alphabet
  £         // Map; At each char:
   R        //   Newline
    iX      //   Insert: X (Iterative char)
      iYî   //   Insert: " " repeated Y (index) times

By default, i inserts the target char at index 0. So, RiXiYî becomes Yî +X+R.

\$\endgroup\$
4
  • \$\begingroup\$ ;C£RiXiYî for 9 bytes. \$\endgroup\$
    – Shaggy
    Commented Jun 8, 2017 at 6:52
  • \$\begingroup\$ ;C¬£XiYç with -R also works for 9. \$\endgroup\$
    – Shaggy
    Commented Jun 8, 2017 at 7:11
  • \$\begingroup\$ A short cut for would allow for an 8 byte solution ;) \$\endgroup\$
    – Shaggy
    Commented Jun 8, 2017 at 13:58
  • \$\begingroup\$ I imagine this wasn't available then but 7 bytes ;C¬Ëù°E \$\endgroup\$ Commented Feb 18, 2023 at 22:38
2
\$\begingroup\$

Nano, 186 77 bytes

M- represents the alt key

M-ia
 b
 c
 d
 e
 f 
 g
 h
 i
 j
 k
 l
 m
 n
 o
 p
 q
 r
 s
 t
 u
 v
 w
 x
 y
 z

Explanation

I've seen a few Vim answers on this site so I thought I would do a nano answer. Its not nearly as terse as the Vim answers but its still better than typing out the whole thing.

This is pretty simple (a lot simpler than my original answer). M-i sets the auto-indent on. This means that every line will copy the indentation of the last. So we start with a and every line add a space and a letter all the way through.

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 39 Bytes

Grid@DiagonalMatrix@Alphabet[]/. 0->" "

This has trailing whitespace.

Explanation:

                    Alphabet[]          - Create a list of the alphabet
     DiagonalMatrix@                    - Use that as the diagonal of a matrix
                              /. 0->" " - Replaces the 0's with whitespace
Grid@                                   - Then turn it into a 2D grid
\$\endgroup\$
1
  • \$\begingroup\$ Since a list of strings is allowed, and you are not asked to print it or anything, I don't think you need the Grid@. \$\endgroup\$
    – Mark S.
    Commented Aug 19, 2017 at 16:13
2
\$\begingroup\$

braingasm, 20 bytes

26[#[32.]97+#+.10.>]

26 times, print (.) a space (32) # (current position on the tape) times; increase cell (+) by 97, then by #; and print the current cell value, along with a newline (10); then go to next cell (>).

\$\endgroup\$
2
\$\begingroup\$

AWK, 40 bytes

BEGIN{for(;++i<27;)printf"%"i"c\n",i+96}

Try it online!

I'm curious if there's a shorter way to do this in AWK.

\$\endgroup\$
1
  • \$\begingroup\$ not sure if the rules allow it, but replace BEGIN with END and add 1 char for the needed ctrl-d ? \$\endgroup\$ Commented Jun 9, 2017 at 13:38
2
\$\begingroup\$

Perl 6, 25 bytes

{" " «x«^26 Z~'a'..'z'}

This function returns a list of the required strings: "a", " b", " c", ...

^26 is a list of the numbers from 0 to 25. «x« is the string-replication hyperoperator that maps each element n of its right-hand side to the left-hand side replicated n times, producing the list "", " ", " ", .... That list is then zipped with string concatenation (the Z~ operator) with the range of letters from 'a' to 'z'.

\$\endgroup\$
2
\$\begingroup\$

Scala, 41 bytes

('a'to'z').map(a=>println(" "*(a-'a')+a))

Previous answer: 45 bytes

(0 to 25).map(a=>printf("%s%c\n"," "*a,97+a))

It is 1 byte shorter than

(0 to 25).map(a=>println(" "*a+(97+a).toChar))

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Here is a 37 bytes solution: 'a'to'z'map(c=>println(" "*(c-97)+c)) \$\endgroup\$
    – 6infinity8
    Commented Nov 4, 2018 at 14:29
2
\$\begingroup\$

JAVASCRIPT

Option 1: 59 bytes

a='';for(i=0;i<26;)a+=''.padEnd(i++)+(i+9).toString(36)+`
`

Option 2: 60 bytes

a='';for(i=0;i<26;)a+=' '.repeat(i++)+(i+9).toString(36)+`
`

Option 3: 61 bytes

a='',i=0;while(i<26)a+=' '.repeat(i++)+(i+9).toString(36)+`
`

Option 4: 67 bytes

a='';for(i=0;i<26;)a+=' '.repeat(i++)+String.fromCharCode(96+i)+`
`

Option 5: 68 bytes

[...Array(26)].map(_=>' '.repeat(++i-10)+i.toString(36),i=9).join`
`
\$\endgroup\$
1
  • \$\begingroup\$ easily tested in the browser's console :) \$\endgroup\$
    – w3jimmy
    Commented Jun 8, 2017 at 20:00
2
\$\begingroup\$

Bash, 38 36 35 bytes

printf "%*s
" `echo $[++i]\ {a..z}`

Try it online!

2 bytes less thanks to @DigitalTrauma

1 byte less thanks to @NahuelFouilleul

"%*s\n" takes padding as an argument. $((++i))\ {a..z} for each letter increment and prepend i plus space (arithmetic expansion)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Replace $(( )) with $[ ]. \$\endgroup\$ Commented Jun 8, 2017 at 15:04
  • \$\begingroup\$ If another user suggests an edit that improves your score and you use it, it is usual to credit that user in your answer - Thanks! \$\endgroup\$ Commented Jun 11, 2017 at 18:30
  • \$\begingroup\$ I don't know if newline is counted as two characters or one, in that case \n can be replaced with new line \$\endgroup\$ Commented Jun 12, 2017 at 13:15
2
\$\begingroup\$

R, 56 bytes

for(i in 1:26){cat(rep(" ",i-1),LETTERS[i],'\n',sep='')}

Try it online!

Prints i-1 spaces and the ith letter of the alphabet, plus a newline, 26 times.

Outgolfed by Giuseppe (47 bytes), user2390246 (40 bytes), and Sven Hohenstein (37 bytes), but this is the only answer (so far) that doesn't use diag.

\$\endgroup\$
2
\$\begingroup\$

Pyth, 9 8 bytes

.e+*dkbG

Try it online

Explanation

.e     G  For each element of 'abc...z'...
   *dk    Take a string of spaces with length equal to the index...
  +   b   Add the letter.
\$\endgroup\$
3
  • \$\begingroup\$ 0 indexing really helps for this, doesn't it? \$\endgroup\$
    – Stephen
    Commented Jun 8, 2017 at 2:02
  • \$\begingroup\$ I'm two months late, but you can take out the j because outputting as a list is allowed. \$\endgroup\$
    – clapp
    Commented Jul 25, 2017 at 8:28
  • \$\begingroup\$ @clap Improvements are always welcome. \$\endgroup\$
    – user48543
    Commented Jul 25, 2017 at 13:02
2
\$\begingroup\$

Julia, 42 bytes

for i=0:25 println(" "^i,Char[97+i][1])end

Pretty straight forward, print space and character.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Ruby, 18 bytes

puts [*?a..?z]*?\v

Does not work on TIO, link for reference:

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Pyth, 11 bytes

j.b+*dNY26G

Explanation:

Try it online!

j.b+*dNY26G   expects no input

j             joins on new line
 .b           for...in loop, uses two iterator variables
   +          joins *dN and Y
    *dN       repeats d (space) N times, where N is one of .b's iterating variables
       Y      another one of .b's iterating variables
        26    the array being iterated for N,.b automatically takes the unary range
          G   initialized to the lowercase alphabet, the array being iterated for Y
\$\endgroup\$
2
\$\begingroup\$

SOGL V0.12, 2 bytes

z╝

Try it Here!

z   push the alphabet
 ╝  convert to a diagonal array
    implicitly output as a newline joined string
\$\endgroup\$
2
\$\begingroup\$

TI-Basic, 71 69 bytes

"       
For(I,1,26
Disp sub(Ans+Ans+Ans+Ans,1,I)+sub("ABCDEFGHIJKLMNOPQRSTUVWXYZ",I,1
End
\$\endgroup\$
3
  • \$\begingroup\$ Don't know how I missed that, thanks for -26 bytes @StephenS \$\endgroup\$
    – Timtech
    Commented Jun 8, 2017 at 15:50
  • \$\begingroup\$ I think you can just do sub(Ans+Ans+Ans+Ans,1,I). \$\endgroup\$
    – lirtosiast
    Commented Aug 25, 2017 at 0:43
  • \$\begingroup\$ @lirtosiast Thanks, I should have noticed that one. \$\endgroup\$
    – Timtech
    Commented Aug 25, 2017 at 2:19
2
\$\begingroup\$

Pyth, 13 bytes

I've noticed that I've been getting better at these, but Dave or Mr. Xcoder will school me any day of the week.

Update: Pyth tester is down, rip back up.

#p*dt=hZhG=tG

Explanation:

#         Loop until error
 p*dt=hZ  Print without newline: " " times ++Z - 1 (Z defaults to 0) (Could have used + instead of p, but it's easier to explain)
 hG       Implicit print with newline: The first character of the alphabet
 =tG      Remove the first letter in the alphabet variable

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Befunge-98 (PyFunge), 30 bytes

1+:0\:'`+\' \k:>:#,_$:'␚-!k@a,

Where is a literal substitute character

Every line has 3 leading spaces

Try it online!

\$\endgroup\$
2
\$\begingroup\$

uBASIC, 36 bytes

Anonymous function that takes no input and outputs to the console.

0ForI=0To25:?Tab(I);Chr$(65+I):NextI

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Soon: 100% of alphabet challenge are necromancer'd to the top being called "basic" xD. \$\endgroup\$ Commented Jan 31, 2018 at 18:31
  • 1
    \$\begingroup\$ @MagicOctopusUrn OH NO!!! You discovered my plot!! \$\endgroup\$ Commented Jan 31, 2018 at 18:36
  • 1
    \$\begingroup\$ Creating meta-thread to vote on replacing tag "alphabet" with "basic" lmao! Seriously, I love these challenges, but there isn't an alphabet challenge not completed in 05AB1E... ;_; \$\endgroup\$ Commented Jan 31, 2018 at 18:46
2
\$\begingroup\$

PowerShell, 35 bytes

$i=97;0..25|%{" "*$_+[char]$i;$i++}

Try it online!

Pipes an array of 0 through 25 to ForEach-Object, which cycles through ascii codes and prefixes with a space multiplied by an incrementing variable.

\$\endgroup\$
1
  • \$\begingroup\$ Roll the [char] calculation into the loop variable for 29 bytes -- Try it online! \$\endgroup\$ Commented Mar 19, 2018 at 13:25
2
\$\begingroup\$

Mouse-2002, 43 bytes

(A.Z1+<^A.B:(B.^" "B.1-B:)A.97+!'"!"A.1+A:)

Needs to be run in the Mouse-2002 Interpreter REPL. Working interpreter can be found here.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 51 bytes

print("\n".join(" "*n+chr(n+97)for n in range(26)))
\$\endgroup\$
1
  • \$\begingroup\$ you can avoid the join stuff by putting all this in a comprehensive list [print(" "*n+chr(n+97))for n in range(26)] \$\endgroup\$
    – bobrobbob
    Commented Jun 15, 2018 at 18:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.