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 = "";
}
}
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 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). – 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