<jupyter_start><jupyter_text>ScenariosThose are different escenarios than have a Sudoku puzzle:<jupyter_code># Test1: (Return a state 2 in our Solver.py) The puzzle is invalid and the same number in a row or column. 
test1 = '88..74...1.........7.5.9.4....65.4..73.....296.8.......2.3.1.5.........8...9....2'

# Test3 2 and 3: (Return a state 1 in our Solver.py) Those are valid puzzles
test2 = '.5.....7.9......6...172.16.17.32..69.3...8...4...3.42..192.168.127.12...9......1.8.....2.'
test3 = '8...74...1.........7.5.9.4....65.4..73.....296.8.......2.3.1.5.........8...9....2'

# Test 4:(Return a state 3 in our Solver.py) Valid Sudoku, the initial puzzle follows the Sudoku rules
# but doesn't have a solution
test4 = '...2....58....9..4...18..2.3.....5.95......7..6.....4.........6..1.3498.92.85...3'

# Test_triple 1,2 and . Those are puzzle that have naked triples (sudoku technique)
test_triple = '...29438....17864.48.3561....48375.1...4157..5..629834953782416126543978.4.961253'
test_triple2 = '4..5..37.32......4.........8....172.16.17.32...........9..7..9.1..94.651.......7....'
test_triple3 = '39....7........65.5.7...349.4938.5.66.1.54983853...4..9..8..134..294.8654.....297'

names_tests = ['test2', 'test3', 'test4', 'test_triple', 'test_triple2', 'test_triple3']
tests = [test2, test3, test4, test_triple, test_triple2, test_triple3]<jupyter_output><empty_output><jupyter_text>Testing all the possible scenarios  Except when the puzzle is invalid in the beginning (doesn't follow the Sudoku rules) after loading the image<jupyter_code>"""
To solve a puzzle use the function solve and pass the sudoku grid( String 81 characters)
"""
count=0
for test in tests:
    print(names_tests[count] + ' :' + "\n", solve(test)[0])
    count+=1
    
solve(test1)
count=0
for test in tests:
    print(names_tests[count])
    grid = dict(zip(boxes, ["." if
                    element == "." else element for element in test]))
    count += 1
    state = solve(test)[0]
    print('State: ', state)
    print('Initial Grid')
    #display a puzzle in a grid
    display(grid)
    if state == 1:
        print('Solution')
        display(conv_values(solve(test)[1]))<jupyter_output>test2
State:  1
Initial Grid
. 5 . |. . . |. 7 . 
9 . . |. . . |. 6 . 
. . 2 |1 . 7 |5 . 8 
------+------+------
. 4 . |. 6 9 |. 3 . 
. . 8 |. . . |4 . . 
. 3 . |4 2 . |. 5 . 
------+------+------
3 . 1 |2 . 8 |6 . . 
. 9 . |. . . |. . 1 
. 8 . |. . . |. 2 . 

Solution
8 5 3 |9 4 6 |1 7 2 
9 1 7 |5 8 2 |3 6 4 
4 6 2 |1 3 7 |5 9 8 
------+------+------
1 4 5 |8 6 9 |2 3 7 
6 2 8 |3 7 5 |4 1 9 
7 3 9 |4 2 1 |8 5 6 
------+------+------
3 7 1 |2 9 8 |6 4 5 
2 9 4 |6 5 3 |7 8 1 
5 8 6 |7 1 4 |9 2 3 

test3
State:  1
Initial Grid
8 . . |. 7 4 |. . . 
1 . . |. . . |. . . 
. 7 . |5 . 9 |. 4 . 
------+------+------
. . . |6 5 . |4 . . 
7 3 . |. . . |. 2 9 
6 . 8 |. . . |. . . 
------+------+------
. 2 . |3 . 1 |. 5 . 
. . . |. . . |. . 8 
. . . |9 . . |. . 2 

Solution
8 5 9 |1 7 4 |2 3 6 
1 6 4 |8 3 2 |5 9 7 
3 7 2 |5 6 9 |8 4 1 
------+------+------
2 9 1 |6 5 7 |4 8 3 
7 3 5 |4 1 8 |6 2 9 
6 4 8 |2 9 3 |1 7 5 
------+------+------
9 2 6 |3 8 1 |7 5 4 
4 1 3 |7 2 5 |9 6 8 
5 8 7 |9 4 6 |3 1 [...]<jupyter_text>Testing scenario when the puzzle is invalid from the beginning (doesn't follow the Sudoku rules) after loading the image<jupyter_code>print(solve(test1))
print('\n Initial Grid: ')
display(dict(zip(boxes, ["." if
                    element == "." else element for element in test1])))<jupyter_output>(2, [['8', 'A1'], ['8', 'A2']], '88..74...1.........7.5.9.4....65.4..73.....296.8.......2.3.1.5.........8...9....2', 'No Difficulty Level')

 Initial Grid: 
8 8 . |. 7 4 |. . . 
1 . . |. . . |. . . 
. 7 . |5 . 9 |. 4 . 
------+------+------
. . . |6 5 . |4 . . 
7 3 . |. . . |. 2 9 
6 . 8 |. . . |. . . 
------+------+------
. 2 . |3 . 1 |. 5 . 
. . . |. . . |. . 8 
. . . |9 . . |. . 2<jupyter_text>Individual Testing<jupyter_code>test_individual = tests[3]
# convert a string into the dictionary with the values
grid_test_individual = dict(zip(boxes, ["123456789" if element == "." else element for element in test_individual]))
len(test_individual)
solve(test_individual)
#search( ) is the function try to reduce a puzzle as much as possible and returns the solution
search(grid_test_individual)
reduce_puzzle(grid_test_individual)
solve_technique(test_individual,'single_position')
solve_technique(test_individual,'single_candidate')
solve_technique(test_individual,'naked_twins')
solve_technique(test_individual,'naked_triple')<jupyter_output><empty_output><jupyter_text>Testing solver with dataset<jupyter_code>df = pd.read_csv('../Omega2020/data/dataset.csv').drop('Unnamed: 0', axis=1)
print(df.shape)
df.head()
solve(df['Sudoku'][0])
a = solve(df['Sudoku'][0])[2]

a = dict(zip(boxes, ["." if element == "." else element for element in a]))
# tranform a dictionary into a string
"".join([value if len(value) == 1 else "." for value in a.values()])
display(a)
df['Solver'] = df['Sudoku'].apply(lambda x: solve(x)[0])
df.head()
df['Solver'].value_counts()<jupyter_output><empty_output><jupyter_text>Testing solver by technique with dataset<jupyter_code>df['Solved_single_position'] = df['Sudoku'].apply(lambda x: solve_technique(x, 'single_position')[0])
df['Solved_single_position'].value_counts()
df['Solved_single_candidate'] = df['Sudoku'].apply(lambda x: solve_technique(x, 'single_candidate')[0])
df['Solved_single_candidate'].value_counts()
df['Solved_naked_twins']= df['Sudoku'].apply(lambda x: solve_technique(x, 'naked_twins')[0])
df['Solved_naked_twins'].value_counts()
df['Solved_naked_triple']= df['Sudoku'].apply(lambda x: solve_technique(x, 'naked_triple')[0])
df['Solved_naked_triple'].value_counts()
df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2422349470761756, 'fraction_numerical': 0.09960090230782578, 'mean_word_length': 2.2528216704288937, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '180312', 'license': 'MIT', 'n_tokens_mistral': 2628, 'n_tokens_neox': 2290, 'n_words': 600, 'path': 'Notebooks/Solver and Scraper/2_Solver_test.ipynb', 'repo_name': 'supai-red/omega2020-ds', 'size': '5752'}