interview question
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1.1KB

Spiral

bad attempts at solving interview questions

The Problem - Verbatim

Write a small program which:

accepts on the command line an odd number N

outputs a sequence of numbers corresponding to the following steps:

the numbers 1 through N^2 are written in a grid

the first number in the sequence is the number in the center of the grid

the subsequent numbers are found by spiraling out clockwise, starting to the right

For example, for N = 3 we have the grid:

1 2 3
4 5 6
7 8 9

So we start with 5, head right to 6, then down to 9, left through 8 and 7, up through 4 and 1 and finally right through 2 and 3. This gives the output: 5, 6, 9, 8, 7, 4, 1, 2, 3, end

For N=5 the output should be: 13, 14, 19, 18, 17, 12, 7, 8, 9, 10, 15, 20, 25, 24, 23, 22, 21, 16, 11, 6, 1, 2, 3, 4, 5, end

IMPORTANT: your solution should not make use of arrays or matrices of numbers. If you are unable to determine a way to solve it without arrays/matrices then a solution with them is better than no solution.

Compiling and Running

ghc -O2 -Wall Spiral.hs -o spiral
./spiral