def test_remove_all_doctest() -> None: """This is the doctest given in remove_all.""" queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) remove_all(queue) assert queue.is_empty()
def solve(self, puzzle: Puzzle, seen: Optional[Set[str]] = None) -> List[Puzzle]: """ Return a list of puzzle states representing a path to a solution of <puzzle>. The first element in the list should be <puzzle>, the second element should be a puzzle that is in <puzzle>.extensions(), and so on. The last puzzle in the list should be such that it is in a solved state. In other words, each subsequent item of the returned list should take the puzzle one step closer to a solution, which is represented by the last item in the list. Return an empty list if the puzzle has no solution. <seen> is either None (default) or a set of puzzle states' string representations, whose puzzle states can't be any part of the path to the solution. """ if puzzle.is_solved(): return [puzzle] puzzle_copy = puzzle.extensions() path_options = Queue() path_options.enqueue(puzzle_copy) path = [] seen = [] while not path_options.is_empty(): path.extend(path_options.dequeue()) puzzle_copy = [] for states in path: if any(str(states) in fails for fails in seen): path.remove(states) elif states.fail_fast(): seen.append(str(path.remove(states))) else: puzzle_copy.extend(states.extensions()) if not puzzle_copy: path_options = Queue() if path[-1].is_solved: return path else: path_options.enqueue(puzzle_copy)
def test_remove_all_but_one_doctest2() -> None: """Test is the doctest given in remove_all_but_one.""" queue = Queue() queue.enqueue(2) queue.enqueue(2) queue.enqueue(3) remove_all_but_one(queue) assert queue.is_empty() is False assert queue.dequeue() == 3 assert queue.is_empty()
def construct_from_list(values: List[List[Union[str, int]]]) -> ExprTree: """ Construct an expression tree from <values>. See the handout for a detailed explanation of how <values> encodes the expression tree. Hint: We have provided you with the helper method ExprTree.append You will likely want to use this method. Precondition: <values> encodes a valid expression tree >>> example = [[5]] >>> exp_t = construct_from_list(example) >>> exp_t == ExprTree(5, []) True >>> print(exp_t) 5 >>> print(ExprTree(5, [])) 5 >>> example = [['+'], [3, 'a']] >>> exp_t = construct_from_list(example) >>> subtrees = [ExprTree(3, []), ExprTree('a', [])] >>> exp_t == ExprTree('+', subtrees) True """ if not values: return ExprTree(None, []) copy = values[:] current_level = Queue() first = copy.pop(0)[0] if first not in OPERATORS: return ExprTree(first, []) else: result = ExprTree(first, []) current_level.enqueue(result) while copy: lst = copy.pop(0) tree = current_level.dequeue() for element in lst: subtree = ExprTree(element, []) tree.append(subtree) if element in OPERATORS: current_level.enqueue(subtree) return result
def solve(self, puzzle: Puzzle, seen: Optional[Set[str]] = None) -> List[Puzzle]: """ Return a list of puzzle states representing a path to a solution of <puzzle>. The first element in the list should be <puzzle>, the second element should be a puzzle that is in <puzzle>.extensions(), and so on. The last puzzle in the list should be such that it is in a solved state. In other words, each subsequent item of the returned list should take the puzzle one step closer to a solution, which is represented by the last item in the list. Return an empty list if the puzzle has no solution. <seen> is either None (default) or a set of puzzle states' string representations, whose puzzle states can't be any part of the path to the solution. """ if puzzle.fail_fast(): return [] if seen is not None: if str(puzzle) in seen: return [] if puzzle.is_solved(): return [puzzle] if seen is None: seen = {str(puzzle)} else: seen.add(str(puzzle)) queue = Queue() for extension in puzzle.extensions(): queue.enqueue([puzzle, extension]) while not queue.is_empty(): curr_path = queue.dequeue() examine = curr_path[-1] if examine.is_solved(): if seen is None or str(examine) not in seen: return curr_path if not (examine.fail_fast() or (str(examine) in seen)): seen.add(str(examine)) for extension in examine.extensions(): new_path = curr_path + [extension] queue.enqueue(new_path) return []
def construct_from_list(values: List[List[Union[str, int]]]) -> ExprTree: """ Construct an expression tree from <values>. See the handout for a detailed explanation of how <values> encodes the expression tree. Hint: We have provided you with the helper method ExprTree.append You will likely want to use this method. Precondition: <values> encodes a valid expression tree >>> example = [[5]] >>> exp_t = construct_from_list(example) >>> exp_t == ExprTree(5, []) True >>> print(exp_t) 5 >>> print(ExprTree(5, [])) 5 >>> example = [['+'], [3, 'a']] >>> exp_t = construct_from_list(example) >>> subtrees = [ExprTree(3, []), ExprTree('a', [])] >>> exp_t == ExprTree('+', subtrees) True """ solution = ExprTree(values[0][0], []) temp = Queue() temp.enqueue(solution) lst = values lst.remove(values[0]) for value in values: v = temp.dequeue() for i in value: if i in ["*", "+"]: tree = ExprTree(i, []) temp.enqueue(tree) v.append(tree) else: v.append(ExprTree(i, [])) return solution
def construct_from_list(values: List[List[Union[str, int]]]) -> ExprTree: """ Construct an expression tree from <values>. See the handout for a detailed explanation of how <values> encodes the expression tree. Hint: We have provided you with the helper method ExprTree.append You will likely want to use this method. Precondition: <values> encodes a valid expression tree >>> example = [[5]] >>> exp_t = construct_from_list(example) >>> exp_t == ExprTree(5, []) True >>> print(exp_t) 5 >>> print(ExprTree(5, [])) 5 >>> example = [['+'], [3, 'a']] >>> exp_t = construct_from_list(example) >>> subtrees = [ExprTree(3, []), ExprTree('a', [])] >>> exp_t == ExprTree('+', subtrees) True """ queue = Queue() a = ExprTree(values.pop(0)[-1], []) queue.enqueue(a) for lst in values: gen = queue.dequeue() for i in lst: root = ExprTree(i, []) if i in OPERATORS: queue.enqueue(root) gen.append(root) else: gen.append(root) return a
def filter_queue(q: Queue, min: int) -> None: """Remove all items from <q> that are less than <minimum>. >>> q = Queue() >>> q.enqueue(2) >>> q.enqueue(21) >>> q.enqueue(5) >>> q.enqueue(1) >>> filter_queue(q, 10) >>> q.dequeue() 21 >>> q.is_empty() True """ temp_queue = Queue() while not q.is_empty(): value = q.dequeue() if value >= min: temp_queue.enqueue(value) while not temp_queue.is_empty(): q.enqueue(temp_queue.dequeue())
def solve(self, puzzle: Puzzle, seen: Optional[Set[str]] = None) -> List[Puzzle]: """ Return a list of puzzle states representing a path to a solution of <puzzle>. The first element in the list should be <puzzle>, the second element should be a puzzle that is in <puzzle>.extensions(), and so on. The last puzzle in the list should be such that it is in a solved state. In other words, each subsequent item of the returned list should take the puzzle one step closer to a solution, which is represented by the last item in the list. Return an empty list if the puzzle has no solution. <seen> is either None (default) or a set of puzzle states' string representations, whose puzzle states can't be any part of the path to the solution. """ queue = Queue() if seen is None: seen = set() queue.enqueue([puzzle]) while not queue.is_empty(): lst = queue.dequeue() p = lst[-1] if p.fail_fast() or str(p) in seen: if str(p) not in seen: seen.add(str(p)) elif p.is_solved(): return lst else: for puz in p.extensions(): sublst = lst + [puz] queue.enqueue(sublst) return []
def solve(self, puzzle: Puzzle, seen: Optional[Set[str]] = None) -> List[Puzzle]: """ Return a list of puzzle states representing a path to a solution of <puzzle>. The first element in the list should be <puzzle>, the second element should be a puzzle that is in <puzzle>.extensions(), and so on. The last puzzle in the list should be such that it is in a solved state. In other words, each subsequent item of the returned list should take the puzzle one step closer to a solution, which is represented by the last item in the list. Return an empty list if the puzzle has no solution. <seen> is either None (default) or a set of puzzle states' string representations, whose puzzle states can't be any part of the path to the solution. """ if seen is None: seen = set() if puzzle.fail_fast(): return [] else: lst = puzzle.extensions() potentials = Queue() for i in lst: if not i.fail_fast() and not i.__str__() in seen: potentials.enqueue([puzzle] + [i]) while not potentials.is_empty(): path = potentials.dequeue() if path[-1].is_solved(): return path potentials = _check_extensions(path[-1], potentials, path, seen) return []