Print the alphabet Christmas tree

4

2

I know it's 1.5 weeks after Christmas but trying to print an isosceles triangle in a monospaced font makes it look weird.

Challenge

Print the alphabet from A to Z so that it forms a filled triangle in the output. For each line in the output, starting from "A", add the next letter.

No inputs, only outputs. Pad the leading edge of the triangle with spaces.

Best answer is the code with the least bytes.

Output

            A
            AB
           ABC
           ABCD
          ABCDE
          ABCDEF
         ABCDEFG
         ABCDEFGH
        ABCDEFGHI
        ABCDEFGHIJ
       ABCDEFGHIJK
       ABCDEFGHIJKL
      ABCDEFGHIJKLM
      ABCDEFGHIJKLMN
     ABCDEFGHIJKLMNO
     ABCDEFGHIJKLMNOP
    ABCDEFGHIJKLMNOPQ
    ABCDEFGHIJKLMNOPQR
   ABCDEFGHIJKLMNOPQRS
   ABCDEFGHIJKLMNOPQRST
  ABCDEFGHIJKLMNOPQRSTU
  ABCDEFGHIJKLMNOPQRSTUV
 ABCDEFGHIJKLMNOPQRSTUVW
 ABCDEFGHIJKLMNOPQRSTUVWX
ABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Java Code (430 bytes)

public static void main(String args[]) {
    String spaces = "";
    String alpha = "";
    char A = 'A';

    for (int i = 0; i < 26; i++) {
        for (int j = 13 - (i/2); j > 0; j--) {
           spaces += " ";
        }

        alpha += (char)(A + i);

        System.out.println(spaces + alpha);
        spaces = "";
    } 
}

bigyihsuan

Posted 2018-01-05T04:42:52.560

Reputation: 1 483

3Welcome to PPCG and nice first challenge! Another tag that is appropriate is kolmogorov-complexity. And most likely someone will ask if leading and trailing newlines and spaces are allowed so specifying if they are might be a good idea. – LiefdeWen – 2018-01-05T04:49:04.253

1Just a note... using String instead of StringBuilder in Java may cause bad performance. It's better use StringBuilder when you need to build a string. (don't argue with me that O(N) is O(1) when N is small, good coding practice is good anyway). – user202729 – 2018-01-05T05:31:33.747

@user202729 But but O(N) is O(1) when N is small. – LiefdeWen – 2018-01-05T05:37:03.853

2No one tell Leaky that alphabet challenges are back – caird coinheringaahing – 2018-01-05T06:43:50.333

1Is lower case alphabet okay as well? – Emigna – 2018-01-05T07:18:30.957

I guess that this is a lot easier than the other one at least in PHP: I can increment letters, but not decrement. – Titus – 2018-12-26T09:13:53.207

Answers

5

Ruby, 44 bytes

t=''
26.times{|x|puts' '*(12-x/2)+(t<<65+x)}

Try it online!

Unihedron

Posted 2018-01-05T04:42:52.560

Reputation: 1 115

3

05AB1E, 5 bytes

Auη.c

Try it online!

Exlpanation

A        # push the alphabet
 u       # convert to upper case
  η      # get list of prefixes
   .c    # centralize, focused to the left

Emigna

Posted 2018-01-05T04:42:52.560

Reputation: 50 798

2

SOGL V0.12, 8 bytes

'⁸∫Zm}¹╚

Try it Here!

Explanation:

'⁸        push 26
  ∫  }    that many times, pushing the counter
   Zm       mold the alphabet to the counters length
      ¹   wrap the stack in an array
       ╚  center horizontally

dzaima

Posted 2018-01-05T04:42:52.560

Reputation: 19 048

1You know your languages is built for kolmo challenges when you have a 'center horizontally' command. – caird coinheringaahing – 2018-01-05T06:39:45.647

1

C# (.NET Core), 114 + 18 = 132 bytes

Try It Online!

()=>string.Join("\n",new int[26].Select((i,e)=>"ABCDEFGHIJKLMNOPQRSTUVWXYZ".Substring(0,e+1).PadLeft(13+(e+1)/2)))

LiefdeWen

Posted 2018-01-05T04:42:52.560

Reputation: 3 381

Can you use a function with a dummy argument (_=>) to save a byte? – caird coinheringaahing – 2018-01-05T06:41:48.060

thats what I usually do but OP said "No inputs" so I thought lets honor that requirement. – LiefdeWen – 2018-01-05T07:10:55.833

1

J, 38 36 bytes

echo(' '<@#"0~2#i._13),&><\u:65+i.26

Try it online!

This is honestly disgusting.

39 bytes

echo(2#i._13)(>@],~' '#~[)"0<\u:65+i.26

Explanation

These will be snippets of the code built up from a REPL session. Three spaces means an input, and no spaces means the output from it.

The alphabet

First we generate the alphabet by making the range [65,90] and converting this to unicode using u:.

   65 + i.10 NB. truncated
65 66 67 68 69 70 71 72 73 74
   u: 65 + i.26
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Then we generate the A, AB, etc. sections by taking all of the prefixes of the alphabet and boxing them using <\

   <\ u: 65 + i.5 NB. truncated again
┌─┬──┬───┬────┬─────┐
│A│AB│ABC│ABCD│ABCDE│
└─┴──┴───┴────┴─────┘

The padding

We need to pad the alphabet with 12, 12, 11, 11, etc. spaces. So first we construct this range using i. with a negative argument (which makes it go from that value minus 1 to 0, descending).

   i. _13
12 11 10 9 8 7 6 5 4 3 2 1 0

We then copy each value twice

   2 # i. _13
12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0

And then copy the space character as many times as specified, boxing it because J would pad the matrix with spaces to fill otherwise.

   '*' #"0~ 2 # i. _5 NB. truncated, and using '*' so you can see the output
****
****
*** 
*** 
**  
**  
*   
*   
    NB. This line is empty
    NB. This line is empty, too

Getting the output

Then we just join the two together using , (unboxing first using &>) and write to STDOUT using echo. There's nothing to show here since it's just the output.

cole

Posted 2018-01-05T04:42:52.560

Reputation: 3 526

1

Excel (VBA), 129 82 bytes

Using Immediate Window.  and cell [A1] as output.

For x=25to 0Step -1:For y=x to 25:b=b &Chr(65+y-x):Next:?Space(x/2.01)&b:b="":Next

remoel

Posted 2018-01-05T04:42:52.560

Reputation: 511

You can make some small changes to get to For x=-25To 0:For y=Abs(x)To 25:b=b+Chr(65+y+x):Next:?Spc(x/-2.1)b:b="":Next or some bigger changes and get to For x=-25To 0:?Spc(x/-2.01);:For y=Abs(x)To 25:?Chr(65+y+x);:Next:?:Next (72 Bytes) – Taylor Scott – 2018-01-11T14:51:30.530

Noted and Thank you. :) Will apply it for future challenge :) – remoel – 2018-01-12T07:14:27.773

0

Perl 5, 32 bytes

say$"x(13-++$k/2),A..$_ for A..Z

Try it online!

Xcali

Posted 2018-01-05T04:42:52.560

Reputation: 7 671

0

C,  84  81 bytes

Thanks to @ceilingcat for saving three bytes!

f(i,j){for(i=1;i++<27;puts(""))for(j=51;j<63-~i/2;)putchar(j++<64-i/2?32:j+i/2);}

Try it online!

Unrolled:

f(i, j) // Using the argument list only to declare the variables.
{
    for (i=1; i++<27; puts(""))
        for (j=51; j<63-~i/2;)
            putchar(j++<64-i/2 ? 32 : j+i/2);
}

Steadybox

Posted 2018-01-05T04:42:52.560

Reputation: 15 798

0

Jelly, 12 bytes

ØAJ’HU⁶ẋż¹ƤY

Try it online!

Uses code very similar to the linked question (further evidence that it is most likely a dupe).

-3 bytes thanks to FlipTack and Jonathan Allan (though indirectly)

Old version, 15 bytes

12Rx2U⁶ẋżØA¹Ƥ¤Y

Try it online!

caird coinheringaahing

Posted 2018-01-05T04:42:52.560

Reputation: 13 702

You may want to look at this answer, though I suspect this question will soon be closed as a duplicate of that one anyway.

– FlipTack – 2018-01-05T07:53:26.483

@FlipTack I've already VTC'ed, and thanks for the byte save! – caird coinheringaahing – 2018-01-05T08:03:41.630