solution.py
def naked_twins(values):
    """Eliminate values using the naked twins strategy.
    Args:
        values(dict): a dictionary of the form {'box_name': '123456789', ...}

    Returns:
        the values dictionary with the naked twins eliminated from peers.
    """
    """
    Find all instances of naked twins
    Eliminate the naked twins as possibilities for their peers
    """
    # initialize sudoku processing...
    import collections
    sudoku_init()
    global row_units
    global column_units
    global square_units
    #global peers
    global boxes
    global unitlist
    global dia_units
    ulist = [x for x in unitlist if x not in dia_units]
    units = dict((s, [u for u in ulist if s in u]) for s in boxes)
    peers = dict((s, set(sum(units[s],[]))-set([s])) for s in boxes)
    row_units = dict((s, [u for u in row_units if s in u]) for s in boxes)
    col_units = dict((s, [u for u in column_units if s in u]) for s in boxes)
    sqr_units = dict((s, [u for u in square_units if s in u]) for s in boxes)
    row_peers = dict((s, set(sum(row_units[s],[]))-set([s])) for s in boxes)
    col_peers = dict((s, set(sum(col_units[s],[]))-set([s])) for s in boxes)
    sqr_peers = dict((s, set(sum(sqr_units[s],[]))-set([s])) for s in boxes)

    stalled = False
    key_pairs_d = dict((box,values[box]) for box in values.keys() if len(values[box]) == 2)
    display(values, boxes, rows, cols)
    #print(key_pairs_d)

    twins = find_naked_twins(key_pairs_d)
    #print(twins)
    new_sudoku = values.copy()
    for k, v in twins.items():
        if v in row_peers[k] and k in row_peers[v] and new_sudoku[k] == new_sudoku[v]:
            #print(v, " in row_peers of ", k, "and viceversa")
            for peer in row_peers[k]:
                if len(new_sudoku[peer]) > 1:
                    #print("row peers of : ",k, " is ", peer, new_sudoku[peer])
                    if new_sudoku[peer] != new_sudoku[k]:
                        for substring in new_sudoku[k]:
                            if substring in new_sudoku[peer]:
                                new_sudoku = assign_value(new_sudoku, peer, new_sudoku[peer].replace(substring,""))
                                #print("new_sudoku updated: ", new_sudoku[peer])
                    else:
                        pass
                        #print("new_sudoku[",peer,"] = ", new_sudoku[peer])
                        #print("new_sudoku[", k,"] = ", new_sudoku[k])

        elif v in col_peers[k] and k in col_peers[v]:
            #print(v, " in col_peers of ", k, "and viceversa")
            for peer in col_peers[k]:
                if len(new_sudoku[peer]) > 1:
                    #print("col peers of : ",k, " is ", peer, new_sudoku[peer])
                    if new_sudoku[peer] != new_sudoku[k]:
                        for substring in new_sudoku[k]:
                            if substring in new_sudoku[peer]:
                                new_sudoku = assign_value(new_sudoku, peer, new_sudoku[peer].replace(substring,""))
                                #print("new_sudoku updated: ", new_sudoku[peer])
                    else:
                        pass
                        #print("new_sudoku[",peer,"] = ", new_sudoku[peer])
                        #print("new_sudoku[", k,"] = ", new_sudoku[k])
        else:
            pass

        if v in sqr_peers[k] and k in sqr_peers[v]:
            #print(v, " in sqr_peers of ", k, "and viceversa")
            for peer in sqr_peers[k]:
                if len(new_sudoku[peer]) > 1:
                    #print("sqr peers of : ",k, " is ", peer, new_sudoku[peer])
                    if new_sudoku[peer] != new_sudoku[k]:
                        for substring in new_sudoku[k]:
                            if substring in new_sudoku[peer]:
                                #print("new_sudoku before updated: ", new_sudoku[peer])
                                new_sudoku = assign_value(new_sudoku, peer, new_sudoku[peer].replace(substring,""))
                                #print("new_sudoku after updated: ", new_sudoku[peer])
                    else:
                        pass
                        #print("new_sudoku[",peer,"] = ", new_sudoku[peer])
                        #print("new_sudoku[", k,"] = ", new_sudoku[k])

    display(new_sudoku, boxes, rows, cols)
    #print("############################################################################################")
    return new_sudoku
Eliminate values using the naked twins strategy.
    Args:
        values(dict): a dictionary of the form {'box_name': '123456789', ...}

    Returns:
        the values dictionary with the naked twins eliminated from peers.
    
Eliminate values using the naked twins strategy.
Eliminate values using the naked twins strategy .
def naked_twins(values):
    import collections
    sudoku_init()
    global row_units
    global column_units
    global square_units
    global boxes
    global unitlist
    global dia_units
    ulist = [x for x in unitlist if x not in dia_units]
    units = dict((s, [u for u in ulist if s in u]) for s in boxes)
    peers = dict((s, set(sum(units[s],[]))-set([s])) for s in boxes)
    row_units = dict((s, [u for u in row_units if s in u]) for s in boxes)
    col_units = dict((s, [u for u in column_units if s in u]) for s in boxes)
    sqr_units = dict((s, [u for u in square_units if s in u]) for s in boxes)
    row_peers = dict((s, set(sum(row_units[s],[]))-set([s])) for s in boxes)
    col_peers = dict((s, set(sum(col_units[s],[]))-set([s])) for s in boxes)
    sqr_peers = dict((s, set(sum(sqr_units[s],[]))-set([s])) for s in boxes)
    stalled = False
    key_pairs_d = dict((box,values[box]) for box in values.keys() if len(values[box]) == 2)
    display(values, boxes, rows, cols)
    twins = find_naked_twins(key_pairs_d)
    new_sudoku = values.copy()
    for k, v in twins.items():
        if v in row_peers[k] and k in row_peers[v] and new_sudoku[k] == new_sudoku[v]:
            for peer in row_peers[k]:
                if len(new_sudoku[peer]) > 1:
                    if new_sudoku[peer] != new_sudoku[k]:
                        for substring in new_sudoku[k]:
                            if substring in new_sudoku[peer]:
                                new_sudoku = assign_value(new_sudoku, peer, new_sudoku[peer].replace(substring,""))
                    else:
                        pass
        elif v in col_peers[k] and k in col_peers[v]:
            for peer in col_peers[k]:
                if len(new_sudoku[peer]) > 1:
                    if new_sudoku[peer] != new_sudoku[k]:
                        for substring in new_sudoku[k]:
                            if substring in new_sudoku[peer]:
                                new_sudoku = assign_value(new_sudoku, peer, new_sudoku[peer].replace(substring,""))
                    else:
                        pass
        else:
            pass
        if v in sqr_peers[k] and k in sqr_peers[v]:
            for peer in sqr_peers[k]:
                if len(new_sudoku[peer]) > 1:
                    if new_sudoku[peer] != new_sudoku[k]:
                        for substring in new_sudoku[k]:
                            if substring in new_sudoku[peer]:
                                new_sudoku = assign_value(new_sudoku, peer, new_sudoku[peer].replace(substring,""))
                    else:
                        pass
    display(new_sudoku, boxes, rows, cols)
    return new_sudoku
def naked_twins ( values ) : """
    Find all instances of naked twins
    Eliminate the naked twins as possibilities for their peers
    """ import collections sudoku_init ( ) global row_units global column_units global square_units global boxes global unitlist global dia_units ulist = [ x for x in unitlist if x not in dia_units ] units = dict ( ( s , [ u for u in ulist if s in u ] ) for s in boxes ) peers = dict ( ( s , set ( sum ( units [ s ] , [ ] ) ) - set ( [ s ] ) ) for s in boxes ) row_units = dict ( ( s , [ u for u in row_units if s in u ] ) for s in boxes ) col_units = dict ( ( s , [ u for u in column_units if s in u ] ) for s in boxes ) sqr_units = dict ( ( s , [ u for u in square_units if s in u ] ) for s in boxes ) row_peers = dict ( ( s , set ( sum ( row_units [ s ] , [ ] ) ) - set ( [ s ] ) ) for s in boxes ) col_peers = dict ( ( s , set ( sum ( col_units [ s ] , [ ] ) ) - set ( [ s ] ) ) for s in boxes ) sqr_peers = dict ( ( s , set ( sum ( sqr_units [ s ] , [ ] ) ) - set ( [ s ] ) ) for s in boxes ) stalled = False key_pairs_d = dict ( ( box , values [ box ] ) for box in values . keys ( ) if len ( values [ box ] ) == 2 ) display ( values , boxes , rows , cols ) twins = find_naked_twins ( key_pairs_d ) new_sudoku = values . copy ( ) for k , v in twins . items ( ) : if v in row_peers [ k ] and k in row_peers [ v ] and new_sudoku [ k ] == new_sudoku [ v ] : for peer in row_peers [ k ] : if len ( new_sudoku [ peer ] ) > 1 : if new_sudoku [ peer ] != new_sudoku [ k ] : for substring in new_sudoku [ k ] : if substring in new_sudoku [ peer ] : new_sudoku = assign_value ( new_sudoku , peer , new_sudoku [ peer ] . replace ( substring , "" ) ) else : pass elif v in col_peers [ k ] and k in col_peers [ v ] : for peer in col_peers [ k ] : if len ( new_sudoku [ peer ] ) > 1 : if new_sudoku [ peer ] != new_sudoku [ k ] : for substring in new_sudoku [ k ] : if substring in new_sudoku [ peer ] : new_sudoku = assign_value ( new_sudoku , peer , new_sudoku [ peer ] . replace ( substring , "" ) ) else : pass else : pass if v in sqr_peers [ k ] and k in sqr_peers [ v ] : for peer in sqr_peers [ k ] : if len ( new_sudoku [ peer ] ) > 1 : if new_sudoku [ peer ] != new_sudoku [ k ] : for substring in new_sudoku [ k ] : if substring in new_sudoku [ peer ] : new_sudoku = assign_value ( new_sudoku , peer , new_sudoku [ peer ] . replace ( substring , "" ) ) else : pass display ( new_sudoku , boxes , rows , cols ) return new_sudoku
Eliminate values using the naked twins strategy.
Eliminate values using the naked twins strategy .
"""Eliminate values using the naked twins strategy.
    Args:
        values(dict): a dictionary of the form {'box_name': '123456789', ...}

    Returns:
        the values dictionary with the naked twins eliminated from peers.
    """ """
    Find all instances of naked twins
    Eliminate the naked twins as possibilities for their peers
    """ # initialize sudoku processing...
 #global peers
 #print(key_pairs_d)
 #print(twins)
 #print(v, " in row_peers of ", k, "and viceversa")
 #print("row peers of : ",k, " is ", peer, new_sudoku[peer])
 #print("new_sudoku updated: ", new_sudoku[peer])
 #print("new_sudoku[",peer,"] = ", new_sudoku[peer])
 #print("new_sudoku[", k,"] = ", new_sudoku[k])
 #print(v, " in col_peers of ", k, "and viceversa")
 #print("col peers of : ",k, " is ", peer, new_sudoku[peer])
 #print("new_sudoku updated: ", new_sudoku[peer])
 #print("new_sudoku[",peer,"] = ", new_sudoku[peer])
 #print("new_sudoku[", k,"] = ", new_sudoku[k])
 #print(v, " in sqr_peers of ", k, "and viceversa")
 #print("sqr peers of : ",k, " is ", peer, new_sudoku[peer])
 #print("new_sudoku before updated: ", new_sudoku[peer])
 #print("new_sudoku after updated: ", new_sudoku[peer])
 #print("new_sudoku[",peer,"] = ", new_sudoku[peer])
 #print("new_sudoku[", k,"] = ", new_sudoku[k])
 #print("############################################################################################")

the values dictionary with the naked twins eliminated from peers.
the values dictionary with the naked twins eliminated from peers .
values
None