Spiral/README.md

41 lines
1.1 KiB
Markdown
Raw Normal View History

2019-07-22 20:31:43 -04:00
# Spiral
bad attempts at solving interview questions
## The Problem - Verbatim
Write a small program which:
2019-07-22 20:33:08 -04:00
2019-07-22 20:31:43 -04:00
accepts on the command line an odd number N
2019-07-22 20:33:08 -04:00
2019-07-22 20:31:43 -04:00
outputs a sequence of numbers corresponding to the following steps:
2019-07-22 20:33:08 -04:00
2019-07-22 20:31:43 -04:00
the numbers 1 through N^2 are written in a grid
2019-07-22 20:33:08 -04:00
2019-07-22 20:31:43 -04:00
the first number in the sequence is the number in the center of the grid
2019-07-22 20:33:08 -04:00
2019-07-22 20:31:43 -04:00
the subsequent numbers are found by spiraling out clockwise, starting to the right
2019-07-22 20:33:08 -04:00
2019-07-22 20:31:43 -04:00
For example, for N = 3 we have the grid:
2019-07-22 20:33:08 -04:00
```
2019-07-22 20:31:43 -04:00
1 2 3
4 5 6
7 8 9
2019-07-22 20:33:08 -04:00
```
2019-07-22 20:31:43 -04:00
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 -02 -Wall Spiral.hs -o spiral
2019-07-22 20:33:08 -04:00
./spiral
2019-07-22 20:31:43 -04:00
```