Question: Need the code for the function (ie where it says, YOUR CODE HERE). Must run smoothly according to tests. No additional code needed (ie besides

Need the code for the function (ie where it says, "YOUR CODE HERE"). Must run smoothly according to tests. No additional code needed (ie besides the code after "YOUR CODE HERE").

Need the code for the function (ie where it says, "YOUR CODE

HERE"). Must run smoothly according to tests. No additional code needed (ie

besides the code after "YOUR CODE HERE"). class HolyQueens(object): def __init__(self, board):

self.board = board self.num_rows, self.num_cols = self.board.shape # Current number of queens

in the board. self.num_queens = np.sum(self.board == QUEEN) def show(self): show_board(self.board) def

class HolyQueens(object):

def __init__(self, board):

self.board = board

self.num_rows, self.num_cols = self.board.shape

# Current number of queens in the board.

self.num_queens = np.sum(self.board == QUEEN)

def show(self):

show_board(self.board)

def propagate(self):

"""Propagates the information on the board, marking with 2 the

positions where a queen cannot be."""

# The solution can be written concisely in about 20 lines of code,

# but if you brute force it, it might be quite long.

### YOUR CODE HERE

def search(self, total_num_queens):

"""Searches for a solution, starting from the given board,

which contains exactly num_queens. It returns the board,

if such a solution is found, or None, if none could be found

after exhaustive search."""

pass

### YOUR CODE HERE

QUEEN 1 EMPTY FORBIDDEN WALL = 3 2 def show_board(board): rows, cols = board.shape for r in range (rows): S = ** QUEEN: for c in range(cols): if board[r, c] s += "Q" elif board[r, c] FORBIDDEN: S + WALL: EMPTY: elif board[r, c] S += "#" elif board[r, c] S + else: S += "?" print(s) import numpy as np 1 2 3 4. 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 board=np.array([ [0, 0, 0, 0, 0, 0], [0, 1, 0, 3, 3, 0], [0, 0, 0, 3, 3, 0], [0, 0, 1, 0, 0, 0], 0, 0, 0, 0] ]) show_board(board) def read_board(string_list): rows = len(string_list) cols len(string_list[@]) board np.zeros((rows, cols)) for r, row in enumerate(string_list): assert len(row) == cols for c, s in enumerate(row): if s == "Q": board[r, c] QUEEN elif s "#": board[r, c] WALL elif s == "*": board[r, c] = FORBIDDEN return board bs = [ 49 50 51 52 53 54 55 56 ".Q.##. ..##. ..Q... ] show_board(read_board(bs)) class HolyQueens (object): def _init__(self, board): self.board = board self.num_rows, self.num_cols = self.board.shape # Current number of queens in the board. self.num_queens np. sum( self.board == QUEEN) 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 def show(self): show_board(self.board) def propagate(self): ****** Propagates the information on the board, marking with 2 the positions where a queen cannot be."*** # The solution can be written concisely in about 20 lines of code, # but if you brute force it, it might be quite long. ### YOUR CODE HERE def search(self, total_num_queens): **** Searches for a solution, starting from the given board, which contains exactly num_queens. It returns the board, if such a solution is found, or None, if none could be found after exhaustive search. pass ### YOUR CODE HERE # Test for Propagation. # Propagating this bs [ 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 ".Q. ##.", .##. "..Q.. ] # should give this: bs_prop [ **Q*##.", *##. ha HolyQueens (read_board(bs)) hq-propagate() hq.show() assert (hq.board read_board(bs_prop)).all() # Another test for Propagation Propagation. # Propagating this bs [ ".....Q", -Q##. ".. ] # should give this: bs_prop [ ******Q", **Q##*" *** **Q****"] 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 ha HolyQueens (read_board(bs)) hq-propagate() hq.show() assert (hq.board read_board(bs_prop)).all() # test for search function bs = [ hq r = ] HolyQueens (read_board(bs)) hq.search(4) assert r is not None # You should get a solution with 4 non-interfering queens. show boarder) # another test for search function bs [ 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 r = ] ha HolyQueens (read_board(bs)) hq.search(6) assert r is not None # You should get a solution with 6 non-interfering queens. show_board(r) QUEEN 1 EMPTY FORBIDDEN WALL = 3 2 def show_board(board): rows, cols = board.shape for r in range (rows): S = ** QUEEN: for c in range(cols): if board[r, c] s += "Q" elif board[r, c] FORBIDDEN: S + WALL: EMPTY: elif board[r, c] S += "#" elif board[r, c] S + else: S += "?" print(s) import numpy as np 1 2 3 4. 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 board=np.array([ [0, 0, 0, 0, 0, 0], [0, 1, 0, 3, 3, 0], [0, 0, 0, 3, 3, 0], [0, 0, 1, 0, 0, 0], 0, 0, 0, 0] ]) show_board(board) def read_board(string_list): rows = len(string_list) cols len(string_list[@]) board np.zeros((rows, cols)) for r, row in enumerate(string_list): assert len(row) == cols for c, s in enumerate(row): if s == "Q": board[r, c] QUEEN elif s "#": board[r, c] WALL elif s == "*": board[r, c] = FORBIDDEN return board bs = [ 49 50 51 52 53 54 55 56 ".Q.##. ..##. ..Q... ] show_board(read_board(bs)) class HolyQueens (object): def _init__(self, board): self.board = board self.num_rows, self.num_cols = self.board.shape # Current number of queens in the board. self.num_queens np. sum( self.board == QUEEN) 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 def show(self): show_board(self.board) def propagate(self): ****** Propagates the information on the board, marking with 2 the positions where a queen cannot be."*** # The solution can be written concisely in about 20 lines of code, # but if you brute force it, it might be quite long. ### YOUR CODE HERE def search(self, total_num_queens): **** Searches for a solution, starting from the given board, which contains exactly num_queens. It returns the board, if such a solution is found, or None, if none could be found after exhaustive search. pass ### YOUR CODE HERE # Test for Propagation. # Propagating this bs [ 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 ".Q. ##.", .##. "..Q.. ] # should give this: bs_prop [ **Q*##.", *##. ha HolyQueens (read_board(bs)) hq-propagate() hq.show() assert (hq.board read_board(bs_prop)).all() # Another test for Propagation Propagation. # Propagating this bs [ ".....Q", -Q##. ".. ] # should give this: bs_prop [ ******Q", **Q##*" *** **Q****"] 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 ha HolyQueens (read_board(bs)) hq-propagate() hq.show() assert (hq.board read_board(bs_prop)).all() # test for search function bs = [ hq r = ] HolyQueens (read_board(bs)) hq.search(4) assert r is not None # You should get a solution with 4 non-interfering queens. show boarder) # another test for search function bs [ 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 r = ] ha HolyQueens (read_board(bs)) hq.search(6) assert r is not None # You should get a solution with 6 non-interfering queens. show_board(r)

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!