week3/crossword/generate.py
def backtrack(self, assignment):
        """
        USE PSEUDOCODE FROM THE LECTURE NOTES

        Using Backtracking Search, take as input a partial assignment for the
        crossword and return a complete assignment if possible to do so.

        `assignment` is a mapping from variables (keys) to words (values).

        If no assignment is possible, return None.
        """
        # assignment is already complete (all variables have words), simply return assignment
        if self.assignment_complete(assignment): return assignment

        # select an unassigned variable to choose its domain (word)
        variable = self.select_unassigned_variable(assignment)

        # traverse over all values in the domain that it sorted with heuristics (least constraining values)
        for value in self.order_domain_values(variable, assignment):
            # assign the variable selected to assignment to see if it fits
            assignment[variable] = value
            # if the assignment remains consistent (all words unique and all overlaps are of the same characters)
            if self.consistent(assignment):
                # recursive call on this method to see if further values that arise from this choice are consistent
                result = self.backtrack(assignment)
                if result:  # if the result received assignment (base case is on line 289), then the variable works
                    return result

                # if the result did not receive assignment (received None), then remove the variable from assignment,
                # as it doesn't produce a solution
                assignment.pop(variable, None)

        # return None if the chosen variable does not fit in the assignment (used for recursion, to check line 302)
        return None

        USE PSEUDOCODE FROM THE LECTURE NOTES

        Using Backtracking Search, take as input a partial assignment for the
        crossword and return a complete assignment if possible to do so.

        `assignment` is a mapping from variables (keys) to words (values).

        If no assignment is possible, return None.
        
USE PSEUDOCODE FROM THE LECTURE NOTES
Using Backtracking Search, take as input a partial assignment for the
crossword and return a complete assignment if possible to do so.

`assignment` is a mapping from variables (keys) to words (values).

If no assignment is possible, return None.
USE PSEUDOCODE FROM THE LECTURE NOTES Using Backtracking Search take as input a partial assignment for the crossword and return a complete assignment if possible to do so . ` assignment ` is a mapping from variables ( keys ) to words ( values ) . If no assignment is possible return None .
def backtrack(self, assignment):
        if self.assignment_complete(assignment): return assignment
        variable = self.select_unassigned_variable(assignment)
        for value in self.order_domain_values(variable, assignment):
            assignment[variable] = value
            if self.consistent(assignment):
                result = self.backtrack(assignment)
                if result:  
                    return result
                assignment.pop(variable, None)
        return None
def backtrack ( self , assignment ) : if self . assignment_complete ( assignment ) : return assignment variable = self . select_unassigned_variable ( assignment ) for value in self . order_domain_values ( variable , assignment ) : assignment [ variable ] = value if self . consistent ( assignment ) : result = self . backtrack ( assignment ) if result : return result assignment . pop ( variable , None ) return None
USE PSEUDOCODE FROM THE LECTURE NOTES
Using Backtracking Search, take as input a partial assignment for the
crossword and return a complete assignment if possible to do so.
USE PSEUDOCODE FROM THE LECTURE NOTES Using Backtracking Search take as input a partial assignment for the crossword and return a complete assignment if possible to do so .
"""
        USE PSEUDOCODE FROM THE LECTURE NOTES

        Using Backtracking Search, take as input a partial assignment for the
        crossword and return a complete assignment if possible to do so.

        `assignment` is a mapping from variables (keys) to words (values).

        If no assignment is possible, return None.
        """ # assignment is already complete (all variables have words), simply return assignment # select an unassigned variable to choose its domain (word) # traverse over all values in the domain that it sorted with heuristics (least constraining values) # assign the variable selected to assignment to see if it fits # if the assignment remains consistent (all words unique and all overlaps are of the same characters) # recursive call on this method to see if further values that arise from this choice are consistent # if the result received assignment (base case is on line 289), then the variable works # if the result did not receive assignment (received None), then remove the variable from assignment, # as it doesn't produce a solution # return None if the chosen variable does not fit in the assignment (used for recursion, to check line 302)
self
assignment