-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Rectangles solution #373
Open
macta
wants to merge
5
commits into
exercism:main
Choose a base branch
from
macta:rectangles-exercise
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.
Open
Rectangles solution #373
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dfb961a
This is quite an interesting problem
macta 8ff9978
Small code cleanups, particularly clarifying that isHorizontal is bet…
macta 6e981eb
Improve isRow comment
macta ffc052c
Refactor for clarity
macta a383dd2
Update class comment to better describe the algoroithm being used in …
macta 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 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,20 @@ | ||
Extension { #name : #Array2D } | ||
|
||
{ #category : #'*Exercise@Rectangles' } | ||
Array2D >> isLineAt: aLineSegment [ | ||
"Answer true, if a LineSegment identifies a valid line in my space. | ||
Lines can be composed of +,-,| characters, anything else is invalid" | ||
|
||
| lineChars | | ||
|
||
lineChars := aLineSegment points | ||
collect: [ :point | self at: point x at: point y ] | ||
thenReject: [ :char | char = $+ ]. | ||
|
||
^ (lineChars | ||
reject: [ :char | | ||
char = | ||
(aLineSegment isRow | ||
ifTrue: [ $- ] | ||
ifFalse: [ $| ]) ]) ifEmpty: [ true ] ifNotEmpty: [ false ] | ||
] |
This file contains 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,17 @@ | ||
Extension { #name : #LineSegment } | ||
|
||
{ #category : #'*Exercise@Rectangles' } | ||
LineSegment >> isRow [ | ||
"Answer true if the segment is on the same row (the internal point x@y is storing row@col)" | ||
|
||
^self start x = self end x | ||
] | ||
|
||
{ #category : #'*Exercise@Rectangles' } | ||
LineSegment >> points [ | ||
"Answer the points either along the same row, or the same column" | ||
|
||
^ self isRow | ||
ifTrue: [ (self start y to: self end y) collect: [ :y | self start x @ y ] ] | ||
ifFalse: [ (self start x to: self end x) collect: [ :x | x @ self start y ] ] | ||
] |
This file contains 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,10 @@ | ||
Extension { #name : #Point } | ||
|
||
{ #category : #'*Exercise@Rectangles' } | ||
Point >> region: aPoint [ | ||
"Answer a Region that encompasses the receiver and aPoint." | ||
|
||
^ Region | ||
topLeft: self | ||
bottomRight: aPoint | ||
] |
This file contains 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,69 @@ | ||
" | ||
Mentor notes: | ||
|
||
- this solution identifies potential rectangles, by searching for + corners in a matrix, deriving valid rectangle permuations from them, and then checking if those derived rectangles actually have lines joining the corners | ||
- this solution had to use a new Region object, as Rectangles optimise their coordinates | ||
" | ||
Class { | ||
#name : #Rectangles, | ||
#superclass : #Object, | ||
#category : #'Exercise@Rectangles' | ||
} | ||
|
||
{ #category : #helper } | ||
Rectangles >> getAllRectangleCornersIn: matrix [ | ||
^ matrix | ||
withIndicesInject: OrderedCollection new | ||
into: [ :result :value :r :c | | ||
value = $+ | ||
ifTrue: [ result add: r @ c ]. | ||
result ] | ||
] | ||
|
||
{ #category : #helper } | ||
Rectangles >> possibleRectangleRegionsIn: matrix [ | ||
"Answer all possible Regions in the matrix, where there could be a rectangle" | ||
|
||
| corners | | ||
corners := self getAllRectangleCornersIn: matrix. | ||
|
||
^ corners | ||
inject: Set new | ||
into: [ :result :corner | | ||
| lowerCorners | | ||
|
||
lowerCorners := self potentialLowerCornersFor: corner given: corners. | ||
|
||
result | ||
addAll: | ||
(lowerCorners | ||
collect: [ :lc | corner region: lc ] | ||
thenSelect: [ :region | region isValid ]); | ||
yourself ] | ||
] | ||
|
||
{ #category : #helper } | ||
Rectangles >> potentialLowerCornersFor: topLeftCorner given: corners [ | ||
^ corners | ||
reject: [ :corner | | ||
corner = topLeftCorner | (corner x < topLeftCorner x) | ||
| (corner y < topLeftCorner y) ] | ||
] | ||
|
||
{ #category : #exercism } | ||
Rectangles >> rectanglesFromStrings: aCollection [ | ||
"Answer the number of rectangles recognized in aCollection of Strings" | ||
|
||
| matrix | | ||
|
||
aCollection ifEmpty: [ ^ 0 ]. | ||
|
||
matrix := Array2D | ||
rows: aCollection size | ||
columns: aCollection first size | ||
contents: (aCollection flatCollect: [ :c | c ]). | ||
|
||
^(self possibleRectangleRegionsIn: matrix) count: [ :region | | ||
region definesRectangleIn: matrix ] | ||
|
||
] |
This file contains 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
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.
this doesn't look like a boolean result (??) Typo 'minus' instead of 'equals' ?
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.
this is a boolean result - are the x values the same - however it did prompt me to change the name as horizontal ness is confusing if x@y are cartesian, where im actually using the point to represent row@column (so a bit of a stretch). I figure if the method is, isRow (and I used a comment) that makes it a bit better)