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

67 Answers 67

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\$
  • \$\begingroup\$ This is almost a four-finned fish... coincidence? \$\endgroup\$ – Neil Aug 25 '17 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\$

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\$
  • \$\begingroup\$ Failed outgolf on tio.run/… \$\endgroup\$ – sergiol Oct 29 '17 at 14:02
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\$
  • \$\begingroup\$ BTW This is definitely a valid way to take input. +1 \$\endgroup\$ – HyperNeutrino Nov 10 '17 at 17:02
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\$

Kotlin, 57 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Röda, 53 bytes

f L{seq 0,ord(L)-65|[" "*_..`${chr(_1+65)} `*(_1+1)]}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Charcoal, 19 bytes

F⁻℅S⁶³«×⁺℅⁺ι⁶⁴ ιJιι

Try it online! Link is to verbose version.

A bit of a different approach from the other answer, let's see if it's golfable...

\$\endgroup\$
0
\$\begingroup\$

Proton, 47 bytes

c=>[' '*i+-~i*('%c '%(i+65))for i:0..ord(c)-64]

Not unlike the Python answers.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 39 bytes

[:(#\([' '&,1j1##)"0])i.@>:&.(65-~3&u:)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

CJam, 22 21 20 bytes

This is the "list of lines" version. Just add an "N" before the close bracket to make it look nice. (Thanks to @Erik)

rc'@-{)_S*\_'@+*S*}%

Try it Online (nice version).

\$\endgroup\$
  • \$\begingroup\$ i64 can be '@ \$\endgroup\$ – Erik the Outgolfer Aug 23 '17 at 16:42
  • \$\begingroup\$ Oh, and you can re-order your code like this to avoid leading spaces: rc'@-{_S*\)_'@+*S*N}% \$\endgroup\$ – Erik the Outgolfer Aug 23 '17 at 16:49
0
\$\begingroup\$

Jelly, 19 bytes

ḢØAḣi¥p⁶ẋ"J$µLḶ⁶ẋ;"

Try it online!

Function that returns a list. Append ⁸Y to print in separate lines. Erase the footer to show actual output instead of string representation.

\$\endgroup\$
0
\$\begingroup\$

TXR Lisp, 77 bytes

(do mapdo(op pprinl`@{""@2}@{(take(inc @2)(gun @1))}`)(range #\A@1)(range 0))

Run:

This is the TXR Lisp interactive listener of TXR 184.
Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet.
1> (do mapdo(op pprinl`@{""@2}@{(take(inc @2)(gun @1))}`)(range #\A@1)(range 0))
#<interpreted fun: lambda (#:arg-01-0173 . #:rest-0172)>
2> [*1 #\F]
A
 B B
  C C C
   D D D D
    E E E E E
     F F F F F F
\$\endgroup\$
0
\$\begingroup\$

C++ (gcc), 179 bytes

Well, someone ought to post the most un-original method, I might as well be that someone.

#include <iostream>
using namespace std;
int main(){char n;cin>>n;for(char i='A';i<=n;i++){for(char k='A';k<i;k++){cout<<" ";}for(int j=0;j<=i-'A';j++){cout<<i<<" ";}cout<<endl;}}

An easier-to-read version of the code:

#include <iostream>
using namespace std;
int main()
{
    char n;
    cin>>n;
    for (char i='A';i<=n;i++)
    {
        for (int k=0;k<n-i;k++)
        {
            cout<<" ";
        }
        for (int j=0; j<n-'A'+1;j++)
        {
            cout<<i<<" ";
        }
        cout<<endl;
    }
}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java, 363 bytes

public class MyClass {
    public static void main(String args[]) {
        char x='A';
        int count=0;
      for(x='A';x<='Z';x++){
        for(int x1=count;x1>0;x1--) {
            System.out.print(" ");
        }
        for(int y=count;y>=1;y--) {
            System.out.print(x);
        }
        System.out.print(x+"\n");
       count++;
    }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ make sure your answer is consistent with the other answers by using a title and a byte count \$\endgroup\$ – Michthan Aug 24 '17 at 11:10
  • \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! Notice that you can remove a lot of the whitespace to shorten your code. Also, your code should print a space between the letters on each line. Changing System.out.print(x); to System.out.print(x+" "); will do the trick. \$\endgroup\$ – Steadybox Aug 24 '17 at 11:20
  • \$\begingroup\$ How do we enter any parameter? Like that limits where we stop? This is present in the question, I don't see it in this answer. \$\endgroup\$ – Olivier Grégoire Aug 24 '17 at 12:59
0
\$\begingroup\$

Shnap, 97 96 bytes

-1 byte because I remembered that the parser reads strings through newlines, so a literal newline is shorter than \n

$c{n=65s=""for k:range(n,c+1){for range(n,k)s+=" "for range(n,k+1)s+=char(k)+" "s+="
"}return:s}

This is an anonymous function (actually, all functions in Shnap are anonymous...).

Try it online!

Alternative version that uses the fact that blocks return the last instruction's value

Ungolfed/explanation:

$ (c) {                  //Function with 1 parameter
    n=65                    //Value of 'A'
    s=""                    //The result
    for k:range(n,c+1) {    //Inclusive range from n to c, range(n,c,1,1) also works, as third arg is step and fourth is inclusive (bool).
                            //For loop variable is k
        for range(n,k)      //Exclusive range from n to k
            s+=" "          //Add a space
        for range(n,k+1)    //Inclusive range from n to k
            s+=char(k)+" "  //Add k and a space
        s+="\n"             //Add a newline

    }
    return:s                //Return the result
}

I really should implement string multiplication and eval...

\$\endgroup\$
0
\$\begingroup\$

Pyth, 26 bytes

Should meet OPs specs.

VhxrG1w++*dNr@GN1*+dr@GN1N

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I am afraid you misunderstood. You must have exactly one space between the characters, not many. See my answer for clarifications. \$\endgroup\$ – Mr. Xcoder Aug 24 '17 at 9:40
  • \$\begingroup\$ Ah, sorry. I'll fix that \$\endgroup\$ – Stan Strum Aug 24 '17 at 16:08
0
\$\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\$
  • \$\begingroup\$ You can use the input with quotes around to save 1 byte through implicit input (Q) - 17 bytes \$\endgroup\$ – user70974 Aug 25 '17 at 12:07
0
\$\begingroup\$

Recursiva, 28 bytes

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

Try it online!

\$\endgroup\$
0
\$\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\$
0
\$\begingroup\$

SOGL V0.12, 15 14 12 bytes

ZZ,Wm{ē@Ο}¹¾

Try it Here!

\$\endgroup\$
0
\$\begingroup\$

Pip, 22 bytes

Wz@iLEaPsXi.Yz@iX++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.

W                        While
 z@i                     i'th character in lowercase alphabet
    LEa                  is less than or equal to a:
       P                  Print
        sXi               i spaces
           .(         )   to which concatenate
             z@i          i'th character in lowercase alphabet
                X++i      repeated i times, after incrementing i
                    Js    joined on spaces
\$\endgroup\$

Your Answer

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