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

1
\$\begingroup\$

Batch, 187 114 bytes

@set s=
@for %%p in (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)do @call:e %%p
:e
@echo(%s%%1
@set s= %s%
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You don't need the @s inside the ()s. Also, you don't need c at all - you can just add a space to s each time. By avoiding setlocal enabledelayedexpansion I got this down to 112 bytes. \$\endgroup\$
    – Neil
    Commented Jun 12, 2017 at 15:33
  • \$\begingroup\$ I've successfully removed the @s, variable c, and now I couldn't figure out how to echo the variable s without delayedexpansion. \$\endgroup\$
    – stevefestl
    Commented Jun 13, 2017 at 8:15
  • 1
    \$\begingroup\$ Since you're so close already, here's my 112-byte solution: @set s=&for %%p in (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)do @call echo %%s%%%%p&call set s= %%s%%. \$\endgroup\$
    – Neil
    Commented Jun 13, 2017 at 12:13
1
\$\begingroup\$

tcl, 54 47

time {puts [format %[incr i]c [expr $i+96]]} 26

demo

\$\endgroup\$
2
  • \$\begingroup\$ @avl42: mine is longer than yours :P \$\endgroup\$
    – sergiol
    Commented Jun 12, 2017 at 17:09
  • \$\begingroup\$ @avl42: Mine is NOW shorter than yours, using your own poison! \$\endgroup\$
    – sergiol
    Commented Jun 13, 2017 at 23:04
1
\$\begingroup\$

vim 14+16=30 keystrokes

16 keystrokes if you have alpha in nrformats already (I mean, who hasn't?), 30 otherwise.

:set nf=alpha<cr>
ia<esc>
qqYp<c-a>I <esc>q
24@q

Angle brackets denote single characters, e.g. <c-a> is ctrl+a (increment). Actual newlines only for clarity. Seriously, set nf+=alpha is really neat, and it's a feature I've missed in a few specific cases when programming.

\$\endgroup\$
1
\$\begingroup\$

Rexx (Regina), 41 37 bytes

do i=1 to 26
  say right(d2c(96+i),i)
end

Try it online!

\$\endgroup\$
1
\$\begingroup\$

SimpleTemplate, 66 42 bytes

This simply loops through all characters and outputs them one by one, with newlines and spaces.

{@forfrom"a"to"z"}{@echolV,_}{@setV V," "}

This abuses the automatic variable _ and the automatic flattening of echoed arrays and automatic newline with echol.

Ungolfed: (produces invalid results due to whitespace)

{@for chr from "a" to "z"}
    {@echo indentation, chr, "\n"}
    {@// will create an array like: [[[..., " "], " "], " "]}
    {@set indentation indentation, " "}
{@/}{@// automatically added after the code}


Old version:

There's a repeat function, but I couldn't use it because it's broken.
But this works too.

{@for_ from"a"to"z"}{@incz}{@callstr_repeat intos" ",z}{@echols,_}

Ungolfed: (produces incorrect results due to whitespace added)

{@set z 0}{@//removes warning}
{@for chr from "A" to "Z"}
    {@inc spaces by 1}
    {@call str_repeat into indentation " ", spaces}
    {@echo indentation, chr, "\n"}
{@/}{@// automatically added after the code}
\$\endgroup\$
1
\$\begingroup\$

Retina, 93 bytes


aAbBc¶3d¶4e¶5f¶6g¶7h¶8i¶9jA0kA1lA2mA3nA4oA5pA6qA7rA8sA9tB0uB1vB2wB3xB4yB5z
B
¶2
A
¶1
\d+
$* 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Noether, 52 bytes

"abcdefghijklmnopqrstuvwyxz"~aL(" "i*Pai/P"
"Pi1+~i)

Try it here!

Pushes the alphabet and loops through it, increasing the number of spaces before the character by one each time.


A non competing solution for 26 24 bytes is:

26(" "i*P0Ai/P"
"Pi1+~i)

Try it here!

\$\endgroup\$
1
\$\begingroup\$

Swift 3, 75 bytes

for i in 0..<26{print(String(repeating:" ",count:i)+String(i+10,radix:36))}
\$\endgroup\$
1
\$\begingroup\$

Groovy, 40 38 bytes

i=0;("a".."z").each{println" "*++i+it}

Previous solution:

0.upto(25){println" "*it+("a".."z")[it]}

iis used to store the number of spaces that must be printed before each letter. The program iterates through the range "a".."z" (the alphabet), printing each letter with i spaces before it and increments i inline.

\$\endgroup\$
1
\$\begingroup\$

Common Lisp, SBCL, 70 bytes

(dotimes(i 26)(format t"~v@t~a
"i(aref"abcdefghijklmnopqrstuvwxyz"i)))

Looping through letters of alphabet and outputting accurate number of spaces before each letter.

another 70 bytes solution:

(format t"~{~26<~a~#@t~>
~}"(coerce"abcdefghijklmnopqrstuvwxyz"'list))

looping through letters and justifying to 26 columns, where word to justify is letter+accurate number of spaces.

building alphabet list using code-char would make it 76 bytes:

(format t"~{~26<~a~#@t~>
~}"(loop as i from 97 to 122 collect(code-char i)))
\$\endgroup\$
1
\$\begingroup\$

dc, 53 52 bytes

0si[32Plj1+dsjli>S]sS[0sjlid0<S1+dsi64+dPAP90>M]dsMx

Try it online!

Uppercase because checking that we're below character 90 (Z) is one fewer byte than checking that we're below character 122 (z). Otherwise pretty straightforward: one macro to print spaces and another to call the spacing macro, print the letter, and print a newline.

-1 byte because I forgot to golf my 10 into an A.

\$\endgroup\$
1
\$\begingroup\$

Pyth, 9 bytes

m+*;xGddG

Try it online!

The other Pyth approach is golfier because it uses enumerated map, which is 1 byte shorter. I just wanted to post a solution with regular map m, that happens to use the same variable, d as the built-in space. That may be inconvenient, because you cannot refer to the space with d. However, I did a work-around with ;, which gets the global value of the innermost lambda variable.

This returns a list of lines. In order to join them by newlines, we just need to add j in front of the whole code (the link contains the j for readability).

Explanation

jm+*;xGddG  - Full program.

 m       G  - Map over the alphabet with a variable d.
  +         - Add:
   *          - The multiplication of:
     xGd        - The index of the current letter in the alphabet (0-indexed) and
    ;           - Space (which is the global value of d, the innermost lambda 
variable).
        d     - The current letter.
j           - Optional: Join by newlines.
\$\endgroup\$
1
\$\begingroup\$

QBasic, 54 bytes

SCREEN 12
FOR x=1TO 26
LOCATE x,x
?CHR$(x+96)
NEXT

Explanation

SCREEN 12       Set the screen mode to something that can actially handle > 20 chars on a line
FOR x=1TO 26    Loop from 1 to 26
LOCATE x,x      Set the cursior to the screen position x,x, starting at 1,1 on the top left.
?CHR$(x+96)  Print the current letter of the alphabet
NEXT
\$\endgroup\$
1
\$\begingroup\$

C#, 82 bytes

var b="";for(int i=0;i<=25;i++){var a=(char)(97+i);b+=new string(' ',i)+a+"\r\n";}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C++ (gcc), 73 bytes

As unnamed generic lambda, requires a stream as parameter (like std::cout)

[](auto&O){for(char c=96;++c<123;O<<c<<'\n')for(char w=c-96;--w;O<<' ');}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Recursiva, 16 15 bytes

{)"P+*-O}97' '}

Try it online!

Explanation:

{)"P+*-O}97' '}
{                - For each
 )               - lower-case alphabet yield 'abc...z'
  "              - Iteration command begin
   P             - Print
    +            - concatenate
     *-O}97' '   - Appropriate number of spaces
              }  - current alphabet (iterate)          
\$\endgroup\$
1
\$\begingroup\$

Pushy, 10 bytes

`a\x26`:"h32}

Where \x26 represents the literal byte 0x26.

Try it online!

`a\x26`         \ Push 'a' and 26
       :        \ Pop 26, loop that many times:
        "       \   Print the stack
         h      \   Increment the letter
          32}   \   Prepend a space
\$\endgroup\$
1
\$\begingroup\$

AWK, 40 bytes

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

Try it online!

I didn't see an AWK solution, so here we go. Straightforward implementation, so more golfing may be possible.

\$\endgroup\$
1
\$\begingroup\$

Sincalr ZX81/Timex TS1000/1500 BASIC, ~45 tokenized BASIC bytes

 1 FOR I=CODE "A" TO CODE "Z"
 2 PRINT TAB (I-CODE "A");CHR$ I
 3 NEXT I

Some notes; without adding in more bytes to the listing, the standard output to the screen is 32 x 22 only, so when the screen fills, you will need to press C, and the keyword CONT will appear. You will then see the rest of the alphabet.

For those who want to see the Alphabet without having to manually enter CONT each time the last row is hit in the text area, this solution does it for you:

 1 FOR I=CODE "A" TO CODE "Z"
 2 PRINT AT CODE "+",I-CODE "A";CHR$ I
 3 SCROLL
 4 NEXT I

This version costs another 9 tokenized bytes though.

\$\endgroup\$
1
\$\begingroup\$

Pyth, 12 bytes

Fb26p*\ b@Gb

There's probably some way I can shave off a couple bytes, but I'm not quite sure yet.

Explanation:

Fb26p*\ b@Gb |
Fb26         | for b in range(26):
    p        |     print(
     *\ b    |           " "*b
         @Gb |                +G[b]

Note: G is initialized to "abcdefghijklmnopqrstuvwxyz"
\$\endgroup\$
1
\$\begingroup\$

MY-BASIC, 62 bytes

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

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Visual Basic .NET (Mono), 98 bytes

Declared Subroutine that that takes no input and outputs to the console.

Module M
Sub Main
Dim I
For I=0To 25
Console.WriteLine(Space(i)+Chr(65+I))
Next
End Sub
End Module

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Momema, 51 bytes

m01*0w1-9 32 1+*1-1w0w=*1-9+97*0-9 10 0+1*0m=+-26*0

Try it online!

Explanation:

                                                              #  a = 0
m   0        #  label m0: jump past label m0 (no-op)          #  do {
1   *0       #            [1] = [0]                           #    b = a
w   1        #  label w0: jump past label w1                  #    while b != 0 {
-9  32       #            print chr 32                        #      print ' '
1   +*1-1    #            [1] = [1] - 1                       #      b -= 1
w   0        #  label w1: jump past label w0 (no-op)          #    [end-of-loop jump point]
w   =*1      #  label w2: jump past label w((2 + !![1]) % 3)  #    }
-9  +97*0    #            print chr 97 + [0]                  #    print chr(97 + a)
-9  10       #            print chr 10                        #    print '\n'
0   +1*0     #            [0] = [0] + 1                       #    a += 1
m   =+-26*0  #  label m1: jump past label m((1 + !![0]) % 2)  #  } while a != 0
\$\endgroup\$
1
\$\begingroup\$

Python 3, 74 64 63 Bytes

Saved ten bytes thanks to Caird

Saved one byte thanks to Stephen

Changed format to one-liner

Yes there are several Python 2 answers but how about one with Python 3?

for b in'abcdefghijklmnopqrstuvwxyz':print(' '*(ord(b)-97)+b)

First it makes a list of the alphabet then prints each letter but with n (Index of the value) spaces.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ 64 bytes \$\endgroup\$ Commented Feb 26, 2018 at 21:12
  • \$\begingroup\$ 41 bytes \$\endgroup\$
    – Jo King
    Commented Feb 26, 2018 at 23:33
  • \$\begingroup\$ I found there's an identical answer here \$\endgroup\$
    – Jo King
    Commented Feb 26, 2018 at 23:54
  • \$\begingroup\$ Mine is a little different \$\endgroup\$ Commented Feb 27, 2018 at 14:16
  • \$\begingroup\$ b in 'abc -> b in'abc \$\endgroup\$
    – Stephen
    Commented Feb 28, 2018 at 1:52
1
\$\begingroup\$

Japt -R, 7 bytes

;C¬ËiEç

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Java (OpenJDK 8), 58 bytes

a->{for(char b=96;b++<122;a+=' ')System.out.println(a+b);}

Try it online!

I had no idea that you could make a for loop with a char variable... but here ya go! I’m pretty proud of my solution here, it may be my most golfed submission to date!

\$\endgroup\$
1
\$\begingroup\$

Add++, 43 36 bytes

D,f,@:^,32$yVVL97+
D,g,,26RBF€{f}
$g

Try it online!

Super suboptimal because I don't know what I'm doing

\$\endgroup\$
1
\$\begingroup\$

Python 2, 56 bytes

a=map(chr,range(65,91))
for i in a:print' '*a.index(i)+i
\$\endgroup\$
1
\$\begingroup\$

CJam, 13 bytes

26{_S*\'a+N}/

Try it online!

Explanation

26{        }/  for i in [0..25]:
    S*           puts ' ' * i
   _  \'a+       puts 'a' + i
          N      puts '\n'
\$\endgroup\$
1
\$\begingroup\$

PHP, 72 68 67 Bytes

Try it online!

Try it online (after @Titus review)

Try it online, last review. @Titus Strikes Back

Code, recursive function

function f($v=26){$v&&print str_pad("",$v).chr($v+96)." 
".f($v-1);}

Explanation

function f($v=26){
    $v&&print           # if $v not zero then print
    str_pad("",$v)     # pad spaces $v times
    .chr($v+96)     # concat the character at 96+$v 
    ."                 # concat a linebreak 
    ".f($v-1);          # call the function 
}
\$\endgroup\$
3
  • \$\begingroup\$ 1) You need no braces for the echo. 2) Try $l&&print instead of if($l>0)echo. 3) And please don´t use $l as a variable name. ;) \$\endgroup\$
    – Titus
    Commented Jun 17, 2018 at 21:26
  • \$\begingroup\$ As always, thank you @Titus for your suggestions :D \$\endgroup\$ Commented Jun 19, 2018 at 15:51
  • \$\begingroup\$ You´re welcome. print needs no parentheses. \$\endgroup\$
    – Titus
    Commented Jun 21, 2018 at 7:08

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.