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

75 Answers 75

1 2
3
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\$
1
\$\begingroup\$

Very naïve approach.

Tcl, 94 bytes

time {puts [format %[incr i]s ""][lrepeat $i [format %c [expr $i+64]]]} [expr [scan $c %c]-64] 

Try it online!


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!][TIO-j6r5o6wd] Tcl: http://tcl.tk/ [TIO-j6r5o6wd]: https://tio.run/##RcoxDsIwDAXQq/whnRCIAbrASXA8VMYUSyVEsRFIiLMHNub3QpY@azg8zlYg3TVg2B6eV1sUOdmRXKaCJBiE1@MO7/r4fbrc220KDMk2jgwmj2ZlRtOqfxWQvmpDstW4Z84AWZEGY/70fvoC "Tcl – 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\$ Commented Oct 29, 2017 at 14:02
1
\$\begingroup\$

Raku (Perl 6) (rakudo), 42 bytes

{say " "x($/=.ord-64)~"$_ "x$/for 'A'..@_} 

Attempt This Online!

{say # craft and print string: " "x # multiply number of spaces ($/= # (set variable $/) .ord-64) # by ascii code -64 ~ # append "$_ " # current letter and a space x$/ # times $/ from above. for 'A'..@_} # loop from A to ending letter 
\$\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\$ Commented 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\$ Commented 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\$

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\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Aug 24, 2017 at 9:40
  • \$\begingroup\$ Ah, sorry. I'll fix that \$\endgroup\$ Commented Aug 24, 2017 at 16:08
0
\$\begingroup\$

AWK, 85 bytes

@load"ordchr";{for(i=64;i++<ord($1);printf"\n"(z=" "z))for(j=64;j++<i;)printf"%c ",i} 

Attempt This Online!

@load"ordchr"; # lib for ascii {for(i=64; # alpha starts at 65 i++<ord($1); # while less than input printf"\n" # print a return (z=" "z)) # along with a concatenating string of spaces for(j=64; # ascii alpha j++<i;) # number of chars equal to it's place in alpha printf"%c ",i} # print char and a space 
\$\endgroup\$
0
\$\begingroup\$

TXR Lisp, 77 59 56 bytes

(op map(op pprinl`@{""@2}@{(repeat @1 @3)}`)"A"..@1 0 1) 

Run:

This is the TXR Lisp interactive listener of TXR 299. Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet. TXR causes vomiting if accidentally ingested; no need to induce such. 1> (op map(op pprinl`@{""@2}@{(repeat @1 @3)}`)"A"..@1 0 1) #<interpreted fun: lambda (#:arg-1-0022 . #:arg-rest-0021)> 2> [*1"F"] A B B C C C D D D D E E E E E F F F F F F ("A" " B B" " C C C" " D D D D" " E E E E E" " F F F F F F") 

The list is the result value, not part of the output.

\$\endgroup\$
1 2
3