def is_align_diagonally(position1, position2): column1, row1 = to_coordinate_pair(position1) column2, row2 = to_coordinate_pair(position2) all_y = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, } x1 = int(row1) y1 = all_y[column1] x2 = int(row2) y2 = all_y[column2] same_position = (x1 == x2) and (y1 == y2) aligned_diagonally = (x1 - y1 == x2 - y2 or x1 + y1 == x2 + y2) return (not same_position) and aligned_diagonally
def find_safe_positions(queen_positions, safe_positions): safe_positions = set(safe_positions) for queen_position in queen_positions: column, row = to_coordinate_pair(queen_position) all_positions_in_the_column = positions_in_the_same_column(column) all_positions_in_the_row = positions_in_the_same_row(row) all_diagonal_positions = get_diagonal_positions(queen_position) safe_positions = safe_positions - set(all_positions_in_the_column) safe_positions = safe_positions - set(all_positions_in_the_row) safe_positions = safe_positions - set(all_diagonal_positions) return safe_positions