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

10
\$\begingroup\$

Python 3, 59 bytes

lambda l:[' '*i+'%c '%(i+65)*-~i for i in range(ord(l)-64)]

Try it online!

Python 3, 61 bytes

lambda l:[' '*i+-~i*(chr(i+65)+' ')for i in range(ord(l)-64)]

Try it online! (link to pretty-print version)

\$\endgroup\$
  • 8
    \$\begingroup\$ I see absolutely no reason for a downvote. Can the @downvoter explain? \$\endgroup\$ – Mr. Xcoder Aug 23 '17 at 14:59
  • 1
    \$\begingroup\$ I'd imagine it's just a misclick, or perhaps someone not liking a lack of explanation (the latter is quite unlikely IMO) \$\endgroup\$ – Conor O'Brien Aug 23 '17 at 19:14
  • \$\begingroup\$ I dislike Python and I can't implement with Python, so the answer is not useful for me? Just kidding, but the button tooltips probably do not fit the rules of this site \$\endgroup\$ – Thomas Weller Aug 25 '17 at 12:20
  • \$\begingroup\$ Is it just me or does it say Mr. Xcoder has 1 rep...? \$\endgroup\$ – Stan Strum Sep 25 '17 at 2:34
9
\$\begingroup\$

Python 2, 63 61 59 bytes

-2 bytes thanks to Rod. -2 bytes thanks to Felipe Nardi Batista.

i=1
exec"print' '*i+'%c '%(i+64)*i;i+=1;"*(ord(input())-64)

Try it online!

\$\endgroup\$
8
\$\begingroup\$

C, 89 bytes

i,j;f(l){for(i=64;i++<l&&printf("%*c ",i-64,i);puts(""))for(j=i-65;j--;)printf("%c ",i);}

Try it online!

\$\endgroup\$
7
\$\begingroup\$

PowerShell, 45 42 bytes

65..$args[0]|%{" "*$i+++"$([char]$_) "*$i}

Try it online!

Takes input as a literal char, then loops up through the capitals to that point, each iteration prepending the appropriate number of spaces and then the char\space hybrid.

Saved 3 bytes thanks to TessellatingHeckler.

\$\endgroup\$
  • \$\begingroup\$ @TessellatingHeckler Indeed. I've been golfing that to "$args" so much, which wouldn't work here, I forgot about the [0] method. Haha. \$\endgroup\$ – AdmBorkBork Aug 25 '17 at 12:22
5
\$\begingroup\$

JavaScript (ES6), 85 bytes

Works in lower case for both input and output. Outputs a leading space and a trailing space on each line.

f=(c,k=10,C=k.toString(36),r=s=>`${s} `.repeat(k-9))=>r``+r(C)+(C==c?'':`
`+f(c,k+1))

Demo

f=(c,k=10,C=k.toString(36),r=s=>`${s} `.repeat(k-9))=>r``+r(C)+(C==c?'':`
`+f(c,k+1))

O.innerText = f('m')
<pre id=O>

\$\endgroup\$
  • \$\begingroup\$ `${s} ` can be replaced by (s+"") for one byte saving \$\endgroup\$ – Luke Aug 24 '17 at 7:07
  • \$\begingroup\$ @Luke I need this space. It can be replaced by (s+" "), but that's just as long. \$\endgroup\$ – Arnauld Aug 24 '17 at 9:18
5
\$\begingroup\$

APL (Dyalog), 26 bytes

Prompts for scalar character. Prints list of lines.

(∊⍴∘'',' ',¨⍨⊢⍴⊃∘⎕A)¨⍳⎕A⍳⎕

Try it online! (has ASCII art version at one additional byte)

 prompt for input

⎕A⍳ find ɩndex in Alphabet

 first that many ɩntegers

( apply the following tacit function to each :

⊃∘⎕A pick the argument'th letter letter from the Alphabet

⊢⍴ cyclically reshape it to the argument length

' ',¨⍨ append a space to each

⍴∘'', prepend a string of argument length (padded with spaces)

ϵnlist (flatten)


The ASCII art version just has a on the very left; mix list of strings into table of characters.

\$\endgroup\$
4
\$\begingroup\$

Perl 5, 31 bytes

30 bytes code + 1 for -l.

print$"x$-,"$_ "x++$-for A..<>

Try it online!

\$\endgroup\$
  • \$\begingroup\$ You can cut this down by using say instead of the -l flag: Try it online! \$\endgroup\$ – Xcali Aug 23 '17 at 21:03
  • \$\begingroup\$ @Xcali I'm torn on -E/-M5.01, I've used say considerably in the past, and would probably abuse the fact that say is an alternative to print in a restricted source challenge or similar perhaps, but for the sake of -3, I'll keep as-is for now. See this meta post for a fair argument. Appreciate the input though! \$\endgroup\$ – Dom Hastings Aug 24 '17 at 5:22
3
\$\begingroup\$

Dyalog APL, 38 bytes

{↑{(y/' '),(2×y←⎕A⍳⍵)⍴⍵,' '}¨⎕A↑⍨⎕A⍳⍵}

Try it online!

How?

⎕A↑⍨ - take the alphabet until

⎕A⍳⍵ - the input character

¨ - for each char

    ⍵,' ' - take the char and a space

    (...)⍴ - reshape to

    2×y←⎕A⍳⍵ - twice the index of the char in the alphabet

    (y/' ') - and prepend index-of-char spaces

- then flatten

\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog Classic), 26 bytes

{↑{(≠\⍵<⍳3×⍵)\⍵⊃⎕A}¨⍳⎕A⍳⍵}

Try it online!

Explanation

                      ⍳⎕A⍳⍵  generate indexes up to position of right arg ⍵
{                   }¨       on each index apply function
   (≠\⍵<⍳3×⍵)                generate boolean mask for expansion (each line has a length 3 times its index ⍵, starting with ⍵ blanks and then alternating letter blank)
             \⍵⊃⎕A          expand character in position ⍵
 ↑                            mix result into text matrix
\$\endgroup\$
  • \$\begingroup\$ Goodness... 4 APL-er solving the same problem at the same time! :) I think in codegolf you're allowed to remove the outer {}, replace with , and claim it's a "complete program" rather than a function. That would make your solution the best (so far). \$\endgroup\$ – ngn Aug 24 '17 at 6:40
  • \$\begingroup\$ Must be a good sign :) Thanks for the suggestion. I've seen it done but wasn't sure where to draw the line. I guess that I can save 3 bytes if I remove curlies and mix. \$\endgroup\$ – Gil Aug 24 '17 at 10:33
3
\$\begingroup\$

V, 28, 26, 25, 23 bytes (Competing)

¬A[/a
lDÓ./& ò
ò-Ûä$Û>

Try it online!

Note that although I have been planning on adding certain features for a long time, this challenge was what convinced me to finally do it.

The output contains one leading space on each line and one trailing newline.

Hexdump:

00000000: ac41 5b2f 1261 0a6c 44d3 2e2f 2620 f20a  .A[/.a.lD../& ..
00000010: f22d dbe4 24db 3e                        .-..$.>

Explanation:

¬A[         " Insert 'ABCDEFGHIJKLMNOPQRSTUVWXYZ['
   /        " Search for...
    <C-r>a  "   The input
l           " Move one character to the right
 D          " And delete every character after the cursor
  Ó         " Search for...
   .        "   Any character
    /       " And replace it with...
     & ò    "   That character followed by a space and a newline
ò           " Recursively...
 -          "   Move to the beginning of the next line up
  Ûä$       "   Make *line number* copies of the current line
     Û>     "   And indent this line by *line number* spaces
\$\endgroup\$
  • 1
    \$\begingroup\$ This is competing. You may remove the title remark. \$\endgroup\$ – user70974 Aug 23 '17 at 15:32
  • \$\begingroup\$ It's useful for those who weren't aware of the new meta, such as myself \$\endgroup\$ – Conor O'Brien Aug 23 '17 at 22:05
3
\$\begingroup\$

05AB1E, 10 bytes

A¹¡н«ðâƶāú

Try it online!

-2 thanks to Adnan.

Append » to make it print in separate lines.

\$\endgroup\$
  • \$\begingroup\$ You could omit the < as one consistent leading space is okay. \$\endgroup\$ – Emigna Aug 23 '17 at 14:53
  • \$\begingroup\$ @Emigna Thanks! \$\endgroup\$ – Erik the Outgolfer Aug 23 '17 at 14:54
  • \$\begingroup\$ A¹¡н«ðâƶāú should work for 10 bytes \$\endgroup\$ – Adnan Aug 23 '17 at 18:56
  • \$\begingroup\$ @Adnan I think that ¹¡ will make it not work...oh so that's why there's a « in there. :p \$\endgroup\$ – Erik the Outgolfer Aug 24 '17 at 9:03
3
\$\begingroup\$

R, 94 88 bytes

-6 bytes thanks to Giuseppe

function(x,l=LETTERS)for(i in 1:match(x,l))cat(rep(' ',i-1),rep(paste(l[i],' '),i),'\n')}

Ungolfed:

f=function(x,l=letters){
  for(i in 1:which(l==x)){
    A=paste(l[i],' ')
    cat(rep(' ',i-1),rep(A,i),'\n')
  }
}
\$\endgroup\$
  • \$\begingroup\$ 88 bytes : returning an anonymous function is fine, you can get rid of the braces since f is a one-liner, and using match instead of which saves a byte. \$\endgroup\$ – Giuseppe Aug 24 '17 at 14:39
  • \$\begingroup\$ 71 bytes \$\endgroup\$ – Giuseppe Nov 10 '17 at 13:27
  • \$\begingroup\$ 68 bytes taking input from stdin. \$\endgroup\$ – Giuseppe Nov 24 '18 at 14:35
3
\$\begingroup\$

Haskell, 52 44 bytes

f k=[[" ",s:" "]>>=(['A'..s]>>)|s<-['A'..k]]

Returns a list of lines.

Try it online!

f k=                  -- main function is f, input parameter k
  [   |s<-['A'..k]]   -- for each s from ['A'..k]
     >>=              -- map (and collect the results in a single string) the function: 
         (['A'..s]>>) --  replace each element in ['A'..s] with
    [  ,  ]           --  over the list, containing
     " "              --   a single space to get the indent
        s:" "         --   s followed by space to get the letter sequence

Edit: @jferard: saved three bytes. Thanks!

\$\endgroup\$
  • \$\begingroup\$ 49 bytes:f k=[tail$[" ",s:" "]>>=(['A'..s]>>)|s<-['A'..k]] \$\endgroup\$ – jferard Aug 25 '17 at 16:50
  • \$\begingroup\$ @jferard: Thanks a lot. Rereading the challenge I noticed that a leading space per line is allowed, so we don't need the tail$. \$\endgroup\$ – nimi Aug 25 '17 at 18:08
2
\$\begingroup\$

JavaScript (ES8), 92 bytes

c=>(g=n=>n>9?[...g(n-1),`${n.toString(36)} `.repeat(n-=9).padStart(n*3)]:[])(parseInt(c,36))

Uses lowercase letters. Lines have one leading and one trailing space. Returns an array of lines.

Test Snippet

let f=

c=>(g=n=>n>9?[...g(n-1),`${n.toString(36)} `.repeat(n-=9).padStart(n*3)]:[])(parseInt(c,36))

;O.innerText=f("k").join`\n`
<pre id=O></pre>

\$\endgroup\$
2
\$\begingroup\$

Husk, 13 bytes

z+ḣ∞øzRNC1…'A

Takes a character in single quotes as command line argument, prints result to STDOUT. Try it online!

Explanation

I'm exploiting the way Husk prints lists of lists of strings: join inner lists with spaces and outer lists with newlines.

z+ḣ∞øzRNC1…'A  Implicit input, say 'C'
          …'A  Range from A: "ABC"
        C1     Cut into strings of length 1: ["A","B","C"]
     z N       Zip with positive integers
      R        using repetition: x = [["A"],["B","B"],["C","C","C"]]
   ∞ø          The empty string repeated infinitely: ["","","",...
  ḣ            Prefixes: [[],[""],["",""],["","",""],...
z+             Zip with x using concatenation: [["A"],["","B","B"],["","","C","C","C"]]
               Implicitly join each inner list with spaces, join the resulting strings with newlines and print.
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 15 14 13 bytes

Saved 1 byte thanks to Adnan

A¹¡н«ƶ€S»¶¡āú»

Try it online! or the Ascii art version

Explanation

A                # push lowercase alphabet
 ¹¡              # split at input
   н             # get the first part
    «            # append the input
     ƶ           # repeat each a number of times corresponding to its 1-based index
      €S         # split each to a list of chars
        »        # join on spaces and newlines
         ¶¡      # split on newlines
           āú    # prepend spaces to each corresponding to its 1-based index
             »   # join on newlines
\$\endgroup\$
  • \$\begingroup\$ It looks like we handled it a bit differently :D \$\endgroup\$ – Erik the Outgolfer Aug 23 '17 at 14:52
  • \$\begingroup\$ @EriktheOutgolfer: We did it quite similarly, but your very nice idea to append a space before lifting, removing the need for the join made yours shorter. I hadn't read that leading/trailing spaces nor output as list was okay, so that'll hopefully teach me to read the whole challenge before implementing :P \$\endgroup\$ – Emigna Aug 23 '17 at 15:01
  • \$\begingroup\$ tl;dr: vectorization :p \$\endgroup\$ – Erik the Outgolfer Aug 23 '17 at 15:05
  • \$\begingroup\$ A¹¡н« instead of ADIk>£ should work \$\endgroup\$ – Adnan Aug 23 '17 at 18:54
  • \$\begingroup\$ @Adnan: Thanks! I did have A¹¡н but didn't consider « to get the last letter so it wasn't good enough :P \$\endgroup\$ – Emigna Aug 23 '17 at 20:02
2
\$\begingroup\$

APL (Dyalog Unicode), 22 bytesSBCS

⍕⍪⊢∘⊂\2,.↑⍉⍴⍨⌸⎕a↑⍨⎕a⍳⍞

Try it online!

Uses ⎕io←1. Prints a leading space, which is allowed.

\$\endgroup\$
2
\$\begingroup\$

QBasic, 79 74 72 bytes

Thanks to Taylor Scott for byte savings (twice!)

FOR i=1TO ASC(INPUT$(1))-64
?TAB(i)
FOR j=1TO i
?CHR$(64+i)" ";
NEXT j,i

Uses uppercase. The input is by keypress and is not echoed to the screen.

Explanation

We loop i from 1 up to the limiting letter's position in the alphabet (1-based). For each i, we move to column i of the screen using TAB; then, i times, we print the ith letter of the alphabet followed by a space.

\$\endgroup\$
  • \$\begingroup\$ As it turns out you can use the INPUT$(1) command as a direct replacement for the variable z$ for a delta of -2 bytes \$\endgroup\$ – Taylor Scott Jun 29 '18 at 20:14
  • \$\begingroup\$ @TaylorScott Good idea, thanks! \$\endgroup\$ – DLosc Jun 29 '18 at 20:22
2
\$\begingroup\$

Japt -R, 24 23 17 15 bytes

Outputs an array, includes a leading newline and a leading & trailing space on each line.

IòUc ÏçSiXd¹iYç

Test it

  • 1 byte saved with help from Oliver and a further 6 thanks to him pointing out a better way to generate the initial array.
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 18 bytes

F⁺⌕αθ¹«P×⁺§αι ⁺ι¹↘

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Nah, you can't let 05AB1E beat Charcoal... :P \$\endgroup\$ – totallyhuman Aug 23 '17 at 14:52
  • \$\begingroup\$ @totallyhuman the revenge :p \$\endgroup\$ – Erik the Outgolfer Aug 23 '17 at 14:55
  • \$\begingroup\$ Sadly arbitrary leading whitespace isn't allowed otherwise E…·?θ⁺× κ⪫× κι would do the job in 14 bytes. \$\endgroup\$ – Neil Aug 23 '17 at 15:29
  • \$\begingroup\$ @Neil One leading whitespace is allowed, but I'm not sure how ? got in there. It should be A instead I think. Oh wait, ohhhhh I see what you mean. \$\endgroup\$ – Erik the Outgolfer Aug 23 '17 at 15:30
1
\$\begingroup\$

Braingolf, 65 bytes

a#a-# 7-,-~vc<!?>[$_]:$_|&,(.#a-!?.>[# M]1+>[.M# M]:$_!@|v#
&@R);

Try it online!

Lowercase.

Contains 1 trailing space on each line, and a trailing newline at the end of output.

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

C# (.NET Core), 103 bytes

n=>{var i='`';var l="";for(;i<n;l+='\n'){l+="".PadLeft(i++-96);for(int s=96;s++<i;)l+=i+" ";}return l;}

Try it online!

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

JavaScript, 102 94 bytes

2 bytes saved thanks to Neil

f=
a=>[...Array(parseInt(a,36)-9)].map((a,b)=>''.padEnd(b).padEnd(b*3+1,(b+10).toString(36)+' '))

console.log(f('k').join`\n`)

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

Retina, 51 bytes

^.
$&$&
}T`L`_L`^.
.
$.`$* $&$.`$* ¶
+`(\w) \B
$&$1

Try it online! Explanation:

^.
$&$&

Duplicate the (first) letter.

}T`L`_L`^.

Rotate it back 1 in the alphabet, or delete it if it's a duplicate A. Keep duplicating and rotating until we duplicate A, at which point the deletion undoes the duplication and the loop completes.

.
$.`$* $&$.`$* ¶

Replace each letter with a line with the letter padded on both sides.

+`(\w) \B
$&$1

Insert duplicate letters between all pairs of padding spaces to the right of existing letters.

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

Haskell, 57 bytes

x!'@'=x
x!e=([e]:[' ':r++' ':[last r]|r<-x])!pred e
([]!)

Try it online!

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

Charcoal, 15 bytes

F…·AS«P⪫E…@ιι ↘

Try it online! Link is to verbose version of code. Explanation:

 …·AS           Inclusive character range from A to the input
F    «          Loop over each character
         …@ι    Exclusive range from @ to the current character
        E   ι   Replace each element with the current character
       ⪫        Join with spaces
      P         Print without moving the cursor.
              ↘ Move the cursor down and right.

If extra padding was legal, this would work for 14 bytes:

E…·?θ⁺× κ⪫× κι

Try it online! Link is to verbose version of code.

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

Mathematica, 70 bytes

(T=Table)[""<>{" "~T~i,T[Alphabet[][[i]]<>" ",i]},{i,LetterNumber@#}]&

lowercase

outputs a list

thanx @ngenisis for corrections

For version place Column@ at the beginning

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

Excel VBA, 72 Bytes

Anonymous VBE immediate window function that takes input from cell A1 and outputs to the VBE immediate window

For i=1To Asc([A1])-64:[B1]=i:?Space(i-1)[REPT(CHAR(B1+64)&" ",B1)]:Next
\$\endgroup\$
1
\$\begingroup\$

Pyth, 17 bytes

.e+*kd*+bdhk<GhxG

Try it here (pretty print version).


How does this work?

  • hxG - Takes the index of the input in the lowercase alphabet.

  • <G - Trims every character after the input from the alphabet.

  • .e - Enumerated Map. Maps over the trimmed alphabet with the indexes as k and the letters as b.

  • *kd - Append k spaces.

  • +bd - b + a space (the current letter + space).

  • *...hk - Repeat k+1 times.

  • +(...)(...) - Concatenate.

\$\endgroup\$
  • 1
    \$\begingroup\$ One of my favorite things about Pyth is writing an answer and finding that someone wrote the same answer, character for character. It hits that Python "there is a best answer" spot perfectly! \$\endgroup\$ – Dave Aug 23 '17 at 15:08
  • \$\begingroup\$ @pizzakingme Yeah, I wonder if I can do better \$\endgroup\$ – Mr. Xcoder Aug 23 '17 at 15:09
  • \$\begingroup\$ the space addition feels wrong, I think better is possible \$\endgroup\$ – Dave Aug 23 '17 at 15:14
  • \$\begingroup\$ @pizzakingme I could get .e+*kdjd*bhk<GhxG as 17 bytes as well \$\endgroup\$ – Mr. Xcoder Aug 23 '17 at 15:17
  • \$\begingroup\$ 16 bytes: .e+*kd*+bdhkhcGQ \$\endgroup\$ – Dave Aug 23 '17 at 15:22
1
\$\begingroup\$

C++ (gcc), 164 bytes

#include<iostream>
#define f for(int i=0;i<o-'`';i++)
using namespace std;int main(){char c;cin>>c;for(char o='a';o<=c;o++){f cout<<' ';f cout<<o<<' ';cout<<'\n';}}

My first attempt after a long time lurking!

Ungolfed code below:

#include <iostream>

using namespace std;
#define f for (auto i = 0; i < output - '`'; i++)

int main()
{
  char input;

  cin >> input;

  for (char output = 'a'; output <= input; output++)
  {
    f cout << ' ';

    f cout << output << ' ';

    cout << endl;
  }
}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I know there has to be a bunch of golfing things to do, but so far, that's the smallest I've gotten. \$\endgroup\$ – Drise Aug 23 '17 at 20:38

Your Answer

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