Build an Alphabet Pyramid

19

Today we're going to build a pyramid out of letters! Here's an example letter pyramid for the first 5 letters:

  1. Write the first 5 letters with a space between, first ascending and then descending.

    A B C D E D C B A
    
  2. Do the same thing for the first four letters on the line above, but with two extra leading spaces:

      A B C D C B A
    A B C D E D C B A
    
  3. Repeat the same step until the last line is just 'A'

            A 
          A B A
        A B C B A
      A B C D C B A
    A B C D E D C B A
    
  4. Repeat steps two and three going down instead of going up:

            A 
          A B A
        A B C B A
      A B C D C B A
    A B C D E D C B A
      A B C D C B A
        A B C B A
          A B A
            A 
    

This same pattern can be extended up to 26 characters. Your challenge is to write a program or function that takes an integer as input, and produces the corresponding letter pyramid. You can choose to use uppercase or lowercase characters. You may always assume that the input will be an integer in [1, 26], and the output may be any reasonable format for a 2d string. For example, a string with newlines in it, an array of characters, printing to a file, etc. Each line may have trailing spaces on it, and you may optionally output one trailing newline.

Here are some example inputs/outputs:

1:

A

2:

  A
A B A
  A

3:

    A
  A B A
A B C B A
  A B A
    A

5:
        A 
      A B A
    A B C B A
  A B C D C B A
A B C D E D C B A
  A B C D C B A
    A B C B A
      A B A
        A 

13:


                        A
                      A B A
                    A B C B A
                  A B C D C B A
                A B C D E D C B A
              A B C D E F E D C B A
            A B C D E F G F E D C B A
          A B C D E F G H G F E D C B A
        A B C D E F G H I H G F E D C B A
      A B C D E F G H I J I H G F E D C B A
    A B C D E F G H I J K J I H G F E D C B A
  A B C D E F G H I J K L K J I H G F E D C B A
A B C D E F G H I J K L M L K J I H G F E D C B A
  A B C D E F G H I J K L K J I H G F E D C B A
    A B C D E F G H I J K J I H G F E D C B A
      A B C D E F G H I J I H G F E D C B A
        A B C D E F G H I H G F E D C B A
          A B C D E F G H G F E D C B A
            A B C D E F G F E D C B A
              A B C D E F E D C B A
                A B C D E D C B A
                  A B C D C B A
                    A B C B A
                      A B A
                        A


26:

                                                  A
                                                A B A
                                              A B C B A
                                            A B C D C B A
                                          A B C D E D C B A
                                        A B C D E F E D C B A
                                      A B C D E F G F E D C B A
                                    A B C D E F G H G F E D C B A
                                  A B C D E F G H I H G F E D C B A
                                A B C D E F G H I J I H G F E D C B A
                              A B C D E F G H I J K J I H G F E D C B A
                            A B C D E F G H I J K L K J I H G F E D C B A
                          A B C D E F G H I J K L M L K J I H G F E D C B A
                        A B C D E F G H I J K L M N M L K J I H G F E D C B A
                      A B C D E F G H I J K L M N O N M L K J I H G F E D C B A
                    A B C D E F G H I J K L M N O P O N M L K J I H G F E D C B A
                  A B C D E F G H I J K L M N O P Q P O N M L K J I H G F E D C B A
                A B C D E F G H I J K L M N O P Q R Q P O N M L K J I H G F E D C B A
              A B C D E F G H I J K L M N O P Q R S R Q P O N M L K J I H G F E D C B A
            A B C D E F G H I J K L M N O P Q R S T S R Q P O N M L K J I H G F E D C B A
          A B C D E F G H I J K L M N O P Q R S T U T S R Q P O N M L K J I H G F E D C B A
        A B C D E F G H I J K L M N O P Q R S T U V U T S R Q P O N M L K J I H G F E D C B A
      A B C D E F G H I J K L M N O P Q R S T U V W V U T S R Q P O N M L K J I H G F E D C B A
    A B C D E F G H I J K L M N O P Q R S T U V W X W V U T S R Q P O N M L K J I H G F E D C B A
  A B C D E F G H I J K L M N O P Q R S T U V W X Y X W V U T S R Q P O N M L K J I H G F E D C B A
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
  A B C D E F G H I J K L M N O P Q R S T U V W X Y X W V U T S R Q P O N M L K J I H G F E D C B A
    A B C D E F G H I J K L M N O P Q R S T U V W X W V U T S R Q P O N M L K J I H G F E D C B A
      A B C D E F G H I J K L M N O P Q R S T U V W V U T S R Q P O N M L K J I H G F E D C B A
        A B C D E F G H I J K L M N O P Q R S T U V U T S R Q P O N M L K J I H G F E D C B A
          A B C D E F G H I J K L M N O P Q R S T U T S R Q P O N M L K J I H G F E D C B A
            A B C D E F G H I J K L M N O P Q R S T S R Q P O N M L K J I H G F E D C B A
              A B C D E F G H I J K L M N O P Q R S R Q P O N M L K J I H G F E D C B A
                A B C D E F G H I J K L M N O P Q R Q P O N M L K J I H G F E D C B A
                  A B C D E F G H I J K L M N O P Q P O N M L K J I H G F E D C B A
                    A B C D E F G H I J K L M N O P O N M L K J I H G F E D C B A
                      A B C D E F G H I J K L M N O N M L K J I H G F E D C B A
                        A B C D E F G H I J K L M N M L K J I H G F E D C B A
                          A B C D E F G H I J K L M L K J I H G F E D C B A
                            A B C D E F G H I J K L K J I H G F E D C B A
                              A B C D E F G H I J K J I H G F E D C B A
                                A B C D E F G H I J I H G F E D C B A
                                  A B C D E F G H I H G F E D C B A
                                    A B C D E F G H G F E D C B A
                                      A B C D E F G F E D C B A
                                        A B C D E F E D C B A
                                          A B C D E D C B A
                                            A B C D C B A
                                              A B C B A
                                                A B A
                                                  A

As always, this is so standard loopholes apply and the shortest answer in bytes wins!

James

Posted 2016-11-23T00:18:02.197

Reputation: 54 537

3Related - same challenge with slightly different chars, no spaces, no input – Sp3000 – 2016-11-23T01:42:03.983

1@stewiegriffin No, the spaces should be there either way. – James – 2016-11-23T15:02:30.683

4Scrolling this page produces a neat optical illusion for the 26 output. – cobaltduck – 2016-11-23T19:45:36.060

Answers

7

Python, 184 174 169 bytes

R=range
def g(a):
 def f(x,y,z):
  for i in R(x,y,z):print "  "*(i-1)," ".join([chr(65+j) for j in R((a-i))]+[chr(65+(a-i-2)-j) for j in R((a-i-1))])
 f(a,0,-1);f(2,a,1)

Edit: saved 5 bytes thanks to @nedla2004

nephi12

Posted 2016-11-23T00:18:02.197

Reputation: 1 691

I see a two things that can be changed. 1.) You can putg on three lines. 2.) Assign range to a variable. You can see both of these here. (Note: Before def f(x,y,z): there is a space, before the for loop there is a tab.

– nedla2004 – 2016-11-23T02:05:22.860

Aren't there 3 ranges that you can replace? – nedla2004 – 2016-11-23T02:09:59.827

You can save 4 bytes by turning R((a-i)) into R(a-i) and R((a-i-1)) into R(a-i-1), can't you? – L3viathan – 2016-11-23T14:13:00.803

There are 2 spaces that can be removed from before the for loops inside the list comprehension. – Yytsi – 2016-11-24T04:54:19.160

An useless whitespace after print. – Yytsi – 2016-11-25T16:28:37.003

6

05AB1E, 13 bytes

Code:

A.ps£û€û€S».c

Explanation:

A.p             # Push all prefixes of the alphabet.
   s£           # Only get the first input elements.
     û          # Palindromize, turns ['a', 'ab', 'abc'] -> ['a', 'ab', 'abc', 'ab', 'a']
      €û        # Palindromize each, turns ['a', 'ab', 'abc', 'ab', 'a']
                  into... ['a', 'aba', 'abcba', 'aba', 'a']
        €S      # Split each element.
          »     # Gridify, joins the arrays be newlines and the arrays in the arrays 
                  by spaces.
           .c   # Centralize, aligning the text to the center.

Uses the CP-1252 encoding. Try it online!

Adnan

Posted 2016-11-23T00:18:02.197

Reputation: 41 965

So this is what "centralized" stands for in info.txt! Nice solution by the way. – Osable – 2016-11-23T07:42:39.853

@Osable Yep, it also works for strings with newlines in it (rather than only arrays). – Adnan – 2016-11-23T07:48:27.497

5

MATL, 25 24 bytes

Zv&+64+G-t64>*1&!t0*hTec

Try it online!

Longer alternatives:

  • 1Y20hiZv&+G-t0>*1&!t0*hTe) (26 bytes)
  • Zv&+64+G-t64>*l2&Y"tZyP:o*c (27 bytes)

Explanation

         % Implicit input 
Zv       % Symmetric range. For input 3 it gives [1 2 3 2 1]
&+       % Matrix of all pairwise additions. For input 3 it gives a 5×5 matrix
64+G     % Add 64 and subtract input. This gives the desired ASCII codes in the
         % central rhombus 
t64>*    % Make values less than 65 equal to 0. This affects entries outside the
         % central rhombus 
1&!      % Permute second and third dimensions. Transforms the 5×5 matrix into
         % a 5×1×5 array
t0*      % Push a copy of that array with all entries equal to 0
h        % Concatenate along the second dimension. Gives a 5×2×5 array
Te       % Collapse the second and third dimensions. Gives a 5×10 matrix
c        % Convert to char. Char zero is displayed as space
         % Implicit display

Luis Mendo

Posted 2016-11-23T00:18:02.197

Reputation: 87 464

4

V, 45 bytes

i¬A[À|lDybA"Ó./& 
òÄó¨á© á úe± 
>>.YGp{òd

Try it online!

This ended up being way less golfy than I had hoped, so I'm not going to post an explanation yet. Hopefully I can whittle it down some more first. As usual, here is a hexdump:

0000000: 69ac 415b 1bc0 7c6c 4479 6241 1f12 221b  i.A[..|lDybA..".
0000010: d32e 2f26 200a f2c4 f3a8 e1a9 20e1 20fa  ../& ....... . .
0000020: 65b1 200a 3e3e 2e59 4770 7bf2 64         e. .>>.YGp{.d

James

Posted 2016-11-23T00:18:02.197

Reputation: 54 537

4

J, 34 bytes

(' ',u:65+i.26){~0>.]-[:+/~|@i:@<:

Takes the number as input and returns a 2D character array.

Explanation

(' ',u:65+i.26){~0>.]-[:+/~|@i:@<:  input y
                                <:  y - 1
                             i:     "steps" -- i:2 -> -2 1 0 1 2
                           |        absolute value
                        +/~         addition table
                      [:            join right two tines into a conjunction
                    ]-              y - this table
                 0>.                max(0, that)
(' ',u:65+i.26)                     the alphabet preceded by a space
               {~                   index

Decomposed test case

   n =: 5
   <: n
4
   i: <: n
_4 _3 _2 _1 0 1 2 3 4
   | i: <: n
4 3 2 1 0 1 2 3 4
   +/~ | i: <: n
8 7 6 5 4 5 6 7 8
7 6 5 4 3 4 5 6 7
6 5 4 3 2 3 4 5 6
5 4 3 2 1 2 3 4 5
4 3 2 1 0 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 3 4 5 6
7 6 5 4 3 4 5 6 7
8 7 6 5 4 5 6 7 8
   n - +/~ | i: <: n
_3 _2 _1 0 1 0 _1 _2 _3
_2 _1  0 1 2 1  0 _1 _2
_1  0  1 2 3 2  1  0 _1
 0  1  2 3 4 3  2  1  0
 1  2  3 4 5 4  3  2  1
 0  1  2 3 4 3  2  1  0
_1  0  1 2 3 2  1  0 _1
_2 _1  0 1 2 1  0 _1 _2
_3 _2 _1 0 1 0 _1 _2 _3
   0 >. n - +/~ | i: <: n
0 0 0 0 1 0 0 0 0
0 0 0 1 2 1 0 0 0
0 0 1 2 3 2 1 0 0
0 1 2 3 4 3 2 1 0
1 2 3 4 5 4 3 2 1
0 1 2 3 4 3 2 1 0
0 0 1 2 3 2 1 0 0
0 0 0 1 2 1 0 0 0
0 0 0 0 1 0 0 0 0
   u:65
A
   i.26
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   65+i.26
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
   u:65+i.26
ABCDEFGHIJKLMNOPQRSTUVWXYZ
   ' ',u:65+i.26
 ABCDEFGHIJKLMNOPQRSTUVWXYZ
   (' ',u:65+i.26) {~ 0 >. n - +/~ | i: <: n
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
 ABCDCBA
  ABCBA
   ABA
    A

Let's try this with input 5.

Test cases

   f =: (' ',u:65+i.26){~0>.]-[:+/~|@i:@<:
   f 1
A
   f 2
 A
ABA
 A
   f 3
  A
 ABA
ABCBA
 ABA
  A
   f 4
   A
  ABA
 ABCBA
ABCDCBA
 ABCBA
  ABA
   A
   f 5
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
 ABCDCBA
  ABCBA
   ABA
    A
   f 26
                         A
                        ABA
                       ABCBA
                      ABCDCBA
                     ABCDEDCBA
                    ABCDEFEDCBA
                   ABCDEFGFEDCBA
                  ABCDEFGHGFEDCBA
                 ABCDEFGHIHGFEDCBA
                ABCDEFGHIJIHGFEDCBA
               ABCDEFGHIJKJIHGFEDCBA
              ABCDEFGHIJKLKJIHGFEDCBA
             ABCDEFGHIJKLMLKJIHGFEDCBA
            ABCDEFGHIJKLMNMLKJIHGFEDCBA
           ABCDEFGHIJKLMNONMLKJIHGFEDCBA
          ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
         ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
        ABCDEFGHIJKLMNOPQRQPONMLKJIHGFEDCBA
       ABCDEFGHIJKLMNOPQRSRQPONMLKJIHGFEDCBA
      ABCDEFGHIJKLMNOPQRSTSRQPONMLKJIHGFEDCBA
     ABCDEFGHIJKLMNOPQRSTUTSRQPONMLKJIHGFEDCBA
    ABCDEFGHIJKLMNOPQRSTUVUTSRQPONMLKJIHGFEDCBA
   ABCDEFGHIJKLMNOPQRSTUVWVUTSRQPONMLKJIHGFEDCBA
  ABCDEFGHIJKLMNOPQRSTUVWXWVUTSRQPONMLKJIHGFEDCBA
 ABCDEFGHIJKLMNOPQRSTUVWXYXWVUTSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTUVWXYZYXWVUTSRQPONMLKJIHGFEDCBA
 ABCDEFGHIJKLMNOPQRSTUVWXYXWVUTSRQPONMLKJIHGFEDCBA
  ABCDEFGHIJKLMNOPQRSTUVWXWVUTSRQPONMLKJIHGFEDCBA
   ABCDEFGHIJKLMNOPQRSTUVWVUTSRQPONMLKJIHGFEDCBA
    ABCDEFGHIJKLMNOPQRSTUVUTSRQPONMLKJIHGFEDCBA
     ABCDEFGHIJKLMNOPQRSTUTSRQPONMLKJIHGFEDCBA
      ABCDEFGHIJKLMNOPQRSTSRQPONMLKJIHGFEDCBA
       ABCDEFGHIJKLMNOPQRSRQPONMLKJIHGFEDCBA
        ABCDEFGHIJKLMNOPQRQPONMLKJIHGFEDCBA
         ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
          ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
           ABCDEFGHIJKLMNONMLKJIHGFEDCBA
            ABCDEFGHIJKLMNMLKJIHGFEDCBA
             ABCDEFGHIJKLMLKJIHGFEDCBA
              ABCDEFGHIJKLKJIHGFEDCBA
               ABCDEFGHIJKJIHGFEDCBA
                ABCDEFGHIJIHGFEDCBA
                 ABCDEFGHIHGFEDCBA
                  ABCDEFGHGFEDCBA
                   ABCDEFGFEDCBA
                    ABCDEFEDCBA
                     ABCDEDCBA
                      ABCDCBA
                       ABCBA
                        ABA
                         A

Conor O'Brien

Posted 2016-11-23T00:18:02.197

Reputation: 36 228

2Are you sure the lack if spaces in the output is OK? All other answers output this with spaces. I've asked just in case (since this would make it quite a bit easier in MATLAB too. :) – Stewie Griffin – 2016-11-23T08:27:01.307

There seem to be some spaces missing – James – 2016-11-23T15:03:18.157

@DrMcMoylex I misread the challenge, I thought returning a 2D char array with the letters was sufficient. Working on a fix rn – Conor O'Brien – 2016-11-23T17:35:16.640

4

Jelly, 13 bytes

RØAḣUz⁶ŒBṚŒḄG

TryItOnline!

How?

RØAḣUz⁶ŒBṚŒḄG - Main link: n         e.g. 3
R             - range                     [1,2,3]
 ØA           - uppercase alphabet yield  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   ḣ          - head                      ["A","AB","ABC"]
    U         - upend (vectorises)        ["A","BA","CBA"]
     z        - transpose with filler...
      ⁶       -     space character       ["ABC"," AB","  A"]
       ŒB     - bounce (vectorises)       ["ABCBA"," ABA ","  A  "]
         Ṛ    - reverse                   ["  A  "," ABA ","ABCBA"]
          ŒḄ  - bounce (flat)             ["  A  "," ABA ","ABCBA"," ABA ","  A  "]
            G - grid format (join each with spaces and join with line feeds)
                                              A    
                                            A B A  
                                          A B C B A
                                            A B A  
                                              A    

Jonathan Allan

Posted 2016-11-23T00:18:02.197

Reputation: 67 804

3

Pyke, 12 bytes

FhGR<Q{s
)OX

Try it here!

Disable warnings for specified output

Blue

Posted 2016-11-23T00:18:02.197

Reputation: 26 661

2

Python, 158 154 140 139 bytes

r=range
s=int(raw_input())
a=map(chr,range(65,91))
for i in r(0,s)+r(0,s-1)[::-1]:
 print (' '.join(a[:i+1]+a[:i][::-1])).center(s*4-2,' '))

This is my first post so be gentle! I made heavy use of [::-1] to reverse strings, so maybe there's some optimization to be had there. Comments welcome.

EDITS:

Thanks @Rod for the pointer to the helpful link to the Python code golf topic and also for reminding me that the letters need spaces between them. Also thanks @wec for the general tips about removing spaces.

Jeremy Pedersen

Posted 2016-11-23T00:18:02.197

Reputation: 21

You can remove spaces on either side of your operators (e. g. = becomes =), after commas, and before the opening parenthesis in the print statement. Python indentation can be any level as long as it's consistent, so you can save 3 characters by independenting your print statement only one space instead of four. Those are the easy savings I can see. – wec – 2016-11-23T12:41:56.667

Welcome c: , we have this nice topic with lots of python tips (including a shorter alternative to generate the alphabet). Also since you are using z just one time, you don't need to use a variable, just put the s*2-1 directly in the center function. Just one detail, the output need to have spaces between the letters, a ' '.join right after the print should be enough (but you need to rework the s*2-1 to get the right value).

– Rod – 2016-11-23T13:27:01.230

also, the default starting value for range and for [:] is 0, so you don need to put it explicitly – Rod – 2016-11-23T13:43:41.083

2

C, 124 123 bytes

Saving 1 byte thanks to Mukul Kumar

Currently cant get my head to transform the double loop into a single one, will leave it like this for the moment. Actually, this is larger

for(l=0;l<4*n*(4*n+1);++l){
  i=l/(4*n+1)-2*n;
  j=l%(4*n+1)-2*n;

So i just let the nested loop.

i,j,k;f(n){for(i=-2*n-1;++i<2*n;){for(j=-2*n-1;++j<=2*n;k=abs(i/2)+abs(j/2),putchar(j-2*n?k>=n||i%2||j%2?32:65+n+~k:10));}}

Uses the Manhattan norm to get the diamond shape.

Ungolfed:

i,j,k;
f(n){
 for(i=-2*n-1;++i<2*n;){
  for(j=-2*n-1;++j<=2*n;
   k=abs(i/2)+abs(j/2),
   putchar(j-2*n?k>=n||i%2||j%2?32:65+n+~k:10)
  );
 }
}

Karl Napf

Posted 2016-11-23T00:18:02.197

Reputation: 4 131

You can reduce one byte by replacingj==2*n withj-2*n and exchanging the ? () values with :() value – Mukul Kumar – 2016-11-23T12:18:56.687

2

C#, 266 263 262 261 245 238 235 232 bytes

Golfed:

List<string> F(int n){int i=0;var l=new List<string>();for(;i<n;i++){var h="";for(var c='A';c<'B'+i;c++)h+=" "+c;l.Add(new string(' ',(n-i+1)*2)+h+" "+string.Concat(h.Remove(i*2).Reverse()));}for(i-=2;i>=0;i--)l.Add(l[i]);return l;}

Ungolfed with comments:

    List<string> F(int n)
    {
        int i = 0;
        var l = new List<string>(); //collection of lines

        for (; i < n; i++)
        {
            var h = ""; //half of line

            //adding letters to first half of line
            for (var c = 'A'; c < 'B' + i; c++)
                h += " " + c;

            //adding leading spaces + half of line + reversed half of line to list
            l.Add(new string(' ', (n - i + 1)*2) + h + " " + string.Concat(h.Remove(i*2).Reverse()));
        }

        //adding lines in descending order
        for (i -= 2; i >= 0; i--)
            l.Add(l[i]);

        return l;
    }

Try it: http://rextester.com/WIL67940

Returns list of strings. Each string contains one output line.

I did that for fun and training. I realize that winning any of code-golf with C# is out of range.

EDIT1: Changed string interpolation to + operator.

EDIT2: c <= 'A' + i => c < 'B' + i

EDIT3: Swapped char with var.

EDIT4: Changed return type from void to List.

EDIT5: Removal of unnecessary variable.

EDIT6: New way of separating line halves.

EDIT7: @Kaspar Kjeldsen, thank you.

paldir

Posted 2016-11-23T00:18:02.197

Reputation: 109

1You can shave off a character by replacing string h=""; with var h=""; – Kaspar Kjeldsen – 2016-11-23T12:20:04.700

@KasparKjeldsen I can't believe I omited that. – paldir – 2016-11-23T12:25:12.910

1

Can get shorter still! http://rextester.com/JLUL92950 string[] F(int n){int i=0;var q=2n-1;var l=new string[q];for(;i<n;i++){var h="";for(var c='A';c<'B'+i;c++)h+=" "+c;l[q-i-1]=l[i]=new string(' ',(n-i+1)2)+h+ " "+string.Concat(h.Remove(i*2).Reverse());}return l;}

– Kaspar Kjeldsen – 2016-11-23T12:51:08.420

2

Brain-Flak, 630 + 3 = 633 bytes

This requires the -A flag to run

Try it Online

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

This is not a great golf but this challenge is quite difficult in Brain-Flak

Post Rock Garf Hunter

Posted 2016-11-23T00:18:02.197

Reputation: 55 382

1

Pyth, 23 bytes

V+SQt_SQp*dy-QNt_pjd<GN

Try it online

Cameron McCluskie

Posted 2016-11-23T00:18:02.197

Reputation: 346

1

Ruby, 137 115 100 84 bytes

->n{e=->s{s+s.reverse[1..-1]};e[(0..n-1).map{|i|"  "*(n-i)+e[[*?A..?Z][0..i]*' ']}]}

Thanks to manatwork for the comments.

G B

Posted 2016-11-23T00:18:02.197

Reputation: 11 099

Nice first answer. For ways to improve it, take a look at Tips for golfing in Ruby. First of all, kernigh's tip regarding functions. BTW, 0..n-10...n.

– manatwork – 2016-11-23T09:35:45.613

1If you reverse the ternary logic you can use a single character relational operator: n>0?…:"". – manatwork – 2016-11-23T09:42:51.017

1“Your challenge is to write a program or function”. Better make it an anonymous lambda – that can take its parameter directly as integer. – manatwork – 2016-11-23T10:30:14.080

1

TSQL, 261 bytes

One line query - no looping

DECLARE @i INT =5;

WITH C as(SELECT top(@i*2-1)number z
FROM master..spt_values WHERE'P'=type)SELECT
top(@i*2-1)space(abs(z-@i+1)*2)+v+STUFF(reverse(v),1,3,'')FROM(SELECT
z,(SELECT top((@i-abs(@i-z-1)))char(65+z)+' 'FROM c
for xml path(''),type).value('.','varchar(52)')v FROM c)d

Unfortunately the fiddle eats up the spaces at the start of the output lines. Spaces will be there if executed in SQL Server management studio

Fiddle

t-clausen.dk

Posted 2016-11-23T00:18:02.197

Reputation: 2 874

1

Befunge 93, 175 bytes

Try it online!

Probably not very well golfed. Oh well. This was hard enough with befunge:

&:00p"B"\- :13v
v,: _v#`"@"<p5<
v," "<v`+*9<
>" ",: 35g7^
v47"v"_>1+ v
>p#  0^:>- v
v"A"g53<1v<
>+00g-` |53
v">",+91<p+
>74p35g:v11
_v# -g00<+^ _@#:
1>59*:91v> v
^p+199p8>#+ <

Good luck figuring out how it works! I barely know.

MildlyMilquetoast

Posted 2016-11-23T00:18:02.197

Reputation: 2 907

Nice use of a run-time code modification. – James Holderness – 2016-11-24T12:52:42.360

1

C#, 199 bytes

q=>{Action<int>L=x=>{var s="";var k=0;for(;k<x;)s+=(char)('A'+k++)+" ";for(--k;--k>=0;)s+=(char)('A'+k)+" ";Console.WriteLine(new string(' ',(q-x)*2)+s);};var i=0;for(;i<q;)L(++i);for(;i>1;)L(--i);};

As always, C# is not much of a golfing language, but I prefer "readable" code much more than esoteric Code. Also I just did this for fun :)

Here's an ungolfed version, so you can easily understand what I did:

Action<int> C = q => 
{
    Action<int> L = x => 
    {
        var s = "";
        var k = 0;
        for (; k < x;)
            s += (char)('A' + k++) + " ";
        for (--k; --k >= 0;)
            s += (char)('A' + k) + " ";
        Console.WriteLine(new string(' ', (q - x) * 2) + s);
    };
    var i = 0;
    for (; i < q;)
        L(++i);
    for (; i > 1;)
        L(--i);
};

(I think this could be optimized a lot though..)

Stefan

Posted 2016-11-23T00:18:02.197

Reputation: 261

1

Java, 213 bytes

void p(int n){int i=1,s=1,f,c;while(i>0){f=(n+1-i)*2;System.out.printf("%"+f+"s","");c=65;for(;c<64+i;)System.out.printf("%c ",c++);for(;c>64;)System.out.printf("%c ",c--);System.out.println();if(i==n)s=-1;i+=s;}}

Ungolfed:

void p(int n) {
    int i = 1, s = 1, f, c;
    while (i > 0) {
        f = (n + 1 - i) * 2;
        System.out.printf("%" + f + "s", "");
        c = 65;
        for (; c < 64 + i; ) System.out.printf("%c ", c++);
        for (; c > 64; ) System.out.printf("%c ", c--);
        System.out.println();
        if (i == n) s = -1;
        i += s;
    }
}

Eugene

Posted 2016-11-23T00:18:02.197

Reputation: 153

0

Java, 394 Bytes

I normally do C#, but it's good to mix it up...

Golfed:

String P(int n){String a="ABCDEFGHIJKLMNOPQRSTUVWXYZ",o="",k="",s="";int i=0,j=0,l=0;java.util.List<String>b=new java.util.ArrayList<String>();for(i=1;i<=n;i++){k="";s=a.substring(0,i);l=s.length();for(j=0;j<l;j++)k+=s.toCharArray()[j]+" ";while(k.length()<n+n)k=" "+k;if(i>1)for(j=l-2;j>=0;j--)k+=s.toCharArray()[j]+" ";k+="\r\n";b.add(k);o+=k;}for(i=b.size()-2;i>=0;i--)o+=b.get(i);return o;}

Ungolfed:

public String P(int n)
{
    String a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", o ="", k="",s="";
    int i =0, j=0, l=0;
    java.util.List<String> b = new java.util.ArrayList<String>();
    for (i = 1; i <= n; i++) {
        k = "";
        s = a.substring(0, i);
        l = s.length();

        for (j = 0; j < l; j++)
            k += s.toCharArray()[j] + " ";

        while(k.length() < n + n)
            k= " " + k;

        if(i > 1)
            for (j = l-2; j >= 0; j--)
                k += s.toCharArray()[j] + " ";           

        k += "\r\n";
        b.add(k);
        o += k;
     }

    for (i = b.size()-2; i >= 0; i--)
        o += b.get(i);

    return o;
}

Test:

BuildAnAlphabetPyramid b = new BuildAnAlphabetPyramid();
System.out.println(b.P(5));
System.out.println(b.P(26));

        A 
      A B A 
    A B C B A 
  A B C D C B A 
A B C D E D C B A 
  A B C D C B A 
    A B C B A     
      A B A 
        A 

                                                  A 
                                                A B A 
                                              A B C B A 
                                            A B C D C B A 
                                          A B C D E D C B A 
                                        A B C D E F E D C B A 
                                      A B C D E F G F E D C B A 
                                    A B C D E F G H G F E D C B A 
                                  A B C D E F G H I H G F E D C B A 
                                A B C D E F G H I J I H G F E D C B A 
                              A B C D E F G H I J K J I H G F E D C B A 
                            A B C D E F G H I J K L K J I H G F E D C B A 
                          A B C D E F G H I J K L M L K J I H G F E D C B A 
                        A B C D E F G H I J K L M N M L K J I H G F E D C B A 
                      A B C D E F G H I J K L M N O N M L K J I H G F E D C B A 
                    A B C D E F G H I J K L M N O P O N M L K J I H G F E D C B A 
                  A B C D E F G H I J K L M N O P Q P O N M L K J I H G F E D C B A 
                A B C D E F G H I J K L M N O P Q R Q P O N M L K J I H G F E D C B A 
              A B C D E F G H I J K L M N O P Q R S R Q P O N M L K J I H G F E D C B A 
            A B C D E F G H I J K L M N O P Q R S T S R Q P O N M L K J I H G F E D C B A 
          A B C D E F G H I J K L M N O P Q R S T U T S R Q P O N M L K J I H G F E D C B A 
        A B C D E F G H I J K L M N O P Q R S T U V U T S R Q P O N M L K J I H G F E D C B A 
      A B C D E F G H I J K L M N O P Q R S T U V W V U T S R Q P O N M L K J I H G F E D C B A 
    A B C D E F G H I J K L M N O P Q R S T U V W X W V U T S R Q P O N M L K J I H G F E D C B A 
  A B C D E F G H I J K L M N O P Q R S T U V W X Y X W V U T S R Q P O N M L K J I H G F E D C B A 
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Y X W V U T S R Q P O N M L K J I H G F E D C B A 
  A B C D E F G H I J K L M N O P Q R S T U V W X Y X W V U T S R Q P O N M L K J I H G F E D C B A 
    A B C D E F G H I J K L M N O P Q R S T U V W X W V U T S R Q P O N M L K J I H G F E D C B A 
      A B C D E F G H I J K L M N O P Q R S T U V W V U T S R Q P O N M L K J I H G F E D C B A 
        A B C D E F G H I J K L M N O P Q R S T U V U T S R Q P O N M L K J I H G F E D C B A 
          A B C D E F G H I J K L M N O P Q R S T U T S R Q P O N M L K J I H G F E D C B A 
            A B C D E F G H I J K L M N O P Q R S T S R Q P O N M L K J I H G F E D C B A 
              A B C D E F G H I J K L M N O P Q R S R Q P O N M L K J I H G F E D C B A 
                A B C D E F G H I J K L M N O P Q R Q P O N M L K J I H G F E D C B A 
                  A B C D E F G H I J K L M N O P Q P O N M L K J I H G F E D C B A 
                    A B C D E F G H I J K L M N O P O N M L K J I H G F E D C B A 
                      A B C D E F G H I J K L M N O N M L K J I H G F E D C B A 
                        A B C D E F G H I J K L M N M L K J I H G F E D C B A 
                          A B C D E F G H I J K L M L K J I H G F E D C B A 
                            A B C D E F G H I J K L K J I H G F E D C B A 
                              A B C D E F G H I J K J I H G F E D C B A 
                                A B C D E F G H I J I H G F E D C B A 
                                  A B C D E F G H I H G F E D C B A 
                                    A B C D E F G H G F E D C B A 
                                      A B C D E F G F E D C B A 
                                        A B C D E F E D C B A 
                                          A B C D E D C B A 
                                            A B C D C B A 
                                              A B C B A 
                                                A B A 
                                                  A 

Pete Arden

Posted 2016-11-23T00:18:02.197

Reputation: 1 151

0

R, 100 97 87 bytes

x=scan();z=LETTERS;for(i in c(1:x,x:2-1))cat(rep(" ",x-i+1),z[1:i],if(i>1)z[i:2-1],"\n")

4:
#>         A 
#>       A B A 
#>     A B C B A 
#>   A B C D C B A 
#>     A B C B A 
#>       A B A 
#>         A

( Update: scan() for input; abused (x-1):1 == x:2-1. )

Not too shabby for one of the keyword languages by the looks of it (those special-character ones are always going to be better).

Jonathan Carroll

Posted 2016-11-23T00:18:02.197

Reputation: 211

0

PHP, 122 116 bytes

for($i=1-$n=$argv[1];$i<$n;)echo str_pad("",$a=abs($i++)),$s=join(range(A,chr(64+$n-$a))),substr(strrev($s),1),"\n";

is there a shorter way?

Titus

Posted 2016-11-23T00:18:02.197

Reputation: 13 814

0

JavaScript (ES6), 121 bytes

n=>[...Array(n+n--)].map((_,i,a)=>a.map((_,j)=>(j=10+n-g(i)-g(j))>9?(10-j).toString(36):` `,g=i=>i<n?n-i:i-n).join` `).join`\n`

Where \n represents the literal newline character. Outputs in lower case.

Neil

Posted 2016-11-23T00:18:02.197

Reputation: 95 035

0

Batch, 269 bytes

@echo off
set/an=%1-1
set a=ABCDEFGHIJKLMNOPQRSTUVWXYZ                         
for /l %%i in (-%n%,1,%n%)do set i=%%i&call:l
exit/b
:l
set s=
for /l %%j in (-%n%,1,%n%)do set j=%%j&call:c
echo%s%
exit/b
:c
set/aj=n-%i:-=%-%j:-=%
call set s= %%a:~%j%,1%%%s%

Line 2 ends in 25 spaces; this means that when the alphabet index goes negative those squares simply remain blank.

Neil

Posted 2016-11-23T00:18:02.197

Reputation: 95 035