4
\$\begingroup\$

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 = "";
    } 
}
\$\endgroup\$
  • 3
    \$\begingroup\$ Welcome 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. \$\endgroup\$ – LiefdeWen Jan 5 '18 at 4:49
  • 1
    \$\begingroup\$ Just 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). \$\endgroup\$ – user202729 Jan 5 '18 at 5:31
  • \$\begingroup\$ @user202729 But but O(N) is O(1) when N is small. \$\endgroup\$ – LiefdeWen Jan 5 '18 at 5:37
  • 2
    \$\begingroup\$ No one tell Leaky that alphabet challenges are back \$\endgroup\$ – caird coinheringaahing Jan 5 '18 at 6:43
  • 1
    \$\begingroup\$ Is lower case alphabet okay as well? \$\endgroup\$ – Emigna Jan 5 '18 at 7:18
5
\$\begingroup\$

Ruby, 44 bytes

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

Try it online!

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

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

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
\$\endgroup\$
  • 1
    \$\begingroup\$ You know your languages is built for kolmo challenges when you have a 'center horizontally' command. \$\endgroup\$ – caird coinheringaahing Jan 5 '18 at 6:39
1
\$\begingroup\$

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)))
\$\endgroup\$
  • \$\begingroup\$ Can you use a function with a dummy argument (_=>) to save a byte? \$\endgroup\$ – caird coinheringaahing Jan 5 '18 at 6:41
  • \$\begingroup\$ thats what I usually do but OP said "No inputs" so I thought lets honor that requirement. \$\endgroup\$ – LiefdeWen Jan 5 '18 at 7:10
1
\$\begingroup\$

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.

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

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
\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – Taylor Scott Jan 11 '18 at 14:51
  • \$\begingroup\$ Noted and Thank you. :) Will apply it for future challenge :) \$\endgroup\$ – remoel Jan 12 '18 at 7:14
0
\$\begingroup\$

Perl 5, 32 bytes

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

Try it online!

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

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!

\$\endgroup\$
  • \$\begingroup\$ You may want to look at this answer, though I suspect this question will soon be closed as a duplicate of that one anyway. \$\endgroup\$ – FlipTack Jan 5 '18 at 7:53
  • \$\begingroup\$ @FlipTack I've already VTC'ed, and thanks for the byte save! \$\endgroup\$ – caird coinheringaahing Jan 5 '18 at 8:03
0
\$\begingroup\$

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);
}
\$\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.