-
-
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
base: main
Are you sure you want to change the base?
Rectangles solution #373
Conversation
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.
Hey wow!! Pharo > Finder blows me away again.
I wondered if there was a better way than... x flatCollect: [ :c | c ]
but it was awkward entering the array twice, so I wondered if it could do variable assignment, and it can. Squeeze all this in the Examples textfield.
x := #(
'++'
'||'
'++') .
x flatCollect: [ :c | c ]
Discovered #concatenation
|
||
{ #category : #'*Exercise@Rectangles' } | ||
LineSegment >> points [ | ||
^self isHorizontal 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 )]] |
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.
Is this meant to be all on one line?
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 think github doesn't show formatting properly as this is actually formatted over several lines
|
||
{ #category : #'*Exercise@Rectangles' } | ||
LineSegment >> isHorizontal [ | ||
^self start x = self end x |
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)
indicesCollect: [ :r :c | | ||
(matrix at: r at: c) = $+ | ||
ifTrue: [ r @ c ] | ||
ifFalse: [ nil ] ]) asArray reject: [ :p | p isNil ]. |
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 statement is long and hard to hold in my head. Could it be split... corners := corners reject: [...]
?
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've refactored out a few sub methods, to make it more readable
Brute force is quite a valid approach and is the only one I could think of for a while.
(not advocating you change yours, just sharing) |
Your comment about generate line segment permutations is exactly what this solution is doing (maybe its a bit more obvious now) - it finds square permutations (based on the + corners), and then validates if they actually exist because they are correctly joined by "-" or "|" lines. (Probably worth saying this very text in the class comment actually) |
This is quite an interesting problem