From 5fd86514e6a43979ffce8a14654392caf638ad66 Mon Sep 17 00:00:00 2001 From: Zaira <33151350+zairahira@users.noreply.github.com> Date: Tue, 21 Oct 2025 15:02:58 +0500 Subject: [PATCH] feat(curriculum): add n-queens lab (#62420) Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com> --- client/i18n/locales/english/intro.json | 7 +- .../lab-n-queens-problem/index.md | 9 + .../68dbc6b7e8f794a2ae69bc74.md | 178 ++++++++++++++++++ .../blocks/lab-n-queens-problem.json | 15 ++ .../superblocks/full-stack-developer.json | 1 + 5 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 client/src/pages/learn/full-stack-developer/lab-n-queens-problem/index.md create mode 100644 curriculum/challenges/english/blocks/lab-n-queens-problem/68dbc6b7e8f794a2ae69bc74.md create mode 100644 curriculum/structure/blocks/lab-n-queens-problem.json diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index 226c4185475..9387c771191 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -4743,7 +4743,12 @@ "In this lab, you will implement the Depth-First Search Algorithm." ] }, - "lab-n-queens-problem": { "title": "", "intro": [""] }, + "lab-n-queens-problem": { + "title": "Implement the N-Queens Problem", + "intro": [ + "In this lab, you will implement a solution for the N-Queens problem." + ] + }, "review-graphs-and-trees": { "title": "Graphs and Trees Review", "intro": [ diff --git a/client/src/pages/learn/full-stack-developer/lab-n-queens-problem/index.md b/client/src/pages/learn/full-stack-developer/lab-n-queens-problem/index.md new file mode 100644 index 00000000000..71d5c742a22 --- /dev/null +++ b/client/src/pages/learn/full-stack-developer/lab-n-queens-problem/index.md @@ -0,0 +1,9 @@ +--- +title: Introduction to the Implement the N-Queens Algorithm +block: lab-n-queens-problem +superBlock: full-stack-developer +--- + +## Introduction to the Implement the N-Queens Algorithm + +In this lab, you will implement a solution for the N-Queens problem. diff --git a/curriculum/challenges/english/blocks/lab-n-queens-problem/68dbc6b7e8f794a2ae69bc74.md b/curriculum/challenges/english/blocks/lab-n-queens-problem/68dbc6b7e8f794a2ae69bc74.md new file mode 100644 index 00000000000..5be43c2a3f5 --- /dev/null +++ b/curriculum/challenges/english/blocks/lab-n-queens-problem/68dbc6b7e8f794a2ae69bc74.md @@ -0,0 +1,178 @@ +--- +id: 68dbc6b7e8f794a2ae69bc74 +title: Implement the N-Queens Algorithm +challengeType: 27 +dashedName: step-1 +--- + +# --description-- + +The N-Queens problem asks you to place N queens on an N×N chessboard so that no two queens attack each other (no two share a row, column, or diagonal). + +For example, if there is a 4x4 board, one valid arrangement is: + +```md +[1, 3, 0, 2] +``` + +That means that in row 0, the queen is placed in column 1; in row 1, the queen is placed in column 3; in row 2, the queen is placed in column 0; and in row 3, the queen is placed in column 2. + +Visually, this arrangement looks like: + +```md +. Q . . +. . . Q +Q . . . +. . Q . +``` + +Where `Q` represents a queen and `.` represents an empty square. + +In this lab, you will implement the N-Queens problem solver using the depth-first search approach. + + +**Objective**: Fulfill the user stories below and get all the tests to pass to complete the lab. + +**User Stories:** + +1. You should have a function named `dfs_n_queens`. +2. The function should accept exactly one argument: an integer `n`. +3. If `n` is less than `1`, the function should return an empty list (`[]`). +4. The function should return a list of solutions; each solution is itself a list of length `n`, where the element at index `i` is the column index (0-based) of the queen in row `i`. + +# --hints-- + +You should have a function named `dfs_n_queens` that takes one argument. + +```js +({ test: () => runPython(` + import inspect + assert inspect.isfunction(dfs_n_queens) + sig = inspect.signature(dfs_n_queens) + assert len(sig.parameters) == 1 +`) }) +``` + +If `n` is less than `1`, the function should return an empty list. + +```js +({ test: () => runPython(` + assert dfs_n_queens(0) == [] + assert dfs_n_queens(-1) == [] + assert dfs_n_queens(-5) == [] +`) }) +``` + +The function should return a list of solutions, where each solution is a list of length `n`. + +```js +({ test: () => runPython(` + result = dfs_n_queens(4) + assert isinstance(result, list) + for solution in result: + assert isinstance(solution, list) + assert len(solution) == 4 +`) }) +``` + +`dfs_n_queens(1)` should return `[[0]]`. + +```js +({ test: () => runPython(` + assert dfs_n_queens(1) == [[0]] + assert dfs_n_queens(5) == [[0, 2, 4, 1, 3], [0, 3, 1, 4, 2], [1, 3, 0, 2, 4], [1, 4, 2, 0, 3], [2, 0, 3, 1, 4], [2, 4, 1, 3, 0], [3, 0, 2, 4, 1], [3, 1, 4, 2, 0], [4, 1, 3, 0, 2], [4, 2, 0, 3, 1]] +`) }) +``` + +`dfs_n_queens(2)` should return `[]`. + +```js +({ test: () => runPython(` + assert dfs_n_queens(2) == [] + assert dfs_n_queens(5) == [[0, 2, 4, 1, 3], [0, 3, 1, 4, 2], [1, 3, 0, 2, 4], [1, 4, 2, 0, 3], [2, 0, 3, 1, 4], [2, 4, 1, 3, 0], [3, 0, 2, 4, 1], [3, 1, 4, 2, 0], [4, 1, 3, 0, 2], [4, 2, 0, 3, 1]] +`) }) +``` + +`dfs_n_queens(3)` should return `[]`. + +```js +({ test: () => runPython(` + assert dfs_n_queens(3) == [] + assert dfs_n_queens(5) == [[0, 2, 4, 1, 3], [0, 3, 1, 4, 2], [1, 3, 0, 2, 4], [1, 4, 2, 0, 3], [2, 0, 3, 1, 4], [2, 4, 1, 3, 0], [3, 0, 2, 4, 1], [3, 1, 4, 2, 0], [4, 1, 3, 0, 2], [4, 2, 0, 3, 1]] +`) }) +``` + +`dfs_n_queens(4)` should return `[[1, 3, 0, 2], [2, 0, 3, 1]]`. + +```js +({ test: () => runPython(` + assert dfs_n_queens(4) == [[1, 3, 0, 2], [2, 0, 3, 1]] + assert dfs_n_queens(5) == [[0, 2, 4, 1, 3], [0, 3, 1, 4, 2], [1, 3, 0, 2, 4], [1, 4, 2, 0, 3], [2, 0, 3, 1, 4], [2, 4, 1, 3, 0], [3, 0, 2, 4, 1], [3, 1, 4, 2, 0], [4, 1, 3, 0, 2], [4, 2, 0, 3, 1]] +`) }) +``` + +`dfs_n_queens(5)` should return `[[0, 2, 4, 1, 3], [0, 3, 1, 4, 2], [1, 3, 0, 2, 4], [1, 4, 2, 0, 3], [2, 0, 3, 1, 4], [2, 4, 1, 3, 0], [3, 0, 2, 4, 1], [3, 1, 4, 2, 0], [4, 1, 3, 0, 2], [4, 2, 0, 3, 1]]`. + +```js +({ test: () => runPython(` + assert dfs_n_queens(5) == [[0, 2, 4, 1, 3], [0, 3, 1, 4, 2], [1, 3, 0, 2, 4], [1, 4, 2, 0, 3], [2, 0, 3, 1, 4], [2, 4, 1, 3, 0], [3, 0, 2, 4, 1], [3, 1, 4, 2, 0], [4, 1, 3, 0, 2], [4, 2, 0, 3, 1]] + assert dfs_n_queens(3) == [] +`) }) +``` + +`len(dfs_n_queens(5))` should be `10`. + +```js +({ test: () => runPython(` + assert len(dfs_n_queens(5)) == 10 + assert len(dfs_n_queens(6)) == 4 +`) }) +``` + +`len(dfs_n_queens(8))` should be `92`. + +```js +({ test: () => runPython(` + assert len(dfs_n_queens(8)) == 92 + assert len(dfs_n_queens(5)) == 10 + +`) }) +``` + +# --seed-- + +## --seed-contents-- + +```py + +``` + +# --solutions-- + +```py +def dfs_n_queens(n): + if n < 1: + return [] + + results = [] + + def is_safe(queens, row, col): + for r, c in enumerate(queens): + if c == col or abs(row - r) == abs(col - c): + return False + return True + + def dfs(queens): + row = len(queens) + if row == n: + results.append(queens[:]) + return + for col in range(n): + if is_safe(queens, row, col): + queens.append(col) + dfs(queens) + queens.pop() + + dfs([]) + return results +``` diff --git a/curriculum/structure/blocks/lab-n-queens-problem.json b/curriculum/structure/blocks/lab-n-queens-problem.json new file mode 100644 index 00000000000..512f3bd6ef7 --- /dev/null +++ b/curriculum/structure/blocks/lab-n-queens-problem.json @@ -0,0 +1,15 @@ +{ + "name": "Implement the N-Queens Algorithm", + "isUpcomingChange": true, + "dashedName": "lab-n-queens-problem", + "helpCategory": "Python", + "blockLayout": "link", + "challengeOrder": [ + { + "id": "68dbc6b7e8f794a2ae69bc74", + "title": "Implement the N-Queens Algorithm" + } + ], + "blockType": "lab", + "usesMultifileEditor": true +} diff --git a/curriculum/structure/superblocks/full-stack-developer.json b/curriculum/structure/superblocks/full-stack-developer.json index 82891cfc00c..6215a24390f 100644 --- a/curriculum/structure/superblocks/full-stack-developer.json +++ b/curriculum/structure/superblocks/full-stack-developer.json @@ -775,6 +775,7 @@ "lab-adjacency-list-to-matrix-converter", "workshop-breadth-first-search", "lab-depth-first-search", + "lab-n-queens-problem", "review-graphs-and-trees", "quiz-graphs-and-trees" ]