Question: Task: In Python, implement the Nqueens.is _ valid method to check for conflicting queens trying to solve the N - queens problem ( e .

Task: In Python, implement the Nqueens.is_valid method to check for conflicting queens trying to solve the N-queens problem (e.g. are any queens in conflicting columns or diagonals with another queen). In this example, n refers to the number of rows and columns on the board and the number of queens. I am running into some logic issues with my code for this method. Below are the task parameters:
The starter code provided already implements both the brute-force algorithm using the improved representation and the backtracking algorithm. However, both algorithms rely on the Nqueens.is_valid method to determine if a given placement of queens is valid up to the specified number of rows.
You will need to implement the Nqueens.is_valid method to check that the positions stored in the queens array do not conflict on any column or diagonal. You do not need to check for conflict rows because the implementation prevents two queens from being assigned to the same row (i.e. it uses an array where the index represents the row of the queen).
To check that there is not a column conflict you will need to check that for all queens from row 0 up to but not including row "rows" no two queens have the same column value.
To check that there is not a diagonal conflict, you will need to check that for all queens from row 0 up to but not including row "rows" no two queens share a diagonal. You can implement this a variety of ways but one approach is to check one diagonal by adding the row and column together for each queen. If any two queens have the same value, they are on the same diagonal. You also need to check the other diagonal by subtracting the column from the row. If any two queens have the same value, they are on the same diagonal. You will prove this is true later on in the assignment.
The starter code provided so far:
class Nqueens:
def __init__(self, n):
self.n = n
self.queens =[-1]* n
def print_solution(self):
"""Print the board using the positioning found in the
queens array. If queens[0]=-1 you should print "No solution"
When printing the board use "|" to separate columns and
new lines to separate rows. Mark queens with a "Q" and empty
squares with a ''"""
pass
def is_valid(self, rows):
"""Check if the positioning listed in the queens arrays is valid up to "rows"
Ignore queens with an index >= rows.
A valid position is one which does not share a column or diagonal with
any other queen."""
# Check columns and diagonals for conflicting queens
# Columns: Check that all queens from row 0 up to but not including the current row do not have any 2 queens with the same index value
# Diagonals: Check that all queens from row 0 up to but not including the current row do not have any 2 queens that share a diagonal
# To do so: Check for a diagonal in one direction by adding the row and column together for each queen
# To check for the other diagonal, subtract the column from the row
# In either direction, if any two queens have the same resulting value, they are on the same diagonal
pass
def backtrack(self, row):
if row == self.n:
return self.is_valid(row)
for col in range(self.n):
self.queens[row]= col
if self.is_valid(row +1) and self.backtrack(row +1):
return True
self.queens[row]=-1
return False
def backtracking_find_solution(self):
self.backtrack(0)
def bruteforce(self, row):
if row == self.n:
return self.is_valid(row)
for col in range(self.n):
self.queens[row]= col
if self.bruteforce(row +1):
return True
self.queens[row]=-1
return False
def brute_force_find_solution(self):
self.bruteforce(0)
My current attempt at implementing is_valid():
for i in range(0, rows):
for j in range(0, rows):
if (self.queens[i]== self.queens[j] or abs(self.queens[i]+ i)== abs(self.queens[j]+ j)
or abs(i - self.queens[i])== abs(j - self.queens[j])):
return False
else:
return True
When running unit tests, only test_isValidTrue(self)- i.e. checking for is_valid returning true, is passing. Could someone please take a look at my logic for is_valid()? Thank you!

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!