Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import islice
- def load_words(filepath):
- """Loads words from the word list into a list."""
- with open(filepath, "r") as fp:
- return fp.read().splitlines()
- def remove_words_with_repeat_letters(words):
- """Removes words with duplicate letters and returns the filtered list."""
- return [word for word in words if len(set(word)) == len(word)]
- def max_subset_without_repeat_letters(wordsets, disallowed_letters=set(),
- index=-1, depth=0, hist=[]):
- """Given an index, find the maximum number of words after the last word which
- can be added to a pre-existing subset, without repeating any letters in the
- subset. Returns the largest possible amount of words that can be added to
- the subset.
- """
- # Print progress
- if depth == 1:
- print(index, end=" ")
- # Iterate through all the words that come after index
- for i in range(index + 1, len(wordsets)):
- word, wordset = wordsets[i]
- # Check that the word does not contain previous letters
- if disallowed_letters.isdisjoint(wordset):
- new_hist = hist + [word]
- max_subset_without_repeat_letters(wordsets, disallowed_letters |
- wordset, i, depth + 1, new_hist)
- if len(new_hist) >= 5:
- print("\n\n" + " ".join(new_hist) + "\n") # TODO: Save this
- if __name__ == "__main__":
- words_path = "path_to_word_list_here"
- words = load_words(words_path)
- # Remove words with duplicate letters
- words = remove_words_with_repeat_letters(words)
- # import random
- # words = random.sample(words, 3000)
- # Pre-compute sets of words for performance.
- wordsets = tuple([(word, set(word)) for word in words])
- output = max_subset_without_repeat_letters(wordsets)
Advertisement
Add Comment
Please, Sign In to add comment