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.

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

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

Returns

A dictionary of all needles updated with actual counts.

Return type

needles_found

needles_haystack.haystack_builder(filename)[source]

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.

Parameters

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

Returns

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

Return type

haystack

needles_haystack.main()[source]

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

Indices and tables