Skip to content

Commit 0bd686d

Browse files
authored
Add Saddle Points (#121)
1 parent d2e5e1e commit 0bd686d

8 files changed

Lines changed: 352 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@
386386
"prerequisites": [],
387387
"difficulty": 1
388388
},
389+
{
390+
"slug": "saddle-points",
391+
"name": "Saddle Points",
392+
"uuid": "04348df7-d82a-406b-8527-64e82a835fcd",
393+
"practices": [],
394+
"prerequisites": [],
395+
"difficulty": 5
396+
},
389397
{
390398
"slug": "scrabble-score",
391399
"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+
"ricardomaps"
4+
],
5+
"files": {
6+
"solution": [
7+
"saddle-points.nix"
8+
],
9+
"test": [
10+
"saddle-points-test.nix"
11+
],
12+
"example": [
13+
".meta/example.nix"
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
let
2+
inherit (import <nixpkgs/lib>)
3+
cartesianProduct
4+
min
5+
max
6+
range
7+
;
8+
transpose =
9+
matrix:
10+
let
11+
colTotal = builtins.length (builtins.head matrix);
12+
in
13+
builtins.genList (i: map (row: builtins.elemAt row i) matrix) colTotal;
14+
minimum = xs: builtins.foldl' min (builtins.head xs) xs;
15+
maximum = xs: builtins.foldl' max (builtins.head xs) xs;
16+
in
17+
{
18+
saddlePoints =
19+
matrix:
20+
let
21+
maxPerRow = map maximum matrix;
22+
minPerCol = map minimum (transpose matrix);
23+
indices = cartesianProduct {
24+
row = range 1 (builtins.length matrix);
25+
column = range 1 (builtins.length (builtins.head matrix));
26+
};
27+
in
28+
builtins.filter (
29+
{ row, column }:
30+
let
31+
maxInRow = builtins.elemAt maxPerRow (row - 1);
32+
minInCol = builtins.elemAt minPerCol (column - 1);
33+
element = builtins.elemAt (builtins.elemAt matrix (row - 1)) (column - 1);
34+
in
35+
element == maxInRow && element == minInCol
36+
) indices;
37+
}
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: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
let
2+
inherit (import ./saddle-points.nix) saddlePoints;
3+
in
4+
{
5+
"test can identify single saddle point" = {
6+
expr = saddlePoints [
7+
[
8+
9
9+
8
10+
7
11+
]
12+
[
13+
5
14+
3
15+
2
16+
]
17+
[
18+
6
19+
6
20+
7
21+
]
22+
];
23+
expected = [
24+
{
25+
row = 2;
26+
column = 1;
27+
}
28+
];
29+
};
30+
"test can identify that empty matrix has no saddle points" = {
31+
expr = saddlePoints [
32+
[ ]
33+
];
34+
expected = [ ];
35+
};
36+
"test can identify lack of saddle points when there are none" = {
37+
expr = saddlePoints [
38+
[
39+
1
40+
2
41+
3
42+
]
43+
[
44+
3
45+
1
46+
2
47+
]
48+
[
49+
2
50+
3
51+
1
52+
]
53+
];
54+
expected = [ ];
55+
};
56+
"test can identify multiple saddle points in a column" = {
57+
expr = saddlePoints [
58+
[
59+
4
60+
5
61+
4
62+
]
63+
[
64+
3
65+
5
66+
5
67+
]
68+
[
69+
1
70+
5
71+
4
72+
]
73+
];
74+
expected = [
75+
{
76+
row = 1;
77+
column = 2;
78+
}
79+
{
80+
row = 2;
81+
column = 2;
82+
}
83+
{
84+
row = 3;
85+
column = 2;
86+
}
87+
];
88+
};
89+
"test can identify multiple saddle points in a row" = {
90+
expr = saddlePoints [
91+
[
92+
6
93+
7
94+
8
95+
]
96+
[
97+
5
98+
5
99+
5
100+
]
101+
[
102+
7
103+
5
104+
6
105+
]
106+
];
107+
expected = [
108+
{
109+
row = 2;
110+
column = 1;
111+
}
112+
{
113+
row = 2;
114+
column = 2;
115+
}
116+
{
117+
row = 2;
118+
column = 3;
119+
}
120+
];
121+
};
122+
"test can identify saddle point in bottom right corner" = {
123+
expr = saddlePoints [
124+
[
125+
8
126+
7
127+
9
128+
]
129+
[
130+
6
131+
7
132+
6
133+
]
134+
[
135+
3
136+
2
137+
5
138+
]
139+
];
140+
expected = [
141+
{
142+
row = 3;
143+
column = 3;
144+
}
145+
];
146+
};
147+
"test can identify saddle points in a non square matrix" = {
148+
expr = saddlePoints [
149+
[
150+
3
151+
1
152+
3
153+
]
154+
[
155+
3
156+
2
157+
4
158+
]
159+
];
160+
expected = [
161+
{
162+
row = 1;
163+
column = 1;
164+
}
165+
{
166+
row = 1;
167+
column = 3;
168+
}
169+
];
170+
};
171+
"test can identify that saddle points in a single column matrix are those with the minimum value" =
172+
{
173+
expr = saddlePoints [
174+
[ 2 ]
175+
[ 1 ]
176+
[ 4 ]
177+
[ 1 ]
178+
];
179+
expected = [
180+
{
181+
row = 2;
182+
column = 1;
183+
}
184+
{
185+
row = 4;
186+
column = 1;
187+
}
188+
];
189+
};
190+
"test can identify that saddle points in a single row matrix are those with the maximum value" = {
191+
expr = saddlePoints [
192+
[
193+
2
194+
5
195+
3
196+
5
197+
]
198+
];
199+
expected = [
200+
{
201+
row = 1;
202+
column = 2;
203+
}
204+
{
205+
row = 1;
206+
column = 4;
207+
}
208+
];
209+
};
210+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
saddlePoints = matrix: throw "You need to implement this function.";
3+
}

0 commit comments

Comments
 (0)