Lab projects

Needles in a haystack

Python version of my stretch exercise based on Google tech writing exercise.

Console application that models finding needles in a haystack. Needles are words entered by the user, and the haystack is a file containing plain unstuctured text. User can set the number of words to enter, from 1 to 5.

needles_haystack.find_needles(needles_found, haystack)[source]

Compares each word in the dictionary to each word in the haystack list. If the needle matches the word in haystack, increments its counter value.

  • needles_found – A dictionary comprising each needle and its count.

  • haystack – A list containing all ‘words’ from the text file.


A dictionary of all needles updated with actual counts.

Return type



Opens specified file, reads each line, and tokenizes into individual strings. Eliminates contractions, punctuation, parenthesis, and so on, and builds a list named ‘haystack’ of each word, in sequence.


filename – The path and name of the text file to use as haystack.


A list of each word (in order) in the text file.

Return type



Handles input for the program and invokes the functions to perform the search and prints the results.

Indices and tables