# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Solving Sudoku

# +
from sudoku import solve_sudoku, draw_sudoku

def test_sudoku(constraints):
    status, solution = solve_sudoku(constraints)
    return "PASS" if status == 'Optimal' else "FAIL"


# -

# ## Constraints
# Tuples of (x, y, number)

constraints = [(1,1,9), (1,2,3), (8,3,7), (2,2,9)]
print(draw_sudoku(constraints))

# ## The Delta Debugging Algorithm

# +
from ddebug import get_partitions
from covenant import pre, post

@pre(lambda data, test, granularity: test(data)=='FAIL')
@post(lambda result, data, test, granularity: test(result)=='FAIL')

def delta_debug(data, test, granularity=2):
    print('\n"{2}", granularity={1}'.format(len(data), granularity, data))    
    for subset in get_partitions(data, granularity):
        result = test(subset)
        print('"{}" -> {}'.format(subset, result))
        
        if result == 'FAIL':
            if len(subset) > 1:
                return delta_debug(subset, test, granularity)
            else:
                return subset # minimal failing subset
            
    if granularity < len(data):
        return delta_debug(data, test, granularity + 1)
    return data


# -

# ## Simple string example

# +
def no_rats(s): return "FAIL" if 'rat' in s else "PASS"

text = "ten wooden crates"
delta_debug(text, no_rats)
# -

# ## Solving a Sudoku

constraints = [(1,1,9), (1,2,3), (8,3,7), (2,2,9)]
minimal = delta_debug(constraints, test_sudoku)
print(draw_sudoku(minimal))

# ## More complicated Sudoku example

constraints = [(1,1,1), (2,1,2), (3,1,3), (4,2,1), (5,2,2), (6,2,3), (7,3,7), (8,3,8), (9,3,9)]
print(draw_sudoku(constraints))
minimal = delta_debug(constraints, test_sudoku)
print(draw_sudoku(minimal))

StarcoderdataJupyterScriptsDedupFiltered