Skip to content

Commit

Permalink
Add spiral matrix (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakelewis authored Feb 27, 2024
1 parent 5c8fa4f commit e178b7b
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "spiral-matrix",
"name": "Spiral Matrix",
"uuid": "7d5230f7-6aa2-4d57-9618-83c509630360",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
]
},
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/spiral-matrix/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Instructions

Given the size, return a square matrix of numbers in spiral order.

The matrix should be filled with natural numbers, starting from 1 in the top-left corner, increasing in an inward, clockwise spiral order, like these examples:

## Examples

### Spiral matrix of size 3

```text
1 2 3
8 9 4
7 6 5
```

### Spiral matrix of size 4

```text
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
```
17 changes: 17 additions & 0 deletions exercises/practice/spiral-matrix/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["blakelewis"],
"files": {
"solution": [
"spiral-matrix.rkt"
],
"test": [
"spiral-matrix-test.rkt"
],
"example": [
".meta/example.rkt"
]
},
"blurb": "Given the size, return a square matrix of numbers in spiral order.",
"source": "Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension.",
"source_url": "https://web.archive.org/web/20230607064729/https://old.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/"
}
28 changes: 28 additions & 0 deletions exercises/practice/spiral-matrix/.meta/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#lang racket

(provide spiral-matrix)

(define (extract-row r coord)
(let* ([row (filter (λ (c) (= (caar c) r)) coord)]
[sorted-row (sort row < #:key cdar)])
(map cdr sorted-row)))

(define (take-step x y dx dy n h)
(let ([x1 (+ x dx)] [y1 (+ y dy)])
(if (or (negative? x1) (negative? y1)
(= x1 n) (= y1 n) (hash-has-key? h (cons y1 x1)))
(values (- x dy) (+ y dx) (- dy) dx)
(values x1 y1 dx dy))))

(define (coordinates n)
(let ([h (make-hash)])
(for/fold
([x 0] [y 0] [dx 1] [dy 0] #:result (hash->list h))
([i (in-inclusive-range 1 (* n n))])
(hash-set! h (cons y x) i)
(take-step x y dx dy n h))))

(define (spiral-matrix n)
(let ([coord (coordinates n)])
(for/list ([i (in-range n)])
(extract-row i coord))))
28 changes: 28 additions & 0 deletions exercises/practice/spiral-matrix/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[8f584201-b446-4bc9-b132-811c8edd9040]
description = "empty spiral"

[e40ae5f3-e2c9-4639-8116-8a119d632ab2]
description = "trivial spiral"

[cf05e42d-eb78-4098-a36e-cdaf0991bc48]
description = "spiral of size 2"

[1c475667-c896-4c23-82e2-e033929de939]
description = "spiral of size 3"

[05ccbc48-d891-44f5-9137-f4ce462a759d]
description = "spiral of size 4"

[f4d2165b-1738-4e0c-bed0-c459045ae50d]
description = "spiral of size 5"
46 changes: 46 additions & 0 deletions exercises/practice/spiral-matrix/spiral-matrix-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#lang racket/base

(require "spiral-matrix.rkt")

(module+ test
(require rackunit rackunit/text-ui))

(module+ test
(define suite
(test-suite "spiral matrix tests"
(test-equal? "empty spiral"
(spiral-matrix 0)
'())

(test-equal? "trivial spiral"
(spiral-matrix 1)
'((1)))

(test-equal? "spiral of size 2"
(spiral-matrix 2)
'((1 2)
(4 3)))

(test-equal? "spiral of size 3"
(spiral-matrix 3)
'((1 2 3)
(8 9 4)
(7 6 5)))

(test-equal? "spiral of size 4"
(spiral-matrix 4)
'((1 2 3 4)
(12 13 14 5)
(11 16 15 6)
(10 9 8 7)))

(test-equal? "spiral of size 5"
(spiral-matrix 5)
'((1 2 3 4 5)
(16 17 18 19 6)
(15 24 25 20 7)
(14 23 22 21 8)
(13 12 11 10 9)))))

(run-tests suite))

6 changes: 6 additions & 0 deletions exercises/practice/spiral-matrix/spiral-matrix.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#lang racket

(provide spiral-matrix)

(define (spiral-matrix n)
(error "Not implemented yet"))

0 comments on commit e178b7b

Please sign in to comment.