40
\$\begingroup\$

Given a letter of the English alphabet, your task is to build a semi-diagonal alphabet to the input.

How to build a Semi-Diagonal alphabet?

Brief Description: First, you take the position of the letter in the alphabet, P (P is 1-indexed here). Then, you print each letter until the input (inclusive) on a line, preceded by P-1 and repeat that letter P times, interleaving with spaces.

Examples:

  • Given F, your program should output:

    A 
     B B 
      C C C 
       D D D D 
        E E E E E 
         F F F F F F 
    
  • Given K, your program should output:

    A
     B B 
      C C C 
       D D D D 
        E E E E E 
         F F F F F F 
          G G G G G G G 
           H H H H H H H H 
            I I I I I I I I I 
             J J J J J J J J J J 
              K K K K K K K K K K K 
    
  • Given A, your program should output:

    A
    

Rules

  • You may choose either lowercase or uppercase characters, but that should be consistent.

  • You may have extraneous spaces as follows:

    • One consistent leading space (on each line).
    • A trailing or leading newline(s).
    • Trailing spaces.
  • Input and output can be taken by any standard mean, and default loopholes apply.

  • You are allowed to output a list of lines instead, as long as you also provide the version.

  • This is , so the shortest code in bytes wins!

Inspired by this challenge.

\$\endgroup\$
10
  • \$\begingroup\$ Is output as list of strings ok? \$\endgroup\$
    – Adám
    Commented Aug 23, 2017 at 15:16
  • 2
    \$\begingroup\$ Why the downvote? What can i improve? \$\endgroup\$
    – user70974
    Commented Aug 23, 2017 at 15:45
  • 1
    \$\begingroup\$ When you say "P is 1-indexed here", does here refer to the challenge or the example? \$\endgroup\$
    – Dave
    Commented Aug 23, 2017 at 15:48
  • 3
    \$\begingroup\$ @pizzakingme No, you may not. \$\endgroup\$
    – user70974
    Commented Aug 23, 2017 at 16:05
  • 2
    \$\begingroup\$ I accidentlly got an interesting pattern while golfing my answer: tio.run/##K0nO@f@/… \$\endgroup\$
    – sergiol
    Commented Oct 19, 2017 at 19:19

73 Answers 73

1
\$\begingroup\$

brainfuck, 111 bytes

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

Try it online!

Surprising how the Turing Tarpit itself can out-golf some other languages.

The breakdown:

Section A

>+[+[<]>>+<+]                                                       create character 'A'
             ,                                                      take input
              >>++++++++++                                          create '\n' character
                          >++++[>++++++++<-]                        create ' ' character
                                            ----[<<<->>>----]<<<-   map input to (1->26)

Section B

[<[-<+>>>>>.<<<<]+                                 print N spaces, then increment N
                  <[->+>>.>>.<<<<<]                print (N-1) of the Nth letter and spaces
                                   >>>.+>.<<-]     print additional letter, and newline
\$\endgroup\$
1
\$\begingroup\$

><>, 85 Bytes

i1-&0\1o*84\      \o*84o:$\
@)?;&>:> :?/~1+:88*+$:@>:?/~ao&:
     /-/          /$1- /

Try it online

\$\endgroup\$
1
  • \$\begingroup\$ This is almost a four-finned fish... coincidence? \$\endgroup\$
    – Neil
    Commented Aug 25, 2017 at 13:45
1
\$\begingroup\$

Brain-Flak, 179 bytes

({}[((((()()()()){}){}){}){}]){((({}[()])())){({}<(({})<>((((()()()()){}){}){}){
})((((()()()()){}){}){})<>>[()])}{}{({}[()])<>((((()()()()){}){}){})<>}{}<>{}(((
)()()()()){})<>}<>

This is 178 bytes of code, and +1 for the -c flag.

Try it online!

(Semi-)Readable version:

({}[((((()()()()){}){}){}){}])

{

    ((({}[()])()))

    {
        ({}<

            (({})<>((((()()()()){}){}){}){})
            ((((()()()()){}){}){})<>

        >[()])
    }{}

    {
        ({}[()])
        <>
        ((((()()()()){}){}){})
        <>
    }{}

    <>{}

    ((()()()()()){})

    <>

}

Fun fact: This will work with arbitrary characters above Z! Try it online!

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

Java (OpenJDK 8), 81 bytes

c->{String s="";for(int d=64;d++<c;)System.out.printf((s=" "+s+"%1$c ")+"%n",d);}

Try it online!

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

Bash, 93 90 bytes

for c in {A..Z}
{
printf "%$[i++*3+2]s\n" "$(echo `yes $c|head -$i`)"
[ $c = $1 ]&&break
}

Try it online!

Takes input as, and outputs as, uppercase letters.

Still getting the hang of golfing in Bash, so I'm open to tips.

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

C (GCC), 115 109 Bytes

f(a,n,k,s,i){if(k){for(;i<s;i++)printf(" ");for(i=0;i<s+1;i++)printf("%c ",a);puts();f(++a,++n,--k,++s,0);}}

Usage

f(65,5,5,0,0)

Where the two 5's are the given K value, the rest are constants used later for recursive calls.

Ungolfed

f(a,n,k,s,i){
    if(k){
        for (; i<s; i++) printf(" ");
        for (i=0; i<s+1; i++) printf("%c ",a);
        puts();
        f(++a,++n,--k,++s,0);
    }
}

Output

enter image description here

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

C (gcc), 77 bytes

i,j;f(c){for(j=64;i||j++<c&&printf("\n%*c ",i=j-64,j);--i&&printf("%c ",j));}

Try it online!

Contains unsequenced access to possibly incremented j, so very likely to break with a different compiler :)

fixed with identical byte count

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

Alice, 31 bytes

/A''*?h~w0O~
\I"AxrS&..r!y"kx@/

Try it online!

Explanation

Linearized and with relevant spaces included, the program is as follows:

I'A*rh&w.O!" x A"'x?S~.0r~yk@

I                                   take input
 'A*                                append the letter A
    r                               expand to range (e.g., if input was D, string is now DCBA)
     h&w                            split string into characters, and repeat the main loop that number of times
        .O                          output copy of top string on stack with newline
          !                         move top string (last printed row) to tape
           " x A"                   push " x A"
                 'x S               replace the occurrence of "x" with
                   ?                  the string on the tape
                     ~              swap to get next letter
                      .             copy this letter
                       0r           get entire range from that letter down to "0"
                         ~          swap again to put letter on top of stack
                          y         replace all characters in this range (effectively, all non-space characters) with the next letter
                           k        repeat (end of main loop)
                            @       exit
\$\endgroup\$
1
\$\begingroup\$

Pyth, 18 17 bytes

-1 thanks to @TheIOSCoder because you are apparently allowed to surround the input in quotes

j.b+*dYjd*NhYGhxG

Try it online!

Explanation

j.b+*dYjd*NhYGhxG    accepts a token as input, must be lowercase and in quotes

j                    joins on new line
 .b   Y   N Y        maps A (lambdas NY) over B and C in parallel
   +                 joins two strings on same line
    *    *           repeats A B times
     d  d            " " or space
       j             joins B on A
           h  h      A + 1
             G  G    lowercase alphabet
               x     returns position of B in A
                     implicit input
\$\endgroup\$
1
  • \$\begingroup\$ You can use the input with quotes around to save 1 byte through implicit input (Q) - 17 bytes \$\endgroup\$
    – user70974
    Commented Aug 25, 2017 at 12:07
1
\$\begingroup\$

PHP, 69+1 bytes

A<?for($c=A;$c<$argn;)echo($p=str_pad)($p("
",++$i+1),$i*3,++$c." ");

Run as pipe with -nF or try it online.


The Z case cost 4 bytes. for($c=A;$c<=$argn;)echo($p=str_pad)($p(" ",++$i),$i*3,$c++." "); (with -nR) works for A to Y.

Printing a list of underscore-separated lines would save two bytes, but that´d feel like cheating.

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

Very naïve approach.

Tcl, 100 bytes

time {puts [format %[incr i]s \ ][string repe [format %c\  [expr $i+64]] $i]} [expr [scan $c %c]-64]

Try it online!

Tcl, 106 bytes

set i 0;while \$i<[scan $c %c]-64 {puts [format %$i.s \ ][string repe [format %2c [expr $i+65]] [incr i]]}

Try it online!


Below approahes do not have a leading space

Tcl, 107 bytes

set i 0;while \$i<[scan $c %c]-64 {puts [format %$i.s \ ][string repe [format %c [expr $i+65]]\  [incr i]]}

Try it online!

Tcl, 109 bytes

set i 0;time {puts [format %$i.s \ ][string repe [format %c [expr $i+65]]\  [incr i]]} [expr [scan $c %c]-64]

Try it online!

Tcl, 111 bytes

set i 0;time {puts [string repe \  $i][string repe [format %c [expr $i+65]]\  [incr i]]} [expr [scan $c %c]-64]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Failed outgolf on tio.run/… \$\endgroup\$
    – sergiol
    Commented Oct 29, 2017 at 14:02
1
\$\begingroup\$

Recursiva, 28 bytes

{B-Oa64"P+*' '~}J' '*}C+64}"

Try it online!

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

K (oK), 35 bytes

Solution:

`c${,/(x#32),(x+1)#,65+x,-33}'!-64+

Returns list of strings. prepend `0: to write to stdout instead.

Try it online!

Example:

`c${,/(x#32),(x+1)#,65+x,-33}'!-64+"F"
("A "
 " B B "
 "  C C C "
 "   D D D D "
 "    E E E E E "
 "     F F F F F F ")

Explanation:

Struggled to golf this down any further... K is interpretted right-to-left:

`c${,/(x#32),(x+1)#,65+x,-33}'!-64+ / the solution
                               -64+ / subtract 64 from input, "F"=>6 (implicit ascii)
                              !     / til, !6 => 0 1 2 3 4 5
   {                        }'      / lambda function with each input
                         -33        / negative 33
                       x,           / x concatenated, e.g. 0 -33
                    65+             / add 65, 0 -33 => 65 32 (aka "A ")
                   ,                / enlist, wraps "A " into a list ("A ")
             (x+1)#                 / take (#) x+1 instances of this list
            ,                       / concatenate with
      (x#32)                        / x instances of the number 32 (aka " ")
    ,/                              / raze, flattens this list down
`c$                                 / cast to characters
\$\endgroup\$
1
\$\begingroup\$

SOGL V0.12, 15 14 12 bytes

ZZ,Wm{ē@Ο}¹¾

Try it Here!

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

C (gcc), 76 bytes

i,j;f(c){for(i=0;i++<c-64;puts(""))for(j=0;j<i;)printf("%*c",j++?2:i,i+64);}

Try it online!

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

Ruby, 62 Bytes

Assumes input as a string (e.g., 'A'). Wasn't clear from the rules whether this was allowed. Will update if not.

Lots of room for improvement, I'm sure.

->n{a=[*'A'..n];a.map{|x|' '*a.index(x)+(x+' ')*-~a.index(x)}}
\$\endgroup\$
1
  • \$\begingroup\$ BTW This is definitely a valid way to take input. +1 \$\endgroup\$
    – hyperneutrino
    Commented Nov 10, 2017 at 17:02
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 115 bytes

	U =&UCASE
	U INPUT @L
N	U POS(X) LEN(1) . R
	OUTPUT =DUPL(' ',X) DUPL(R ' ',X + 1)
	X =LT(X,L - 1) X + 1	:S(N)
END

Try it online!

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

Swift, 133 bytes

func f(c:String){for i in 9..<Int(c,radix:36)!{var s=""
for _ in 9..<i{s+=" "}
for _ in 9...i{s+=String(i+1,radix:36)+" "}
print(s)}}

Try it online!

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

Julia 0.4, 48 bytes

It should work the same in more recent versions of Julia as well.

x->[" "^i*"$c "^i for (i,c) in enumerate('A':x)]

Try it online!

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

Excel VBA, 78 71 Bytes

Anonymous VBE immediate window function that takes input from range [A1] and outputs to the VBE immediate window

For i=1To[Code(A1)-64]:?:?Spc(i-1);:For j=1To i:?Chr(i+64)" ";:Next j,i

-1 Byte for switching to leading newline over trailing, and condensing Next statements

-6 bytes for using Spc() over String()

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

Julia 0.6, 40 bytes

c->[" "^i*"$('A'+i) "^-~i for i=0:c-'A']

Try it online!

Returns an array of strings containing the output. (Pretty printed on TIO using just map (println, f(c)).)

^ applied to strings repeats the string that many times. $() executes its content and pastes the result into the string (here, the correct letter for the line). Finally, the space repetition and the letter repetition are concatenated with *.

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

Burlesque - 39 bytes

-]'Ajr@id?i?*m{' []}zim{p^' j?*j_+}unsh

This is not fully golfed yet and (trivially at least the sh can be replaced with Q) but it's a first start. m{}un can probably be replaced by a version of map that unlines and zim[ is a good candidate for a shortcut in the near future. The two maps can also probably be merged together into a single map.

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

Kotlin, 57 bytes

{('A'..it).map{" ".repeat(it-'A')+"$it ".repeat(it-'@')}}

Try it online!

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

Common Lisp, 89 bytes

(dotimes(m(-(char-code(read))63))(format t"~va~v@{~c ~:*~}~%"m #\  m(code-char(+ m 64))))

Try it online!

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

SmileBASIC, 58 57 bytes

INPUT E$WHILE L$<E$L=L+1L$=CHR$(L+#X)+" "?" "*L;L$*L
WEND
\$\endgroup\$
1
\$\begingroup\$

Excel

=LET(x,SEQUENCE(CODE(A1)-64),REPT(" ",x-1)&REPT(CHAR(64+x),x))

Link to Spreadsheet

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

Pip, 22 21 bytes

FxzIxLEaPsXi.YxX++iJs

Uses lowercase and takes input from the command line. Try it online!

Explanation

Implicit: a is 1st cmdline arg; z is lowercase alphabet; i is 0; s is space.

Fxz                      For each letter x in lowercase alphabet:
   IxLEa                  If x is less than or equal to a:
        P                  Print
         sXi               i spaces
            .Y             to which concatenate (the Y enforces precedence)
              xX++i        x repeated i times, after incrementing i
                   Js      joined on spaces
\$\endgroup\$
1
\$\begingroup\$

Vim, 45 bytes

D:h<_
jjYZZPJ/<C-r>-
lDqqha
<esc>:,$s/\S/ & &
{$@qq@q

Uses lowercase. Try it online!

Explanation

D

Delete the input letter (we'll use it later).

:h<_<cr>jjYZZ

Yank the lowercase alphabet from an article in the help file (see this tip).

PJ

Paste the alphabet and turn the blank line (where the input used to be) into a trailing space.

/<C-r>-<cr>

Find the input letter (pasting it into the find command from the "small deletion" register -).

lD

Move one character to the right and delete everything till the end of the line. We now have one line containing letters a through the input letter, with the cursor on the last character of the line.

qqh

Begin recording macro q. Try going left one character. If this fails, we've gotten all the way to a and we're done; the macro exits. Otherwise, the cursor is now on the second-last character of the top line.

a<cr><esc>

Insert a newline after the cursor, moving the final character of the top line to its own line.

:,$s/\S/ & &<cr>

On every line from this second line till the end of the file, substitute the first non-space character for space, itself, space, and itself again. This turns a line like c into c c, d d into d d d, etc.

{$@q

Go back to the first line, then go to the end of the line and run the macro recursively.

q@q

Stop recording, and call the macro.

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

Vyxal rMj, 16 13 bytes

Thanks to @Lyxal for -3 bytes by using Map Lambda instead of a for loop.

kAḟƛ›nkAi*Ṅn꘍

Try it Online!

Explanation:

               # Implicit input
kA             # Constant 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  ḟ            # Index of input in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ’
   ƛ           # Map over range [0,index)
     nkAi      # Index of n in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ›    *     # Multiply by n+1
          Ṅ    # Join with spaces
           n꘍  # Prepend n spaces
               # 'j' flag - Join top of stack with newlines and print
\$\endgroup\$
1
1
\$\begingroup\$

Python 2, 101 99 bytes

-2 because I forgot I could remove the parentheses in print

def f(l,a="abcdefghijklmnopqrstuvwxyz",s=0):
 for c in a[:a.index(l)+1]:print" "*s+(c+" ")*-~s;s+=1

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.