Task
Given a non-empty string of lowercase ASCII letters a
-z
, take its first character, and:
- Surround it with a square of copies of the second character,
- Surround that with a diamond of copies of the third character,
- Surround that with a square of copies of the fourth character…
…alternating between square and diamond borders until the end of the string. Empty space between the borders should be represented by ASCII spaces ().
To add a square border, draw a square exactly around the entire current “working array”:
sssssssssss
t s t s
t t s t t s
t t s t t s
t aaa t s t aaa t s
t aca t => st aca ts
t aaa t s t aaa t s
t t s t t s
t t s t t s
t s t s
sssssssssss
To add a diamond border, draw a centered diamond shape that touches the outermost square diagonally, but not orthogonally:
s
s s
s s
s s
s s
s s
s s
wwwwwwwwwww s wwwwwwwwwww s
w o w s w o w s
w o o w s w o o w s
w o o w s w o o w s
w o eee o w s w o eee o w s
wo eme ow => s wo eme ow s
w o eee o w s w o eee o w s
w o o w s w o o w s
w o o w s w o o w s
w o w s w o w s
wwwwwwwwwww s wwwwwwwwwww s
s s
s s
s s
s s
s s
s s
s
Your program must output the final array.
- Each line may contain any amount of trailing spaces.
- You may output a list of strings representing lines, or a single newline-separated string with an optional trailing newline.
- Leading/trailing blank lines are disallowed.
- Leading columns of spaces are also disallowed.
This is code-golf. The shortest code in bytes wins.
Test cases
The above examples are expected outputs for cat
, cats
, meow
, and meows
, in reading order. Some other cases worth handling:
For the input
a
, your program should output:a
For the input
ab
, your program should output:bbb bab bbb
For the input
codegolf
, your program should output:fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff f l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l ooooooooooooooooooooooooooo l f f l o g o l f f l o g g o l f f l o g g o l f f l o g g o l f f l o g g o l f f l o g g o l f f l o g g o l f f l o g eeeeeeeeeee g o l f f l o g e d e g o l f f l o g e d d e g o l f f l o g e d d e g o l f f l o g e d ooo d e g o l f fl og ed oco de go lf f l o g e d ooo d e g o l f f l o g e d d e g o l f f l o g e d d e g o l f f l o g e d e g o l f f l o g eeeeeeeeeee g o l f f l o g g o l f f l o g g o l f f l o g g o l f f l o g g o l f f l o g g o l f f l o g g o l f f l o g o l f f l ooooooooooooooooooooooooooo l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l l f f l f fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Reference implementation
I wrote some Julia code that your program’s output should match (at least visually). Try it online!