def find_solution_for_size(size, dicts): """ Finds a solution for a given size :param size: a tuple of required size :param dicts: dictionary of lists of all available words, per word_length :returns: a solution (Rectangle object) or None """ rectangle_width = size[0] rectangle_height = size[1] words = dicts[rectangle_width] trie = get_trie(rectangle_width, dicts) r = Rectangle(rectangle_width, words) try: while True: r.get_next() #if the rectangle is valid so far, exit or go lower if all(trie.has_keys_with_prefix(col) for col in r.get_cols()): if rectangle_height == r.curr_height: logging.info('Found a solution: %s - %s', size, r) return r else: r.lower() except StopIteration: return None raise Exception('This should not happend')
def test_columns_more_levels(self): r = Rectangle(3, ['123', '456', '789']) r.get_next() r.lower() r.get_next() r.lower() r.get_next() self.assertEquals( list(r.get_cols()), [u'111', u'222', u'333'], ) r.get_next() self.assertEquals( list(r.get_cols()), [u'114', u'225', u'336'], )
def test_columns_one_level(self): r = Rectangle(3, ['123']) self.assertEquals( r.get_next(), '123', ) self.assertEquals( list(r.get_cols()), [u'1', u'2', '3'], )