In python, how do you locate a specific word in a string file. [closed]

You can implement this efficiently by using the same algorithm that grep uses to find words. This is the Boyer-Moore string search algorithm.

Fundamentally you search for the last letter of the string. You do this by creating a list of all of the letters in your target word, and then you inspect letters in the file using seek. If you find a letter which is not in the word then you know that the word cannot end before the full length of the word, so you can skip that far ahead and test again. If the letter is in the word then you use the possible positions of it in the word to refine your search. If you find the last letter, then you can move back to the expected start of the word and check that it is as you expect.

Leave a Comment