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 = "";
}
}
String
instead ofStringBuilder
in Java may cause bad performance. It's better useStringBuilder
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\$