I am working on a class named Boggle which contains two class-side methods below:
search: board for: words
| result visited trie |
result := Dictionary new.
trie := CTTrie new.
words do: [:word | trie at: word put: word].
visited := (1 to: board size) collect: [:i |
(1 to: board first size) collect: [:j | false].
].
board doWithIndex: [:row :i |
row doWithIndex: [:char :j |
self
searchUtil: board
visited: visited
i: i
j: j
prefix: ''
final: Array new
result: result
trie: trie.
].
].
^result
searchUtil: board visited: visited i: i j: j prefix: prefix final: final result: result trie: trie
| newPrefix newFinal|
(visited at: i) at: j put: true.
newPrefix := prefix, ((board at: i) at: j) asString.
newFinal := final copyWith: i @ j.
Transcript show: '1'; cr.
Transcript show: 'newPrefix: ', newPrefix; cr.
Transcript show: 'newFinal: ', newFinal; cr.
(trie contains: newPrefix) ifTrue: [
result at: newPrefix put: newFinal.
].
Transcript show: '2'; cr.
Transcript show: 'result: ', result; cr.
(i - 1) to: (i + 1) do: [ :row |
(j - 1) to: (j + 1) do: [ :col |
((row between: 1 and: board size) and: [
col between: 1 and: (board at: row) size]) ifTrue: [
((visited at: row) at: col) ifFalse: [
Transcript show: '3'; cr.
self
searchUtil: board
visited: visited
i: row
j: col
prefix: newPrefix
final: newFinal
result: result
trie: trie ] ] ] ].
(visited at: i) at: j put: false
Input:
The first argument to this method will board, represented as a static array of arrays whose elements are characters. For example, #(#($e $a) #($s $t)). The second argument will be an array of strings (words) representing the list of legal words, e.g. #(‘east’ ‘ate’ ‘john’ … ) and so on.
Output:
search:for: method will return a dictionary. The keys will be the words that the method found, and the value at each key will be an array of points. Each point corresponds to the 2D index of the corresponding characters, which make up the word, in the board.
When I test the methods with the sample below,
Transcript clear.
words := #('ea' 'eat' 'ate' 'east' 'sat' 'hello' 'girl' 'guy').
board := #( #( $e $a ) #( $s $t ) ).
result := Dictionary new.
result := Boggle search: board for: words.
Transcript show: result; cr.
I get this error:
Error: Improper store into indexable object
Clearly the issue is with newFinal array not being updated. newFinal := final copyWith: i @ j updates the value of newFinal array, and the initial value of the final in the search:for: method has been set to Array new which produces an empty array.
The actual output should be
a Dictionary('ea'-\>#((1@1).(1@2)) 'eat'-\>#((1@1).(1@2).(2.2)) ...)
and so on.
This is all I have been able to figure out. Any help to resolve this issue is greatly appreciated. Thanks in advance.