By: G.A.JAGUAR
Given a bidimentional array n * m
, develop a function that generates an
unidimentional array 1 * nm
which elements correspond with original array's
elements obtained by following a clockwise snail trajectory.
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ],
]
Output:
[ 1, 2, 3, 6, 9, 8, 7, 4, 5 ]
There is a four basic movements that can be performed by the snail: right, down, left and up (in that order, I mean clockwise). Each lap of this curious animal will reduce the distance by one.
This movements will be repeated until the snail reaches the last element of original array.
On each step of our dear friend, it will store the current value into a new array, at the end it will return this last one.
- Begin.
- Read
array
- Verify if
array
has two rows at least. - Verify if each row has two columns at least.
- Verify if all rows are of the same size.
- Verify if all items are integers.
- Initialize
result
array. - Initialize
rows
with the number of rows ofarray
. - Initialize
columns
with the number of columns ofarray
. - Calculate
total
=rows
*columns
. - Initialize
lap
counter at0
. - Repeat next 4 steps until
result
size =total
then jump to step 17: - Move to right and store into
result
the current value on every step. - Move to down and store into
result
the current value on every step. - Move to left and store into
result
the current value on every step. - Move to up and store into
result
the current value on every step. - Add
1
tolap
. - Return
result
. - End.
A implementation of this algorithm is available here.
In order to it works properly, first copy
.env.example
file as.env
.
# build docker container.
sh scripts/install
# execute the script.
sh scripts/run
# create a docker image
docker-compose build
# build for execution
docker-compose run --rm dev
# on linux os:
python3 app/main.py
# on widows os:
py app\main.py