67
\$\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\$
  • \$\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\$ – Digital Trauma Jun 8 '17 at 23:30
  • \$\begingroup\$ @DigitalTrauma as long as it appears the same, I don't care what kind of whitespace you use. \$\endgroup\$ – Stephen Jun 8 '17 at 23:31
  • \$\begingroup\$ Can I use tabs instead of spaces? \$\endgroup\$ – user69335 Jun 9 '17 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 Jun 9 '17 at 23:32
  • \$\begingroup\$ does not affecting appearance include having an extra leading space or 2? \$\endgroup\$ – MildlyMilquetoast Sep 11 '17 at 17:08

152 Answers 152

4
\$\begingroup\$

VBA, 35 Bytes

Anonymous VBE immediate window function that takes no input and outputs the alphabet, diagonally, to the VBE immediate window

For i=0To 25:?Spc(i)Chr(97+i):Next

-2 Bytes thanks to @Chronocidal


Alternatively, 76 50 46 Bytes

Anonymous VBE immediate window function that takes no input and outputs the alphabet, diagonally, to the range [A1:Z26] on the VBE immediate window function

[A:Z]="=If(Row()=Column(),Char(64+ROW()),"""")
\$\endgroup\$
  • \$\begingroup\$ You can save 3 bytes by changing Space(i);Chr(97+i) to Spc(i)Chr(97+i) \$\endgroup\$ – Chronocidal Mar 9 '18 at 17:30
  • 1
    \$\begingroup\$ @Chronocidal - you are absolutely correct - this answer dates back to when I was having some issues with spc() vs space() - Did you know that Space(n) can be assigned to a variable while Spc() cannot? \$\endgroup\$ – Taylor Scott Mar 9 '18 at 19:18
  • \$\begingroup\$ Oh and @Chronocidal you seem to do a lot of work with VBA, so if you haven't already, you might want to check out the Tips for golfing in VBA page. \$\endgroup\$ – Taylor Scott Mar 9 '18 at 19:21
4
\$\begingroup\$

Gforth, 45 Bytes

: X CR 26 0 DO i SPACES i 97 + EMIT CR LOOP ;

Explanation

Gforth doesn't allow interpretation of loops, so I had to define a new word X.

: X                 \ Define new word X
   CR               \ Go to a new line
   26 0 DO          \ Loop from 0 to 25
   i SPACES         \ Print i spaces
   i 97 + EMIT      \ Calculate ASCII-code and print it
   CR               \ Next line
;                   \ End of word definition

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Martin Ender Apr 17 '18 at 20:09
4
\$\begingroup\$

Kotlin, 36 35 bytes

Saved a byte by removing comma (lambda outside of call.)

{List(26){" ".repeat(it)+('a'+it)}}

Try it online!

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

Brachylog, 17 bytes

25⟦{;Ṣj₍}ᵐ;Ạzcᵐẉᵐ

Try it online!

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

Brachylog, 16 bytes

Ạ;I∋₎C∧Ṣ;Ij₎wCẉ⊥

Try it online!

Explanation

Ạ;I∋₎C             C is the Ith character of "abcdefghijklmnopqrstuvwxyz"
      ∧Ṣ;Ij₎       Juxtapose " " I times
            w      Write to STDOUT
             Cẉ    Writeln C to STDOUT
               ⊥   False: try another C
\$\endgroup\$
3
\$\begingroup\$

J, 28 bytes

([,{&a.@+&97@#)@#&' '"0 i.26

pretty:

([ , {&a. @ +&97 @ #) @ #&' ' "0 i.26
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome on the site! While there is no obligations to do so, feel free to add some explanations and maybe a link to an online interpreter where we can test your answer (like Tryitonline for instance). \$\endgroup\$ – Dada Jun 8 '17 at 8:03
  • \$\begingroup\$ You can save a byte by using "+i.26 instead of "0 i.26 \$\endgroup\$ – Conor O'Brien Sep 7 '17 at 23:01
3
\$\begingroup\$

C#, 82 bytes

using System.Linq;()=>Enumerable.Range(0,26).Select(i=>"".PadLeft(i)+(char)(i+65))

Works by creating an array of ints from 0 - 25, creates a string of i length of only spaces. Then adds a char onto the end corresponding to an upper case letter. It lastly implicitly returns an array, in this case an IEnumerable<string>, of each line.

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

MATL, 5 bytes

2Y2Xd

Try it online!

Explanation

2Y2    % Predefined literal: 'a':'z'
Xd     % Diagonal matrix. Implicit display
\$\endgroup\$
3
\$\begingroup\$

><>, 50 49 46 45 38 bytes

0>:5d*+oao:64*)?;1+:191.
?v84*o:{1+:}(

Try it online!

Explanation

0>          # initialize outer counter and start outer loop
:5d*+oao    # add outer counter to 65 and print as ascii followed by a newline
64*)?;      # if outer counter is greater than 24, end program
1+:1        # increment outer counter and initialize inner counter
91.         # jump to inner loop
:}(?v       # if inner counter is greater than outer counter go back to outer loop
84*o        # else print a space character
:{1+        # and increment inner counter
\$\endgroup\$
  • \$\begingroup\$ Your TIO link has an extra > command on the first line but also having a quick look; move the . on the second line to the end and you can then remove the space on the last line for -1 byte :) \$\endgroup\$ – Teal pelican Jun 8 '17 at 13:35
  • \$\begingroup\$ @Tealpelican: Thanks for the suggestion. I did some other changes that saved a byte, but I will check if your suggestion might have saved me more :) \$\endgroup\$ – Emigna Jun 8 '17 at 13:40
3
\$\begingroup\$

R, 40 bytes

write(c("",LETTERS)[diag(1:26)+1],"",26)

Try it online

                    diag(1:26)+1           # Create a matrix of ones with 2:27 on the diagonal
      c("",LETTERS)                        # Create a vector containing an empty string followed by the alphabet
      c("",LETTERS)[diag(1:26)+1]          # Use that vector as a lookup table for the matrix
write(                           ,"",26)   # Print over 26 columns

Use of write() copied from @Giuseppe's answer, but uses a different approach to creating the matrix.

Outgolfed by Sven Hohenstein

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

Go, 97 bytes

package main;import."strings";func main(){for i:=0;i<26;i++{println(Repeat(" ",i)+string(i+97))}}

Definitely not the best language to golf with but wanted to get my first solution up, also should note this prints to stderr but I assume that's okay if it's not specificed

Go (as far as I know) lacks the ability to specify a dynamic width for padding formatted strings which is why I had to bring in and use strings.Repeat()

Readable version:

package main

import . "strings"

func main() {
    for i := 0; i < 26; i++ {
        println(Repeat(" ", i) + string(i+97))
    }
}
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! Yup, STDERR is OK. Nice first post, I'm out of upvotes for the day :/ \$\endgroup\$ – Stephen Jun 8 '17 at 15:37
  • \$\begingroup\$ No problem, thanks for the welcome @StephenS :) - I don't know if this is the best place to ask but I was looking on tryitonline and people seem to use the header/footer parts for stuff like imports, is that valid for all/most submissions? would probably save me a lot of bytes on Go stuff \$\endgroup\$ – smt Jun 8 '17 at 19:18
  • \$\begingroup\$ uhhh I don't know, try asking on meta, sorry \$\endgroup\$ – Stephen Jun 8 '17 at 19:22
3
\$\begingroup\$

Perl: 23 bytes

22 + 1 for -E flag.

say$"x$i++,$_ for a..z

Usage:

perl -E 'say$"x$i++,$_ for a..z'
\$\endgroup\$
3
\$\begingroup\$

k, 21 bytes

`0:`c$32+(65+!26)*=26

Try it online.

Explanation:

                  =26 /identity matrix of length 26
         (65+!26)*    /for i in {0..25}:
                      /    matrix[i] *= 65 + i
      32+             /add 32 to everything
                      /    turns 0's into spaces
                      /    brings us to a lowercase alphabet for the rest
   `c$                /turn to characters
`0:                   /write to stdout/stderr
\$\endgroup\$
3
\$\begingroup\$

Ohm, 10 bytes

αß:^MüL;_,

Try it online!

Explanation

αß:^MüL;_,  
αß          # Pushes [a-z]
  :         # For each...
   ^MüL;    # Print index amout of spaces
        _,  # Prints the char
\$\endgroup\$
  • 4
    \$\begingroup\$ I think this is the wrong challenge :) did you mean this one? codegolf.stackexchange.com/questions/125104/… \$\endgroup\$ – Stephen Jun 8 '17 at 23:06
  • \$\begingroup\$ Yep you're right xD. Probably had the wrong tab open \$\endgroup\$ – Datboi Jun 9 '17 at 16:37
  • \$\begingroup\$ Updated the code to something that actually does this challenge \$\endgroup\$ – Datboi Jun 9 '17 at 16:50
3
\$\begingroup\$

Python 2, 42 bytes

Out-golfed... by Dennis, no less... ><

lambda:[' '*n+chr(n+65)for n in range(26)]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Any reason why you chose uppercase over lowercase? \$\endgroup\$ – Leaky Nun Jun 8 '17 at 2:54
  • 1
    \$\begingroup\$ @LeakyNun First number I tried and I had to leave. \$\endgroup\$ – totallyhuman Jun 8 '17 at 9:55
3
\$\begingroup\$

Common Lisp, 58 bytes

(dotimes(i 26)(format t"~v@a~%"(1+ i)(code-char(+ i 97))))

Try it online!

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

q/kdb+, 26 22 bytes

Solution:

-1(!:[26]#'" "),'.Q.a;

Example:

q)-1(!:[26]#'" "),'.Q.a;
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

Explanation:

Joins each letter of the alphabet with an increasing number of spaces. Brackets are used due to right-to-left evaluation.

-1(til[26]#'" "),'.Q.a; / ungolfed solution
-1                    ; / print to stdout and swallow return value
                  .Q.a  / shorthand for abc..xyz
                ,'      / concatenate (,) each-left/each-right (')
  (            )        / do all this together
          #'" "         / take each-left/each-right (space)
   til[26]              / range of 0..25
\$\endgroup\$
3
\$\begingroup\$

Perl 5 + -l, 21 bytes

$\.=$",print for a..z

Try it online!

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

SmileBASIC 3, 33 bytes

FOR I=0TO 25?" "*I+CHR$(97+I)NEXT

Looks like 12Me hasn't answered this one yet…

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

Underload, 104 bytes

(z)(y)(x)(w)(v)(u)(t)(s)(r)(q)(p)(o)(n)(m)(l)(k)(j)(i)(h)(g)(f)(e)(d)(c)(b)(a)(
)(:S( )*~S):*::*:*::***^

Try it online!

The snippet :S( )*~S has the meaning:

  • Duplicate and print the TOS
  • Add a space to the TOS
  • Print the string under the TOS

If the TOS starts as a newline and the strings on the stack form the alphabet (with a highest), then executing this snippet 26 times generates the correct output.

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

Assembly (nasm, x64, Linux), 62 59 55 45 + 8 bytes

func:
  ; R8 is the buffer the function will write the output to.
  ; most GPRs must be 0'd!
  mov al, 'a'
  inc edx
  or r8, buf ; Suprise! It's more efficient to write it this way, the equivilent mov requires 3 bytes of padding to follow.
.append:
  mov byte [r8+rbx], al
  mov byte [r8+rbx+1], 0Ah
  add ebx, 2
  cmp al, 'z'
  je .exit
  inc eax
  mov ecx, edx
  inc edx
.dospaces:
  mov byte [r8+rbx], ' '
  inc ebx
  ; cx is used here. Be aware!
  loop .dospaces
  jmp .append
.exit:
  ret

Try it online!

I spent a fair while doing this one. Seriously, some things will slowly drive you insane. This function outputs a buffer containing the requested data. Said buffer is in the .bss section.

NOTE: It doesn't seem to work on TIO for some reason. Works perfectly fine on my laptop. I built it using the following commands:

nasm -felf64 diagonal-alphabet_125117.s
gcc diagonal-alphabet_125117.o -no-pie 

For your viewing pleasure, the TIO version is modified to use the data section instead, so it actually runs.

The bytecount is currently based on the bytecount of the function and the 8 bytes needed to represent the length of .bss

Note to future readers: A CC where most GPRs must be 0'd can be useful, because or is shorter than mov, at least in the case demonstrated above.

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

Kotlin, 39 bytes

{('a'..'z').map{" ".repeat(it-'a')+it}}

Lambda returning a list of lines.

Try it online!

{                                  // lambda; implicitly return
    ('a'..'z').map {               // map over the lowercase alphabet
        " ".repeat(it - 'a')       // repeat space by letter position
                             + it  // append letter
    }
}
\$\endgroup\$
3
\$\begingroup\$

Yabasic, 49 bytes

Another answer which takes no input and outputs to the console.

For i=0To 25
For j=1To i?" ";Next
?Chr$(65+i)Next

Try it online!

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

PowerShell, 29 26 bytes

'a'..'z'|%{' '*($_-97)+$_}

Try it online!

Output:

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
\$\endgroup\$
  • \$\begingroup\$ Welcome! There is the more correct answer - codegolf.stackexchange.com/a/125139/80745 \$\endgroup\$ – mazzy Nov 21 '18 at 20:54
  • \$\begingroup\$ @mazzy Thanks for letting me know. I tried to look through and find one but must've missed it! However I don't believe it's more correct. Same length, they just used lowercase, which case was said doesn't matter. Could you explain? Thanks! \$\endgroup\$ – KGlasier Nov 21 '18 at 21:00
  • 1
    \$\begingroup\$ You are right, I wasn't precise enough. That solution is closer to the task definition. It is matter if lengths are equals. \$\endgroup\$ – mazzy Nov 21 '18 at 21:09
  • 1
    \$\begingroup\$ @mazzy How about this one? I think I won now :P \$\endgroup\$ – KGlasier Nov 22 '18 at 22:34
  • \$\begingroup\$ Cool! It would be good to point out Powershell 6+ \$\endgroup\$ – mazzy Nov 23 '18 at 5:43
3
\$\begingroup\$

MathGolf, 10 7 bytes

▄{ï *▌p

Try it online!

I'm happy to announce that MathGolf now has a 1-byte literal for the lowercase alphabet! That should make any challenge related to the alphabet a lot easier!

Explanation

▄         Push alphabet as string
 {        For-each
  ï *     Push " "*(loop counter)
     ▌    Prepend to the character
      p   Print
\$\endgroup\$
3
\$\begingroup\$

Python 3, 41 bytes

for n in range(26):print(" "*n+chr(n+97))
\$\endgroup\$
  • \$\begingroup\$ You can save 2 bytes by using Python 2 \$\endgroup\$ – Beefster Apr 17 '18 at 17:56
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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