Question
With Python #import files if needed class constructor initializes the instance attributes N and state def __init__(self, N): pass #Don't forget to delete
With Python
#import files if needed
""" class constructor initializes the instance attributes N and state """ def __init__(self, N): pass #Don't forget to delete this statement
""" returns a formatted string that represents the instance """ def __str__(self): pass #Don't forget to delete this statement
""" Sets the instance attribute state by displaying a menu to the user. The user either enters the state manually or prompts the system to generate a random state. Check if the input state is a valid state. """ def set_state(self): pass #Don't forget to delete this statement
""" generates and returns a valid random state """ def generate_random_state(self): pass #Don't forget to delete this statement """ This is an internal function that takes a state_str as input and return if this is a valid state or not """ def _is_valid(self,state_str): pass #Don't forget to delete this statement
""" This is the primary function of this class. It returns the number of attacking pairs in the board. """ def count_attacking_pairs(self): pass #Don't forget to delete this statement
Part 2: Testing
Do not change this part. This is the test code.
# This is a test code. You can try with different N values and states.
problem = NQueens(5) #create NQueens instance
print(problem) #print the description of the problem
print(problem.count_attacking_pairs()) #print the total number of attacking pairs in the board
3.2 Representation of NQueens Problem You must use the following representation for an NQueens problem. The state of the board will be kept as a string of length N (let's call it as state). We will assume that a single queen is placed on each column of the board. So, the state string will keep the row index for the queens on each column. See the examples below (Figure 1). 1 25 5 Q3 3 Q2 1 2 3 4 5 1 2 3 4 5 6 7 7 Q4 5 6 4. Q 5 Q4 4 Q2 3 Q7 2 2 Q1 1 Q1 1 Q6 N = 5 N = 7 state = "13432" state = "2457613" Figure 1: Examples for state representation. (03- Qn correspond to the Queen in column 1 - N, respectively.) 3 Qs N 3.3 Implementation Notes for NQueens Class You don't need SimpleAl package for this assignment. Your task is to implement the empty functions. You are not supposed to solve the problem. Read the comments in the class definition carefully. Do not forget to delete poss statements when you implement the functions. There will be two instance attributes: o N (type: int): Determines the number of queens and board size ostate (type: str): String representation of the board state (see previous section) You will implement six functions: 1. __init__(self, N): This is the class constructor. It initializes the instance attributes. It takes one parameter N (in addition to obligatory self). Instance attribute N is initialized to the parameter. Instance attribute stote is initialized by calling set_stotel) member function. 2. __str__(self): This function returns a string description of the current instance. 3. set_state(self): This function sets the instance attribute stote by displaying a menu to the user. The user either enters the state manually or prompts the system to generate a random state. Check if the input state is a valid state. (You will need calls to the member functions generate_random_stotel) and_is_valido).) 4. generate_random_state(self): This function returns a valid, randomly generated state string 5. _is_valid(self, state_str): This is an internal function to check whether the given stote_stris a valid state. It returns True/False. (See Section 6 for details) 6. count_attacking_pairs(self): This is the primary function of this class. It returns the number of attacking pairs of queens in the board. (See Section 3.5 for details) 2 . 3.4 Invalid State Examples Casel: State string includes invalid characters (not digit). (e.g.state = "abc1Z", "1xy Z", ...) Case2: The length of the state string is not equal to N. (e.g: N = 4, state = "12345") Case3: State string includes numbers greater than N or less than 1. (eg. N = 5, state = "123742" N = 5, state = "01234")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started