in reply to Re^2: Check word presence WITHOUT hashes or grep
in thread Check word presence WITHOUT hashes or grep
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Check word presence WITHOUT hashes or grep
by gojippo (Novice) on May 01, 2008 at 02:56 UTC | |
| [reply] [d/l] |
by ysth (Canon) on May 01, 2008 at 03:38 UTC | |
Your binary_search has a few problems. You need to do the int() after dividing by 2, or your limits will be non-integers and you'll see odd errors. You have an if/else in your while loop, but there are three possible cases (eq, lt, gt) you need to take into consideration. You need to set $high to $cur | [reply] |
by gojippo (Novice) on May 01, 2008 at 05:08 UTC | |
It did give me some results, but : 1)I'm getting words that ARE in the dictionnary file. 2)it gives me the use of unitialized value in numeric eq (==). Looking at the "Mastering algorithms with perl" O'Reilly Book, it turned out that I had to do $high = $cur - 1 and not +1, to adjust $high. I don't know why I'm still getting words that exist in the dictionnary file, but I understand why I get the error message, but don't know how to make it cleaner. Any ideas ? | [reply] [d/l] |
by ysth (Canon) on May 01, 2008 at 06:08 UTC | |
by gojippo (Novice) on May 01, 2008 at 06:55 UTC | |
by GrandFather (Saint) on May 01, 2008 at 04:13 UTC | |
Try stripping your code down to a sample that tests the binary search:
Prints:
which seems to indicate that your search is failing. For any non-trivial project you would take the test code and wrap it up in something like Test::More as a unit test so that you have a convenient way to test all the edge cases (zero, one or two words in the list for example) whenever you alter the code. Perl is environmentally friendly - it saves trees | [reply] [d/l] [select] |