From 449032815464154be33320f9041ad236f8fbdde5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20S=2E=20Martins?= Date: Wed, 18 May 2016 19:18:59 +0100 Subject: [PATCH] Words with length < 1 are now rejected. Words of length 2, I'm looking at you. --- crossword_generator.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crossword_generator.py b/crossword_generator.py index e880ba3..4885e60 100755 --- a/crossword_generator.py +++ b/crossword_generator.py @@ -130,15 +130,15 @@ def draw_words(words, n_words=100): while len(selected_words) < n_words: # Choose candidate randomly candidate = words.pop(random.randint(0, len(words)-1)) - # Append candidate if it's not already there - if candidate not in selected_words: + # Append candidate if its length is acceptable + if len(candidate) > 1: selected_words.append(candidate) # ... and return the list return selected_words # Basic Functions -def read_word_list(filename, n_words=100): +def read_word_list(filename): """ This function reads the file and returns the words read. It expects a file where each word is in a line. """ @@ -154,7 +154,6 @@ def read_word_list(filename, n_words=100): return words - def generate_grid(words, dim): """ This function receives a list of words and creates a new grid, which represents our puzzle. The newly-created grid is of dimensions @@ -219,7 +218,7 @@ def generate_grid(words, dim): # Generate new possibilities, if needed while len(possibilities) < 30: print("Getting new words!") - sample = draw_words(words) + sample = draw_words(words, 1000) possibilities.extend(generate_possibilities(sample, dim)) possibilities = [x for x in possibilities if is_valid(x, grid) and x["word"] != new["word"]]