Skip to content

backtracking-3#1080

Open
shinjaneegupta wants to merge 1 commit intosuper30admin:masterfrom
shinjaneegupta:master
Open

backtracking-3#1080
shinjaneegupta wants to merge 1 commit intosuper30admin:masterfrom
shinjaneegupta:master

Conversation

@shinjaneegupta
Copy link

No description provided.

@super30admin
Copy link
Owner

N-Queens (NQueens.py)

Your solution is well-implemented and correctly solves the problem. Here are some points to consider for improvement:

  1. Efficiency in Validity Check: The current validity check uses three while loops to check the column and two diagonals. This is correct, but note that we are only checking above the current row (since we are placing row by row). However, the loops are efficient and correct.

  2. Use of Boolean Board: Using a 2D boolean list to represent the board is acceptable. However, note that in Python, we might consider using a list of integers to represent queen positions to save memory, but for n up to 9, the current approach is fine.

  3. Code Clarity: The code is clear and easy to follow. You might consider adding comments to explain the backtracking steps, especially for learners who are new to the problem.

  4. Alternative Approaches: There are more optimized ways to check for attacks (e.g., using sets for columns and diagonals) which can reduce the validity check to O(1). However, for the problem constraints (n<=9), the current approach is efficient enough.

  5. Pythonic Style: The code is written in a Pythonic style. One small suggestion: in the isValid function, you can avoid reassigning r and c by using local variables in each loop. Alternatively, you could use for loops with range to traverse upwards, but the while loops are fine.

Overall, great job! Your solution is correct and efficient.

VERDICT: PASS


Word Search (WordSearch.py)

Strengths:

  • The student has implemented the DFS backtracking approach which is correct in theory.
  • The code structure is clear and follows the reference solution closely.
  • The use of directions and recursion is appropriate.

Areas for improvement:

  • The main issue is using a string "#" instead of a character '#' for marking visited cells. This causes the visited check to fail because board[i][j] == '#' will be false when the cell is set to the string "#". This is a critical bug that will make the solution incorrect.
  • It is important to be consistent with data types. The board contains characters, so we should use characters for assignment as well.
  • Also, note that the student has a typo in the code: in the DFS function, the line board[i][j] = "#" should be board[i][j] = '#' (with a single quote).
  • Another minor point: the directions are defined as [[0,-1], [0,1], [-1,0], [1,0]] which correspond to left, right, up, down. This is correct.

Recommendation:

  • Change board[i][j] = "#" to board[i][j] = '#' to fix the visited marker.
  • Test the code with the provided examples to ensure it works.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants