Skip to content

Instantly share code, notes, and snippets.

@ziyadm
Created January 6, 2016 02:15
Show Gist options
  • Save ziyadm/44da6f127e6864621902 to your computer and use it in GitHub Desktop.
Save ziyadm/44da6f127e6864621902 to your computer and use it in GitHub Desktop.
stupid python thing
from collections import defaultdict
def preprocess_word_list(words):
postings_list = defaultdict(list)
for word in words:
for index, char in enumerate(word):
postings_list[(char, index)].append(word)
postings_list[('.', index)].append(word)
return postings_list
def find_matching_words(word_list, format_string):
matching_words = None
for index, char in enumerate(format_string):
if not matching_words:
matching_words = set(word_list[(char, index)])
else:
matching_words &= set(word_list[(char, index)])
return filter(lambda word: len(word) == len(format_string), list(matching_words))
def run_test_cases():
words = ['whit', 'what', 'whit', 'well', 'white']
preprocessed_word_list = preprocess_word_list(words)
format_strings = ['wh.t', '.h..', 'w...', '.', '....']
for format_string in format_strings:
print "For format string:", format_string
print "And input words:", words
print "The matching strings are: ", find_matching_words(preprocessed_word_list, format_string)
print "\n"
run_test_cases()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment