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

4
\$\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\$
1
  • \$\begingroup\$ An equivalent version in k4 is -1@(-1-!26)$'$.Q.a;; although since the question allows returning a list of strings, (-1-!26)$'$.Q.a should be valid as well. \$\endgroup\$
    – coltim
    Commented Oct 31, 2020 at 21:43
4
\$\begingroup\$

RProgN 2, 5 bytes

aS`\x0B.

\x0B is a Vertical Tab Literal

This just takes the lowercase alphabet, splits it, and joins it with vertical tabs. This produces the intended effect on certain Bash Terminals.

Running the script

\$\endgroup\$
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\$
3
  • \$\begingroup\$ You can save 3 bytes by changing Space(i);Chr(97+i) to Spc(i)Chr(97+i) \$\endgroup\$ Commented Mar 9, 2018 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\$ Commented Mar 9, 2018 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\$ Commented Mar 9, 2018 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
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Commented Apr 17, 2018 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\$
4
\$\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\$
5
  • \$\begingroup\$ Welcome! There is the more correct answer - codegolf.stackexchange.com/a/125139/80745 \$\endgroup\$
    – mazzy
    Commented Nov 21, 2018 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
    Commented Nov 21, 2018 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
    Commented Nov 21, 2018 at 21:09
  • 1
    \$\begingroup\$ @mazzy How about this one? I think I won now :P \$\endgroup\$
    – KGlasier
    Commented Nov 22, 2018 at 22:34
  • \$\begingroup\$ Cool! It would be good to point out Powershell 6+ \$\endgroup\$
    – mazzy
    Commented Nov 23, 2018 at 5:43
4
\$\begingroup\$

Vyxal rj, 8 7 5 bytes

Thanks to lyxal for -1 bytes.

kaƛC꘍

Try it Online!

There's a... couple leading spaces per line.

Explanation:

ka     # Lowercase alphabet
  ƛ    # For each:
   C   #   Convert to ordinal value
    ꘍  #   Prepend that many spaces
       # 'j' flag - join top of stack with newlines and print
\$\endgroup\$
1
  • \$\begingroup\$ 7 bytes \$\endgroup\$
    – lyxal
    Commented Apr 28, 2021 at 1:55
4
\$\begingroup\$

Perl 5.28.1 (webperl) + -l013, 14 bytes

print for a..z

Try it online!


Perl 5 + -l, 21 bytes

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

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
  • 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
    Commented Jun 8, 2017 at 8:03
  • \$\begingroup\$ You can save a byte by using "+i.26 instead of "0 i.26 \$\endgroup\$ Commented Sep 7, 2017 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\$
2
  • \$\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\$ Commented Jun 8, 2017 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
    Commented Jun 8, 2017 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\$
3
  • \$\begingroup\$ Welcome to PPCG! Yup, STDERR is OK. Nice first post, I'm out of upvotes for the day :/ \$\endgroup\$
    – Stephen
    Commented Jun 8, 2017 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
    Commented Jun 8, 2017 at 19:18
  • \$\begingroup\$ uhhh I don't know, try asking on meta, sorry \$\endgroup\$
    – Stephen
    Commented Jun 8, 2017 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\$

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\$
3
  • 4
    \$\begingroup\$ I think this is the wrong challenge :) did you mean this one? codegolf.stackexchange.com/questions/125104/… \$\endgroup\$
    – Stephen
    Commented Jun 8, 2017 at 23:06
  • \$\begingroup\$ Yep you're right xD. Probably had the wrong tab open \$\endgroup\$
    – Datboi
    Commented Jun 9, 2017 at 16:37
  • \$\begingroup\$ Updated the code to something that actually does this challenge \$\endgroup\$
    – Datboi
    Commented Jun 9, 2017 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\$
2
  • \$\begingroup\$ Any reason why you chose uppercase over lowercase? \$\endgroup\$
    – Leaky Nun
    Commented Jun 8, 2017 at 2:54
  • 1
    \$\begingroup\$ @LeakyNun First number I tried and I had to leave. \$\endgroup\$ Commented Jun 8, 2017 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\$

brainfuck, 64 bytes

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

Try it online!

Prints in uppercase with a trailing newline.

Initialises the tape to 2N, and uses the 16(-3) as the newline, the 32 as the space, the 64 as the start of the uppercase alphabet, the 128(+2) as the counter (-5 each loop), and the cell at the end as the space counter.

\$\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\$

J, 19 16 bytes

u:32+(*=)65+i.26

Try it online!

\$\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\$

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\$

C (gcc), 37 bytes

f(i){for(i=2912;i++%113;printf(&i));}

Inspired by @NieDzejkob; needs to be run on a little endian ASCII machine from a terminal supporting vertical tabs. Try it online!

\$\endgroup\$

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.