19
\$\begingroup\$

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!

\$\endgroup\$
  • 3
    \$\begingroup\$ Related - same challenge with slightly different chars, no spaces, no input \$\endgroup\$ – Sp3000 Nov 23 '16 at 1:42
  • 1
    \$\begingroup\$ @stewiegriffin No, the spaces should be there either way. \$\endgroup\$ – DJMcMayhem Nov 23 '16 at 15:02
  • 4
    \$\begingroup\$ Scrolling this page produces a neat optical illusion for the 26 output. \$\endgroup\$ – cobaltduck Nov 23 '16 at 19:45

22 Answers 22

7
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – nedla2004 Nov 23 '16 at 2:05
  • \$\begingroup\$ Aren't there 3 ranges that you can replace? \$\endgroup\$ – nedla2004 Nov 23 '16 at 2:09
  • \$\begingroup\$ 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? \$\endgroup\$ – L3viathan Nov 23 '16 at 14:13
  • \$\begingroup\$ There are 2 spaces that can be removed from before the for loops inside the list comprehension. \$\endgroup\$ – Yytsi Nov 24 '16 at 4:54
  • \$\begingroup\$ An useless whitespace after print. \$\endgroup\$ – Yytsi Nov 25 '16 at 16:28
6
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ So this is what "centralized" stands for in info.txt! Nice solution by the way. \$\endgroup\$ – Osable Nov 23 '16 at 7:42
  • \$\begingroup\$ @Osable Yep, it also works for strings with newlines in it (rather than only arrays). \$\endgroup\$ – Adnan Nov 23 '16 at 7:48
5
\$\begingroup\$

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

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

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
\$\endgroup\$
  • 2
    \$\begingroup\$ Are 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. :) \$\endgroup\$ – Stewie Griffin Nov 23 '16 at 8:27
  • \$\begingroup\$ There seem to be some spaces missing \$\endgroup\$ – DJMcMayhem Nov 23 '16 at 15:03
  • \$\begingroup\$ @DrMcMoylex I misread the challenge, I thought returning a 2D char array with the letters was sufficient. Working on a fix rn \$\endgroup\$ – Conor O'Brien Nov 23 '16 at 17:35
4
\$\begingroup\$

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

Pyke, 12 bytes

FhGR<Q{s
)OX

Try it here!

Disable warnings for specified output

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

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can shave off a character by replacing string h=""; with var h=""; \$\endgroup\$ – Kaspar Kjeldsen Nov 23 '16 at 12:20
  • \$\begingroup\$ @KasparKjeldsen I can't believe I omited that. \$\endgroup\$ – paldir Nov 23 '16 at 12:25
  • 1
    \$\begingroup\$ Can get shorter still! rextester.com/JLUL92950 string[] F(int n){int i=0;var q=2*n-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;} \$\endgroup\$ – Kaspar Kjeldsen Nov 23 '16 at 12:51
2
\$\begingroup\$

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)
  );
 }
}
\$\endgroup\$
  • \$\begingroup\$ You can reduce one byte by replacingj==2*n withj-2*n and exchanging the ? () values with :() value \$\endgroup\$ – Mukul Kumar Nov 23 '16 at 12:18
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – wec Nov 23 '16 at 12:41
  • \$\begingroup\$ 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). \$\endgroup\$ – Rod Nov 23 '16 at 13:27
  • \$\begingroup\$ also, the default starting value for range and for [:] is 0, so you don need to put it explicitly \$\endgroup\$ – Rod Nov 23 '16 at 13:43
2
\$\begingroup\$

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

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

Pyth, 23 bytes

V+SQt_SQp*dy-QNt_pjd<GN

Try it online

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

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

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

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – manatwork Nov 23 '16 at 9:35
  • 1
    \$\begingroup\$ If you reverse the ternary logic you can use a single character relational operator: n>0?…:"". \$\endgroup\$ – manatwork Nov 23 '16 at 9:42
  • 1
    \$\begingroup\$ “Your challenge is to write a program or function”. Better make it an anonymous lambda – that can take its parameter directly as integer. \$\endgroup\$ – manatwork Nov 23 '16 at 10:30
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Nice use of a run-time code modification. \$\endgroup\$ – James Holderness Nov 24 '16 at 12:52
1
\$\begingroup\$

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..)

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

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

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

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).

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

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?

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

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.

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

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.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.