-
-
Notifications
You must be signed in to change notification settings - Fork 151
Add saddle-points exercice
#1006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
resu-xuniL
wants to merge
2
commits into
exercism:main
Choose a base branch
from
resu-xuniL:saddle-points
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+386
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Instructions | ||
|
|
||
| Your task is to find the potential trees where you could build your tree house. | ||
|
|
||
| The data company provides the data as grids that show the heights of the trees. | ||
| The rows of the grid represent the east-west direction, and the columns represent the north-south direction. | ||
|
|
||
| An acceptable tree will be the largest in its row, while being the smallest in its column. | ||
|
|
||
| A grid might not have any good trees at all. | ||
| Or it might have one, or even several. | ||
|
|
||
| Here is a grid that has exactly one candidate tree. | ||
|
|
||
| ```text | ||
| ↓ | ||
| 1 2 3 4 | ||
| |----------- | ||
| 1 | 9 8 7 8 | ||
| → 2 |[5] 3 2 4 | ||
| 3 | 6 6 7 1 | ||
| ``` | ||
|
|
||
| - Row 2 has values 5, 3, 2, and 4. The largest value is 5. | ||
| - Column 1 has values 9, 5, and 6. The smallest value is 5. | ||
|
|
||
| So the point at `[2, 1]` (row: 2, column: 1) is a great spot for a tree house. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Introduction | ||
|
|
||
| You plan to build a tree house in the woods near your house so that you can watch the sun rise and set. | ||
|
|
||
| You've obtained data from a local survey company that show the height of every tree in each rectangular section of the map. | ||
| You need to analyze each grid on the map to find good trees for your tree house. | ||
|
|
||
| A good tree is both: | ||
|
|
||
| - taller than every tree to the east and west, so that you have the best possible view of the sunrises and sunsets. | ||
| - shorter than every tree to the north and south, to minimize the amount of tree climbing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "authors": [ | ||
| "resu-xuniL" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "SaddlePoints.php" | ||
| ], | ||
| "test": [ | ||
| "SaddlePointsTest.php" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.php" | ||
| ] | ||
| }, | ||
| "blurb": "Detect saddle points in a matrix.", | ||
| "source": "J Dalbey's Programming Practice problems", | ||
| "source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| class SaddlePoints | ||
| { | ||
| public function saddlePoints(array $matrix): array | ||
| { | ||
| if (empty($matrix[0])) { | ||
| return []; | ||
| } | ||
|
|
||
| $trees = []; | ||
| $maxs = array_map('max', array_map(null, $matrix)); | ||
|
|
||
| if (count($matrix) > 1) { | ||
| $mins = array_map('min', array_map(null, ...$matrix)); | ||
| } else { | ||
| $mins = $matrix[0]; | ||
| } | ||
|
|
||
| for ($ridx = 0; $ridx < count($matrix); $ridx++) { | ||
| for ($cidx = 0; $cidx < count($matrix[$ridx]); $cidx++) { | ||
| if ($maxs[$ridx] === $mins[$cidx]) { | ||
| array_push($trees, [ | ||
| 'row' => $ridx + 1, | ||
| 'column' => $cidx + 1 | ||
| ]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $trees; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # 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. | ||
|
|
||
| [3e374e63-a2e0-4530-a39a-d53c560382bd] | ||
| description = "Can identify single saddle point" | ||
|
|
||
| [6b501e2b-6c1f-491f-b1bb-7f278f760534] | ||
| description = "Can identify that empty matrix has no saddle points" | ||
|
|
||
| [8c27cc64-e573-4fcb-a099-f0ae863fb02f] | ||
| description = "Can identify lack of saddle points when there are none" | ||
|
|
||
| [6d1399bd-e105-40fd-a2c9-c6609507d7a3] | ||
| description = "Can identify multiple saddle points in a column" | ||
|
|
||
| [3e81dce9-53b3-44e6-bf26-e328885fd5d1] | ||
| description = "Can identify multiple saddle points in a row" | ||
|
|
||
| [88868621-b6f4-4837-bb8b-3fad8b25d46b] | ||
| description = "Can identify saddle point in bottom right corner" | ||
|
|
||
| [5b9499ca-fcea-4195-830a-9c4584a0ee79] | ||
| description = "Can identify saddle points in a non square matrix" | ||
|
|
||
| [ee99ccd2-a1f1-4283-ad39-f8c70f0cf594] | ||
| description = "Can identify that saddle points in a single column matrix are those with the minimum value" | ||
|
|
||
| [63abf709-a84b-407f-a1b3-456638689713] | ||
| description = "Can identify that saddle points in a single row matrix are those with the maximum value" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * By adding type hints and enabling strict type checking, code can become | ||
| * easier to read, self-documenting and reduce the number of potential bugs. | ||
| * By default, type declarations are non-strict, which means they will attempt | ||
| * to change the original type to match the type specified by the | ||
| * type-declaration. | ||
| * | ||
| * In other words, if you pass a string to a function requiring a float, | ||
| * it will attempt to convert the string value to a float. | ||
| * | ||
| * To enable strict mode, a single declare directive must be placed at the top | ||
| * of the file. | ||
| * This means that the strictness of typing is configured on a per-file basis. | ||
| * This directive not only affects the type declarations of parameters, but also | ||
| * a function's return type. | ||
| * | ||
| * For more info review the Concept on strict type checking in the PHP track | ||
| * <link>. | ||
| * | ||
| * To disable strict typing, comment out the directive below. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| class SaddlePoints | ||
| { | ||
| public function saddlePoints(array $matrix): array | ||
| { | ||
| throw new \BadMethodCallException("Implement the saddlePoints method"); | ||
| } | ||
| } | ||
|
Comment on lines
+27
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This definitely is a functional transformer function :) So please change the interface to a simple function. You can keep the name and signature of the method. |
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd go for difficulty
3here. It's not very easy and mostly filtering of values from arrays.