In this challenge, transform a string into a spiral contained inside a regular square matrix. To build the matrix, you are given the dimension of its side:
// "x" represents the matrix center
side = 3 (odd)
matrix = [
[" ", " ", " "],
[" ", "x", " "],
[" ", " ", " "]
]
side = 4 (even)
matrix = [
[" ", " ", " ", " "],
[" ", "x", " ", " "],
[" ", " ", " ", " "],
[" ", " ", " ", " "],
]
The length of the string has to match exactly the number of cells inside the matrix:
"+" to the end of the string until its length match the number of cells.side = 3 (9 cells)
string = "INNOKODAKADEMIJATEROUS"
// You'll need only "INNOKODAKADEMIJATER", while "OUS" is discarded.
string = "INNOKODAKADEMIJATER"
side = 4 (16 cells)
string = "INNOKODAKADEMIJATEROUS"
// You'll need all the string plus 4 "+" to match the cells.
string = "INNOKODAKADEMIJATEROUS++++"
Starting from the center that you found, you have to fill a regular square matrix side * side placing the characters of the given string str, following a clockwise spiral pattern (first move to the right).
side = 3 (odd)
string = "INNOKODAKADEMIJATEROUS"
matrix = [
["T", "E", "R"],
["T", "E", "D"],
["I", "B", "A"]
]
side = 4 (even)
string = "INNOKODAKADEMIJATEROUS"
matrix = [
["T", "E", "R", "O"],
["T", "E", "D", "U"],
["I", "B", "A", "S"],
["+", "+", "+", "+"],
]
spiralMatrix(2, "DOG") ➞ [
["D", "O"],
["+", "G"]
]
spiralMatrix(3, "COPYRIGHTS") ➞ [
["G", "H", "T"],
["I", "C", "O"],
["R", "Y", "P"]
]
spiralMatrix(4, "SUPERLUMBERJACK") ➞ [
["U", "M", "B", "E"],
["L", "S", "U", "R"],
["R", "E", "P", "J"],
["+", "K", "C", "A"]
]
side, you can expect any valid value greater than 1.