Look at the following string. Notice a pattern?
ABEFNOPEFGH DC G Q I M H R J LKJI S K D T L C U M BAZYXWV N E O D P C Q BAZYXWVUTSR
As some might have noticed, it's basically a spiral of the alphabet, where the distances between rows / columns are gradually increasing by 1 space / newline.
- Let's have a counter c, which is initially 0.
- We write out the first c + 1 letters of the alphabet from left to right:
Then, from top to bottom the next (c + 1)(c + 2)/2 letters (add
From left to right, the next (c + 1)(c + 2)/2 (add
And from bottom to top, the next c + 1 letters (add
Reached the end of the cycle. Hence, let's increment c (which becomes 1). Then, it starts back from the first step, the only difference being that instead of using the first c + 1 letters of the alphabet, we use the next c + 1 letters, starting from the last element of this cycle (
Din this case, so we continue with
Zis reached, it cycles back from
Given an integer N (which is positive for 1-indexing or non-negative for 0-indexing), output the first N cycles of the spiral.
You can either use the lowercase or the uppercase alphabet, but your choice must be consistent (only use one of them, mixing is not allowed).
Acceptable output formats: multiline string, a list of strings representing lines, a list containing multiple lists of characters, each representing one line, or anything else you find suitable. In case you don't choose the first format, it would be nice to include a pretty-print version of your code.
This is code-golf, so the shortest code in bytes (in each language) which fulfils the requirements wins!
The input integer will be separated by its corresponding output through a newline, and the tests will be separated using dashes. Note that these are 1-indexed.
1 AB DC -------- 2 ABEF DC G M H LKJI -------- 3 ABEFNOP DC G Q M H R LKJI S D T C U BAZYXWV ------- 4 ABEFNOPEFGH DC G Q I M H R J LKJI S K D T L C U M BAZYXWV N E O D P C Q BAZYXWVUTSR ------- 5 ABEFNOPEFGHFGHIJ DC G Q I K M H R J L LKJI S K M D T L N C U M O BAZYXWV N P E O Q D P R C Q S BAZYXWVUTSR T R U Q V P W O X NMLKJIHGFEDCBAZY ------ 6 ABEFNOPEFGHFGHIJSTUVWX DC G Q I K Y M H R J L Z LKJI S K M A D T L N B C U M O C BAZYXWV N P D E O Q E D P R F C Q S G BAZYXWVUTSR T H R U I Q V J P W K O X L NMLKJIHGFEDCBAZY M S N R O Q P P Q O R NMLKJIHGFEDCBAZYXWVUTS