Skip to content

Commit f93d953

Browse files
Add saddle-points exercise (#64)
1 parent 48b614d commit f93d953

File tree

8 files changed

+173
-0
lines changed

8 files changed

+173
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@
313313
"prerequisites": [],
314314
"difficulty": 4
315315
},
316+
{
317+
"slug": "saddle-points",
318+
"name": "Saddle Points",
319+
"uuid": "63af24ea-4dc9-485a-8977-223fe2ee0751",
320+
"practices": [],
321+
"prerequisites": [],
322+
"difficulty": 4
323+
},
316324
{
317325
"slug": "triangle",
318326
"name": "Triangle",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"saddle_points.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def saddle_points [m] [n] (matrix: [m][n]i32): [][2]i32 =
2+
let row_maximums = loop row_maximums = replicate m 0 for i < m do
3+
let greatest = loop greatest = i32.lowest for j < n do
4+
i32.max greatest matrix[i][j]
5+
in
6+
row_maximums with [i] = greatest
7+
in
8+
let column_minimums = loop column_minimums = replicate n 0 for j < n do
9+
let least = loop least = i32.highest for i < m do
10+
i32.min least matrix[i][j]
11+
in
12+
column_minimums with [j] = least
13+
in
14+
let result = loop result = [] for i < m do
15+
let result = loop result = result for j < n do
16+
if matrix[i][j] != row_maximums[i] || matrix[i][j] != column_minimums[j] then result else
17+
result ++ [[1 + i32.i64 i, 1 + i32.i64 j]]
18+
in
19+
result
20+
in
21+
result
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def saddle_points [m] [n] (matrix: [m][n]i32): [][2]i32 = ???
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import "saddle_points"
2+
3+
-- Can identify single saddle point
4+
-- ==
5+
-- input { [[9, 8, 7], [5, 3, 2], [6, 6, 7]] }
6+
-- output { [[2, 1]] }
7+
8+
-- Can identify that empty matrix has no saddle points
9+
-- ==
10+
-- input { [empty([0]i32)] }
11+
-- output { empty([0][2]i32) }
12+
13+
-- Can identify lack of saddle points when there are none
14+
-- ==
15+
-- input { [[1, 2, 3], [3, 1, 2], [2, 3, 1]] }
16+
-- output { empty([0][2]i32) }
17+
18+
-- Can identify multiple saddle points in a column
19+
-- ==
20+
-- input { [[4, 5, 4], [3, 5, 5], [1, 5, 4]] }
21+
-- output { [[1, 2], [2, 2], [3, 2]] }
22+
23+
-- Can identify multiple saddle points in a row
24+
-- ==
25+
-- input { [[6, 7, 8], [5, 5, 5], [7, 5, 6]] }
26+
-- output { [[2, 1], [2, 2], [2, 3]] }
27+
28+
-- Can identify saddle point in bottom right corner
29+
-- ==
30+
-- input { [[8, 7, 9], [6, 7, 6], [3, 2, 5]] }
31+
-- output { [[3, 3]] }
32+
33+
-- Can identify saddle points in a non square matrix
34+
-- ==
35+
-- input { [[3, 1, 3], [3, 2, 4]] }
36+
-- output { [[1, 1], [1, 3]] }
37+
38+
-- Can identify that saddle points in a single column matrix are those with the minimum value
39+
-- ==
40+
-- input { [[2], [1], [4], [1]] }
41+
-- output { [[2, 1], [4, 1]] }
42+
43+
-- Can identify that saddle points in a single row matrix are those with the maximum value
44+
-- ==
45+
-- input { [[2, 5, 3, 5]] }
46+
-- output { [[1, 2], [1, 4]] }
47+
48+
let main [m] [n] (matrix: [m][n]i32): [][2]i32 =
49+
saddle_points matrix

0 commit comments

Comments
 (0)