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
    Aug 23, 2017 at 15:16
  • 2
    \$\begingroup\$ Why the downvote? What can i improve? \$\endgroup\$
    – user70974
    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
    Aug 23, 2017 at 15:48
  • 3
    \$\begingroup\$ @pizzakingme No, you may not. \$\endgroup\$
    – user70974
    Aug 23, 2017 at 16:05
  • 2
    \$\begingroup\$ I accidentlly got an interesting pattern while golfing my answer: tio.run/##K0nO@f@/… \$\endgroup\$
    – sergiol
    Oct 19, 2017 at 19:19

73 Answers 73

1 2
3
1
\$\begingroup\$

Ruby, 43 bytes

->c{i=0
(?A..c).map{" "*(i+=1)+[_1]*i*" "}}

Attempt This Online!

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

Vyxal, 20 bytes

C65-›ɾ(nIn‹kAin*Ṅ+,)

Try it Online!

Explanation:

C65-›ɾ  # range from 1 to 0-25 depending on which letter is inputted
 (  # begin loop
  nI  # push n (current loop variable) spaces to the stack
   n‹kAin*Ṅ+  # which letter of the alphabet is n and push n letters 
    separated by spaces
     ,  # output
\$\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\$
2
  • \$\begingroup\$ i64 can be '@ \$\endgroup\$ Aug 23, 2017 at 16:42
  • \$\begingroup\$ Oh, and you can re-order your code like this to avoid leading spaces: rc'@-{_S*\)_'@+*S*N}% \$\endgroup\$ Aug 23, 2017 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\$
3
  • 1
    \$\begingroup\$ make sure your answer is consistent with the other answers by using a title and a byte count \$\endgroup\$
    – Michthan
    Aug 24, 2017 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, 2017 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\$ Aug 24, 2017 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\$
2
  • \$\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, 2017 at 9:40
  • \$\begingroup\$ Ah, sorry. I'll fix that \$\endgroup\$
    – Stan Strum
    Aug 24, 2017 at 16:08
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.