Skip to content

Commit 77f5cb3

Browse files
authored
Add saddle-points (#384)
* Add `saddle-points` * Adjust formatting for expected results
1 parent abb5b28 commit 77f5cb3

File tree

8 files changed

+200
-0
lines changed

8 files changed

+200
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,14 @@
519519
"prerequisites": [],
520520
"difficulty": 2
521521
},
522+
{
523+
"slug": "saddle-points",
524+
"name": "Saddle Points",
525+
"uuid": "3abe4014-3f1d-4178-9b3a-bc0fff0f1322",
526+
"practices": [],
527+
"prerequisites": [],
528+
"difficulty": 2
529+
},
522530
{
523531
"slug": "scrabble-score",
524532
"name": "Scrabble Score",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Instructions
2+
3+
Your task is to find the potential trees where you could build your tree house.
4+
5+
The data company provides the data as grids that show the heights of the trees.
6+
The rows of the grid represent the east-west direction, and the columns represent the north-south direction.
7+
8+
An acceptable tree will be the largest in its row, while being the smallest in its column.
9+
10+
A grid might not have any good trees at all.
11+
Or it might have one, or even several.
12+
13+
Here is a grid that has exactly one candidate tree.
14+
15+
```text
16+
17+
1 2 3 4
18+
|-----------
19+
1 | 9 8 7 8
20+
→ 2 |[5] 3 2 4
21+
3 | 6 6 7 1
22+
```
23+
24+
- Row 2 has values 5, 3, 2, and 4. The largest value is 5.
25+
- Column 1 has values 9, 5, and 6. The smallest value is 5.
26+
27+
So the point at `[2, 1]` (row: 2, column: 1) is a great spot for a tree house.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Introduction
2+
3+
You plan to build a tree house in the woods near your house so that you can watch the sun rise and set.
4+
5+
You've obtained data from a local survey company that show the height of every tree in each rectangular section of the map.
6+
You need to analyze each grid on the map to find good trees for your tree house.
7+
8+
A good tree is both:
9+
10+
- taller than every tree to the east and west, so that you have the best possible view of the sunrises and sunsets.
11+
- shorter than every tree to the north and south, to minimize the amount of tree climbing.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"saddle-points.coffee"
8+
],
9+
"test": [
10+
"saddle-points.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Detect saddle points in a matrix.",
17+
"source": "J Dalbey's Programming Practice problems",
18+
"source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html"
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SaddlePoints = (matrix) ->
2+
return [] if matrix.length == 0
3+
4+
rows = matrix.length
5+
columns = matrix[0].length
6+
7+
tallestInEachRow = matrix.map (row) => Math.max row...
8+
shortestInEachColumn = [0...columns].map (c) ->
9+
Math.min (matrix[r][c] for r in [0...rows])...
10+
11+
saddlePoints = []
12+
for i in [0...rows]
13+
for j in [0...columns]
14+
current = matrix[i][j]
15+
if current == tallestInEachRow[i] and current == shortestInEachColumn[j]
16+
saddlePoints.push {row: i + 1, column: j + 1}
17+
saddlePoints
18+
19+
module.exports = SaddlePoints
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[3e374e63-a2e0-4530-a39a-d53c560382bd]
13+
description = "Can identify single saddle point"
14+
15+
[6b501e2b-6c1f-491f-b1bb-7f278f760534]
16+
description = "Can identify that empty matrix has no saddle points"
17+
18+
[8c27cc64-e573-4fcb-a099-f0ae863fb02f]
19+
description = "Can identify lack of saddle points when there are none"
20+
21+
[6d1399bd-e105-40fd-a2c9-c6609507d7a3]
22+
description = "Can identify multiple saddle points in a column"
23+
24+
[3e81dce9-53b3-44e6-bf26-e328885fd5d1]
25+
description = "Can identify multiple saddle points in a row"
26+
27+
[88868621-b6f4-4837-bb8b-3fad8b25d46b]
28+
description = "Can identify saddle point in bottom right corner"
29+
30+
[5b9499ca-fcea-4195-830a-9c4584a0ee79]
31+
description = "Can identify saddle points in a non square matrix"
32+
33+
[ee99ccd2-a1f1-4283-ad39-f8c70f0cf594]
34+
description = "Can identify that saddle points in a single column matrix are those with the minimum value"
35+
36+
[63abf709-a84b-407f-a1b3-456638689713]
37+
description = "Can identify that saddle points in a single row matrix are those with the maximum value"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SaddlePoints = (matrix) ->
2+
3+
module.exports = SaddlePoints
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
SaddlePoints = require './saddle-points'
2+
3+
describe 'SaddlePoints', ->
4+
it 'can identify single saddle point', ->
5+
matrix = [
6+
[9, 8, 7]
7+
[5, 3, 2]
8+
[6, 6, 7]]
9+
results = SaddlePoints matrix
10+
expect(results.sort()).toEqual [{row: 2, column: 1}]
11+
12+
xit 'can identify that empty matrix has no saddle points', ->
13+
matrix = [[]]
14+
results = SaddlePoints matrix
15+
expect(results.sort()).toEqual []
16+
17+
xit 'can identify lack of saddle points when there are none', ->
18+
matrix = [
19+
[1, 2, 3]
20+
[3, 1, 2]
21+
[2, 3, 1]]
22+
results = SaddlePoints matrix
23+
expect(results.sort()).toEqual []
24+
25+
xit 'can identify multiple saddle points in a column', ->
26+
matrix = [
27+
[4, 5, 4]
28+
[3, 5, 5]
29+
[1, 5, 4]]
30+
results = SaddlePoints matrix
31+
expect(results.sort()).toEqual [{row: 1, column: 2}
32+
{row: 2, column: 2}
33+
{row: 3, column: 2}]
34+
35+
xit 'can identify multiple saddle points in a row', ->
36+
matrix = [
37+
[6, 7, 8]
38+
[5, 5, 5]
39+
[7, 5, 6]]
40+
results = SaddlePoints matrix
41+
expect(results.sort()).toEqual [{row: 2, column: 1}
42+
{row: 2, column: 2}
43+
{row: 2, column: 3}]
44+
45+
xit 'can identify saddle point in bottom right corner', ->
46+
matrix = [
47+
[8, 7, 9]
48+
[6, 7, 6]
49+
[3, 2, 5]]
50+
results = SaddlePoints matrix
51+
expect(results.sort()).toEqual [{row: 3, column: 3}]
52+
53+
xit 'can identify saddle points in a non square matrix', ->
54+
matrix = [
55+
[3, 1, 3]
56+
[3, 2, 4]]
57+
results = SaddlePoints matrix
58+
expect(results.sort()).toEqual [{row: 1, column: 1}
59+
{row: 1, column: 3}]
60+
61+
xit 'can identify that saddle points in a single column matrix are those with the minimum value', ->
62+
matrix = [
63+
[2]
64+
[1]
65+
[4]
66+
[1]]
67+
results = SaddlePoints matrix
68+
expect(results.sort()).toEqual [{row: 2, column: 1}
69+
{row: 4, column: 1}]
70+
71+
xit 'can identify that saddle points in a single row matrix are those with the maximum value', ->
72+
matrix = [[2, 5, 3, 5]]
73+
results = SaddlePoints matrix
74+
expect(results.sort()).toEqual [{row: 1, column: 2}
75+
{row: 1, column: 4}]
76+

0 commit comments

Comments
 (0)