Skip to content
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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Rectangles solution #373

wants to merge 5 commits into from

Conversation

macta
Copy link
Contributor

@macta macta commented May 30, 2019

This is quite an interesting problem

Copy link
Contributor

@bencoman bencoman left a 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 )]]
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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' ?

Copy link
Contributor Author

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 ].
Copy link
Contributor

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: [...] ?

Copy link
Contributor Author

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

@bencoman
Copy link
Contributor

bencoman commented May 31, 2019

Brute force is quite a valid approach and is the only one I could think of for a while.
Then reviewing Array2D >> isLineAt: another concept came to mind...

  1. Generate horizontal line segments and validate pairs as possible rectangle sides
  2. Generate pairs of vertical line segments and validate pairs
  3. Combinatorially compare the two collections

(not advocating you change yours, just sharing)

@macta
Copy link
Contributor Author

macta commented Jun 5, 2019

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)

Base automatically changed from master to main January 28, 2021 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants