Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>1) Currently, your <code>solve()</code> function looks for a word <em>in a straight line</em> starting at each point: is this what you intend? I only ask because 'hello' doesn't appear as a straight line in your sample matrix:</p> <pre><code>H E i l x L p q c L O m </code></pre> <p>If you do want <strong>straight-line</strong> words only, then fine (this is how I've always understood <a href="http://en.wikipedia.org/wiki/Wordsearch" rel="nofollow">these puzzles</a> to work anyway), but if in fact you want to find words in a <strong>snake-wise</strong> fashion, then a recursive search like Zilchonum and BlueRaja are suggesting would be a good bet. Just be careful you don't end up looping back on letters you've already used.</p> <p>2) In either case, your <code>verifyWord()</code> function also has some problems: at the very least it needs to return some value in the case where you exit the <code>while (low &lt; high)</code> loop.</p> <p>Even so, it still won't quite do what you want it to: for example, say your dictionary contains <code>{"ant", "bat" "hello", "yak", "zoo"}</code>, and you call <code>verifyWord()</code> with <code>str="hel"</code>, you'd like to return a value of 2, but at the moment it does this:</p> <pre><code>step low mid high 0 0 0 5 // initialise 1 0 2 5 // set mid = (0+5)/2 = 2... words[2] == "hello" 2 0 2 1 // "hel" &lt; "hello" so set high = mid - 1 3 0 0 1 // set mid = (0+1)/2 = 0... words[0] == "ant" 4 1 0 1 // "hel" &gt; "ant" so set low = mid + 1 5 // now (low&lt;high) is false, so we exit the loop with mid==0 </code></pre> <p>Rather than comparing "hel" with "hello", perhaps you'd be better off truncating the words in the dictionary to the same length as str: ie comparing <code>str</code> to <code>word[mid].substr(0,str.length())</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload