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

Exercise wordsearch #380

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev/src/BaselineOfExercism/BaselineOfExercism.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Class {
BaselineOfExercism class >> exercisePackageNames [
"Answer the list of exercise package names (as we don't yet have proper projects)"

^ #('Exercise@Acronym' 'Exercise@Allergies' 'Exercise@Anagram' 'Exercise@ArmstrongNumbers' 'Exercise@AtbashCipher' 'Exercise@Binary' 'Exercise@Bowling' 'Exercise@Clock' 'Exercise@CollatzConjecture' 'Exercise@Darts' 'Exercise@Diamond' 'Exercise@Die' 'Exercise@Etl' 'Exercise@FlattenArray' 'Exercise@Forth' 'Exercise@GradeSchool' 'Exercise@Grains' 'Exercise@Hamming' 'Exercise@HelloWorld' 'Exercise@HighScores' 'Exercise@IsbnVerifier' 'Exercise@Isogram' 'Exercise@Leap' 'Exercise@Luhn' 'Exercise@MatchingBrackets' 'Exercise@Matrix' 'Exercise@Minesweeper' 'Exercise@Pangram' 'Exercise@Proverb' 'Exercise@Raindrops' 'Exercise@ResistorColorDuo' 'Exercise@ReverseString' 'Exercise@RobotSimulator' 'Exercise@RomanNumerals' 'Exercise@SecretHandshake' 'Exercise@Sieve' 'Exercise@SpaceAge' 'Exercise@SumOfMultiples' 'Exercise@Tournament' 'Exercise@TwelveDays' 'Exercise@TwoFer' 'Exercise@Welcome' 'Exercise@WordCount')
^ #('Exercise@Acronym' 'Exercise@Allergies' 'Exercise@Anagram' 'Exercise@ArmstrongNumbers' 'Exercise@AtbashCipher' 'Exercise@Binary' 'Exercise@Bowling' 'Exercise@Clock' 'Exercise@CollatzConjecture' 'Exercise@Darts' 'Exercise@Diamond' 'Exercise@Die' 'Exercise@Etl' 'Exercise@FlattenArray' 'Exercise@Forth' 'Exercise@GradeSchool' 'Exercise@Grains' 'Exercise@Hamming' 'Exercise@HelloWorld' 'Exercise@HighScores' 'Exercise@IsbnVerifier' 'Exercise@Isogram' 'Exercise@Leap' 'Exercise@Luhn' 'Exercise@MatchingBrackets' 'Exercise@Matrix' 'Exercise@Minesweeper' 'Exercise@Pangram' 'Exercise@Proverb' 'Exercise@Raindrops' 'Exercise@ResistorColorDuo' 'Exercise@ReverseString' 'Exercise@RobotSimulator' 'Exercise@RomanNumerals' 'Exercise@SecretHandshake' 'Exercise@Sieve' 'Exercise@SpaceAge' 'Exercise@SumOfMultiples' 'Exercise@Tournament' 'Exercise@TwelveDays' 'Exercise@TwoFer' 'Exercise@Welcome' 'Exercise@WordCount' 'Exercise@WordSearch')
]

{ #category : #baselines }
Expand Down
8 changes: 8 additions & 0 deletions dev/src/Exercise@WordSearch/String.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Extension { #name : #String }

{ #category : #'*Exercise@WordSearch' }
String >> -->> aCollectionOfAssociations [
"Similar to -> but converts aCollection to aDictionary, useful when building JSON structures"

^self -> aCollectionOfAssociations asDictionary
]
80 changes: 80 additions & 0 deletions dev/src/Exercise@WordSearch/WordSearch.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Class {
#name : #WordSearch,
#superclass : #Object,
#category : #'Exercise@WordSearch'
}

{ #category : #exercism }
WordSearch >> directions [
"answer valid directional transforms where words can occur, left, right, up, down and diagonals.
Points are encoded as Row, Col"

^ {
(-1 @ 0). "N"
(1 @ 0). "S"
(0 @ 1). "E"
(0 @ -1). "W"
(-1 @ 1). "NE"
(-1 @ -1). "NW"
(1 @ 1). "SE"
(1 @ -1). "SW"
}
]

{ #category : #internal }
WordSearch >> find: aString in: array2D at: sourcePoint bearing: bearingPoint ifPresent: aBlock [
| currentPoint |

currentPoint := sourcePoint.

aString
do: [ :char |
(array2D at: currentPoint x at: currentPoint y ifInvalid: nil) = char
ifFalse: [ ^ self ] ]
separatedBy: [ currentPoint := currentPoint + bearingPoint ].

aBlock value: (self wordLocationFrom: sourcePoint to: currentPoint)
]

{ #category : #exercism }
WordSearch >> searchWordsToSearchFor: aWordCollection grid: aGridCollection [
| wordGrid result |

wordGrid := Array2D
rows: aGridCollection size
columns: aGridCollection first size
contents: aGridCollection concatenation.

result := Dictionary new.

aWordCollection
do: [ :word |
result at: word put: nil.

wordGrid
withIndicesDo: [ :v :r :c |
self directions
do: [ :directionPoint |
self
find: word
in: wordGrid
at: r @ c
bearing: directionPoint
ifPresent: [ :location | result at: word put: location ] ] ] ].

^ result
]

{ #category : #internal }
WordSearch >> wordLocationFrom: aPointStart to: aPointEnd [
"Answer a Dictionary of the start/end location of a found word"

^ {('start'
-->>
{('row' -> aPointStart x).
('column' -> aPointStart y)}).
('end'
-->>
{('row' -> aPointEnd x).
('column' -> aPointEnd y)})} asDictionary
]
Loading