Question: Python: The eight queens puzzle/unittests: Can you please help me fix my spacing and indentation, thank you. import unittest from sys import argv # start

Python: The eight queens puzzle/unittests: Can you please help me fix my spacing and indentation, thank you.

import unittest from sys import argv # start with solving this SOLVE_ONE = True # then worry about this if you have time SOLVE_ALL = True def safe(x1, y1, x2, y2): return not (x1 == x2 or y1 == y2 or abs(x2-x1) == abs(y2-y1))

def print_board(solution): length = len(solution) print('-' * length)

a = [['Q' if (i, j) in solution else '.' \

for j in range(length)] \ for i in range(length)] for i in a: print(''.join(i)) print('-' * length)

''' size is the overall size of the problem we're solving. row is the row we're currently on, starting with 0. placed is a list of queens -- a list of tuples with (x,y) -- that have already been placed on the board. ''' def solve_one(size, row, placed): # if we're past the last row, return placed as it has the answer. # for each column # see if placing a queen at row and column is safe from all placed # queens. # if it is safe # make a recursive call with the next row and placed + (row, column) # if there was a solution with those parameters, return it. # return the empty list if row >= size: return placed

for col in range(size): can_place_here = True

for queen_pos in placed: if queen_pos[1] == col: can_place_here = False break

if queen_pos[0] - queen_pos[1] == row - col: can_place_here = False break

if queen_pos[0] + queen_pos[1] == row + col: can_place_here = False break

if can_place_here: solution = solve_one(size, row + 1, placed + [(row, col)])

if len(solution) != 0: return solution return [] ''' as above, but accumulate all the solutions in the last argument. ''' def solve_all(size, row, placed, solutions): if row >= size: solutions.append(placed) return for col in range(size): can_place_here = True for queen_pos in placed: if queen_pos[1] == col: can_place_here = False break if queen_pos[0] - queen_pos[1] == row - col: can_place_here = False break if queen_pos[0] + queen_pos[1] == row + col: can_place_here = False break if can_place_here: solve_all(size, row + 1, placed + [(row, col)], solutions)

class test_safe(unittest.TestCase): def test_same(self): self.assertFalse(safe(1, 1, 1, 1)) def test_same_row(self): self.assertFalse(safe(1, 1, 1, 2)) def test_same_column(self): self.assertFalse(safe(1, 1, 2, 1)) def test_same_diagonal(self): self.assertFalse(safe(1, 1, 5, 5)) class test_one(unittest.TestCase): def test_one_one(self): self.assertEqual(solve_one(1, 0, []), [(0, 0)]) def test_two_all(self): self.assertEqual(solve_one(2, 0, []), []) def test_four_one(self): self.assertEqual(solve_one(4, 0, []), [(0, 1), (1, 3), (2, 0), (3, 2)]) ''' class test_all(unittest.TestCase): def test_one_all(self): self.assertEqual(solve_all(1, 0, [], []), [[(0, 0)]]) def test_two_all(self): self.assertEqual(solve_all(2, 0, [], []), []) def test_three_all(self): self.assertEqual(solve_all(3, 0, [], []), []) def test_four_all(self): solutions = solve_all(4, 0, [], []) self.assertEqual(solutions, [[(0, 1), (1, 3), (2, 0), (3, 2)], \ [(0, 2), (1, 0), (2, 3), (3, 1)]]) ''' if __name__ == "__main__": size = 8 if len(argv) == 1 else int(argv[1]) if SOLVE_ONE: print_board(solve_one(size, 0, []))

if SOLVE_ALL: solutions = [] solve_all(size, 0, [], solutions) print("there are", len(solutions), "solutions for the", \ size, "queens problem:") for solution in solutions: print_board(solution)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!