Skip to content

Commit afa8b03

Browse files
authored
Merge pull request #308 from exercism/saddle-points
Add saddle-points practice exercise
2 parents 04a494e + 9dbb9cb commit afa8b03

File tree

9 files changed

+887
-0
lines changed

9 files changed

+887
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,14 @@
721721
"practices": [],
722722
"prerequisites": [],
723723
"difficulty": 5
724+
},
725+
{
726+
"slug": "saddle-points",
727+
"name": "Saddle Points",
728+
"uuid": "2ffec6c9-0daa-4248-ad2a-e9642254a9cf",
729+
"practices": [],
730+
"prerequisites": [],
731+
"difficulty": 4
724732
}
725733
],
726734
"foregone": [
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+
"rabestro"
4+
],
5+
"files": {
6+
"solution": [
7+
"saddle-points.awk"
8+
],
9+
"test": [
10+
"test-saddle-points.bats"
11+
],
12+
"example": [
13+
".meta/example.awk"
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
NR == 1 {
2+
Cols = NF
3+
for (i = 1; i<= Cols; i++) {
4+
ColsMinimum[i] = $i
5+
}
6+
}
7+
{
8+
row_max = $1
9+
for (i = 2; i <= Cols; i++) {
10+
if ($i > row_max) row_max = $i
11+
}
12+
RowsMaximum[NR] = row_max
13+
14+
for (i = 1; i <= Cols; i++) {
15+
if ($i < ColsMinimum[i]) {
16+
ColsMinimum[i] = $i
17+
}
18+
}
19+
}
20+
END {
21+
for (row = 1; row <= NR; row++) {
22+
for (col = 1; col <= Cols; col++) {
23+
if (RowsMaximum[row] == ColsMinimum[col]) {
24+
print row, col
25+
}
26+
}
27+
}
28+
}
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"

0 commit comments

Comments
 (0)