Skip to content

Commit 110bb99

Browse files
committed
Removed some extraneous code
1 parent b65d764 commit 110bb99

File tree

2 files changed

+8
-66
lines changed

2 files changed

+8
-66
lines changed

nqueens.py

+8-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" N-Queens solutions. """
1+
""" Backtracking solution to N-Queens problem. """
22

33
__author__ = "Caleb Madrigal"
44
__date__ = "2015-02-19"
@@ -22,25 +22,23 @@ def right_to_left_diagonals_valid(board):
2222
[ 56. 57. 58. 59. 60. 61. 62. 63.]]
2323
2424
It checks right-to-left columns in 2 steps:
25-
* First, it checks each right-to-left diagonal that begins with each element on the first row
25+
* Step 1: it checks each right-to-left diagonal that begins with each element on the first row
2626
(skipping the first index, since that "diagonal" is just element[0]) So the indices
2727
of each start of each row-based diagonal are: 1, 2, 3, 4, 5, 6, 7. In order to find the
2828
indices of each subsequent element in each diagonal, it adds (size-1) to the initial
2929
diagonal. For example: if we are checking the right-to-left diagonal starting with index 2,
3030
the diagonal should be indices: 2, 9, 16 (each is separated by 7). In order to perform
3131
this math, the nxn matrix is flattened.
32-
* Second, it does roughly the same thing for the right-to-left diagonals that start on the
32+
* Step 2: it does roughly the same thing for the right-to-left diagonals that start on the
3333
far-right column (indices 15, 23, 31, 39, 47, 55, 63). """
3434

3535
size = len(board)
3636
flat = board.reshape((1, size*size))[0]
37-
# Check right-to-left diagonals
37+
3838
for row_index in range(1, size):
39-
#print("row_index:", row_index)
4039
index = row_index
4140
diag_sum = 0
4241
for diag_index in range(row_index+1):
43-
#print("\tdiag_index: {0}, value: {1}".format(index, flat[index]))
4442
diag_sum += flat[index]
4543
index += (size - 1)
4644
if diag_sum > 1:
@@ -49,13 +47,11 @@ def right_to_left_diagonals_valid(board):
4947
col_diag_index = 0
5048
diag_lengths = list(range(size-1, 0, -1))
5149
for last_col_index in range(2*size-1, size*size-1, size):
52-
#print("col_index:", last_col_index)
5350
index = last_col_index
5451
diag_sum = 0
5552
diag_len = diag_lengths[col_diag_index]
5653
col_diag_index += 1
5754
for diag_index in range(diag_len):
58-
#print("\tdiag_index: {0}, value: {1}".format(index, flat[index]))
5955
diag_sum += flat[index]
6056
index += (size - 1)
6157
if diag_sum > 1:
@@ -80,43 +76,15 @@ def diags_valid(board):
8076
return right_to_left_diagonals_valid(board) and left_to_right_diagonals_valid(board)
8177

8278

83-
def rows_valid(board):
84-
""" Returns True if there are no more than 1 "1" in any row; else False. """
85-
86-
for row in board:
87-
if np.sum(row) > 1:
88-
return False
89-
return True
90-
91-
92-
def cols_valid(board):
93-
""" Returns True if there are no more than 1 "1" in any column; else False. """
94-
95-
for col_index in range(len(board)):
96-
if np.sum(board[:, col_index]) > 1:
97-
return False
98-
return True
99-
100-
101-
def is_valid(board):
102-
""" Returns True if there are no more than 1 "1" in any row, column, or diagonal;
103-
Else, it returns False. """
104-
105-
# We only have to check that the diagonals are valid, since the search
106-
# algorithm doesn't put more than 1 queen on a given row or column
107-
#return rows_valid(board) and cols_valid(board) and diags_valid(board)
108-
return diags_valid(board)
109-
110-
11179
def print_board_indented(board, row):
11280
print('\t'*row + str(board).replace('\n', '\n'+('\t'*row)))
11381

11482

11583
def search(board, row=0, cols_taken=()):
116-
""" In-place search for solution to n-queens. """
84+
""" In-place search for solution to n-queens. Backtracking algorithm. """
11785

11886
# Return if we are at the maximum depth and the solution is valid
119-
if row == len(board) and is_valid(board):
87+
if row == len(board) and diags_valid(board):
12088
return True
12189

12290
# Otherwise, try each column and recursively work down the rows
@@ -127,7 +95,7 @@ def search(board, row=0, cols_taken=()):
12795
board[row][col] = 1
12896
print_board_indented(board, row)
12997

130-
if is_valid(board):
98+
if diags_valid(board):
13199
if search(board, row+1, cols_taken + (col,)):
132100
return True
133101
else:
@@ -149,3 +117,4 @@ def n_queens(size):
149117
if __name__ == '__main__':
150118
board_size = 8
151119
n_queens(board_size)
120+

test/test_nqueens.py

-27
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,6 @@
99

1010
class NQueensTest(unittest.TestCase):
1111

12-
def testRowsValid(self):
13-
zero_board = np.zeros((3, 3))
14-
self.assertTrue(nqueens.rows_valid(zero_board))
15-
16-
bad_board = np.array([[1, 0, 1], [0, 0, 0], [0, 0, 0]])
17-
self.assertFalse(nqueens.rows_valid(bad_board))
18-
19-
bad_board2 = np.array([[1, 0, 0], [0, 0, 0], [0, 1, 1]])
20-
self.assertFalse(nqueens.rows_valid(bad_board2))
21-
22-
rows_good_board = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
23-
self.assertTrue(nqueens.rows_valid(rows_good_board))
24-
25-
rows_good_board2 = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
26-
self.assertTrue(nqueens.rows_valid(rows_good_board2))
27-
28-
def testColsValid(self):
29-
good_board = np.zeros((3, 3))
30-
self.assertTrue(nqueens.cols_valid(good_board))
31-
32-
bad_board = np.array([[0, 0, 0], [0, 1, 0], [0, 1, 0]])
33-
self.assertFalse(nqueens.cols_valid(bad_board))
34-
3512
def testDiagsValid(self):
3613
good_board = np.array([[0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 0]])
3714
self.assertTrue(nqueens.diags_valid(good_board))
@@ -42,10 +19,6 @@ def testDiagsValid(self):
4219
bad_board2 = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
4320
self.assertFalse(nqueens.diags_valid(bad_board2))
4421

45-
def testBoardValid(self):
46-
good_board = np.array([[0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 0]])
47-
self.assertTrue(nqueens.is_valid(good_board))
48-
4922
if __name__ == '__main__':
5023
unittest.main()
5124

0 commit comments

Comments
 (0)