From 8ba3fbfd1ba0649bf926d0f84f68535259d4ecbf Mon Sep 17 00:00:00 2001 From: majestic-owl448 <26656284+majestic-owl448@users.noreply.github.com> Date: Tue, 24 Feb 2026 22:54:23 +0100 Subject: [PATCH] feat(curriculum): add Adjacency List to Matrix Converter lab to JSv9 (#65811) Co-authored-by: Zaira <33151350+zairahira@users.noreply.github.com> --- client/i18n/locales/english/intro.json | 6 + .../698b42492a7f5f737895892e.md | 178 ++++++++++++++++++ ...adjacency-list-to-matrix-converter-js.json | 15 ++ .../structure/superblocks/javascript-v9.json | 5 +- 4 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 curriculum/challenges/english/blocks/lab-adjacency-list-to-matrix-converter-js/698b42492a7f5f737895892e.md create mode 100644 curriculum/structure/blocks/lab-adjacency-list-to-matrix-converter-js.json diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index 55c8ae133fc..9d9b684c159 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -3323,6 +3323,12 @@ "In this lesson, you will learn about fundamental data structures like graphs, trees, and their practical applications in computer science." ] }, + "lab-adjacency-list-to-matrix-converter-js": { + "title": "Build an Adjacency List to Matrix Converter", + "intro": [ + "In this lab, you will implement a function that converts an adjacency list representation of a graph into an adjacency matrix representation." + ] + }, "lecture-understanding-functional-programming": { "title": "Understanding Functional Programming", "intro": [ diff --git a/curriculum/challenges/english/blocks/lab-adjacency-list-to-matrix-converter-js/698b42492a7f5f737895892e.md b/curriculum/challenges/english/blocks/lab-adjacency-list-to-matrix-converter-js/698b42492a7f5f737895892e.md new file mode 100644 index 00000000000..5e17159040c --- /dev/null +++ b/curriculum/challenges/english/blocks/lab-adjacency-list-to-matrix-converter-js/698b42492a7f5f737895892e.md @@ -0,0 +1,178 @@ +--- +id: 698b42492a7f5f737895892e +title: Build an Adjacency List to Matrix Converter +challengeType: 26 +dashedName: build-an-adjacency-list-to-matrix-converter +--- + +# --description-- + +In this lab, you will build a function that converts an adjacency list representation of a graph into an adjacency matrix. An adjacency list is an object where each key represents a node, and the corresponding value is an array of nodes that the key node is connected to. An adjacency matrix is a 2D array where the entry at position `[i][j]` is `1` if there's an edge from node `i` to node `j`, and `0` otherwise. + +For example, given the adjacency list: + +```js +{ + 0: [1, 2], + 1: [2], + 2: [0, 3], + 3: [2] +} +``` + +The corresponding adjacency matrix would be: + +```js +[ + [0, 1, 1, 0], + [0, 0, 1, 0], + [1, 0, 0, 1], + [0, 0, 1, 0] +] +``` + +**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab. + +**User Stories:** + +1. You should define a function named `adjacencyListToMatrix` to convert an adjacency list to an adjacency matrix. +2. The function should take an object representing the adjacency list of an unweighted (either undirected or directed) graph as its argument. +3. The function should: + - Convert the adjacency list to an adjacency matrix. + - Print each row in the adjacency matrix. + - Return the adjacency matrix. + +For example, `adjacencyListToMatrix({0: [2], 1: [2, 3], 2: [0, 1, 3], 3: [1, 2]})` should print: + +```md +[0, 0, 1, 0] +[0, 0, 1, 1] +[1, 1, 0, 1] +[0, 1, 1, 0] +``` + +and return `[[0, 0, 1, 0], [0, 0, 1, 1], [1, 1, 0, 1], [0, 1, 1, 0]]`. + +# --hints-- + +You should define a function named `adjacencyListToMatrix`. + +```js +assert.isFunction(adjacencyListToMatrix); +``` + +The `adjacencyListToMatrix` function should have one parameter. + +```js +assert.lengthOf(adjacencyListToMatrix, 1); +``` + +The function should correctly determine the number of nodes from the adjacency list. + +```js +const adjList = {0: [1], 1: [0], 2: []}; +const result = adjacencyListToMatrix(adjList); +assert.lengthOf(result, 3); +result.forEach(arr => assert.lengthOf(arr, 3)); +``` + +The function should correctly set matrix values to `1` for existing edges. + +```js +const adjList = {0: [1], 1: [0]}; +const result = adjacencyListToMatrix(adjList); +const expected = [[0, 1], [1, 0]]; +assert.sameDeepOrderedMembers(result, expected); +``` + +The function should print each row of the matrix. + +```js +const spy = __helpers.spyOn(console, "log"); +try { + const adjList = {0: [1], 1: []} + adjacencyListToMatrix(adjList); + const args = spy.calls; + assert.sameDeepOrderedMembers(args[0], [ [0, 1] ]); + assert.sameDeepOrderedMembers(args[1], [ [0, 0] ]); +} catch (e) { + assert.fail(e); +} finally { + spy.restore(); +} +``` + +The function should return the adjacency matrix. + +```js +const adjList = {0: [1], 1: []}; +const result = adjacencyListToMatrix(adjList); +assert.sameDeepOrderedMembers(result, [[0, 1], [0, 0]]); +``` + +When given the adjacency list `{0: [1, 2], 1: [2], 2: [0, 3], 3: [2]}`, the function should return `[[0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 1], [0, 0, 1, 0]]`. + +```js +const adjList = {0: [1, 2], 1: [2], 2: [0, 3], 3: [2]}; +const result = adjacencyListToMatrix(adjList); +const expected = [[0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 1], [0, 0, 1, 0]]; +assert.sameDeepOrderedMembers(result, expected); +``` + +When given the adjacency list `{0: [1], 1: [0]}`, the function should return `[[0, 1], [1, 0]]`. + +```js +const adjList = {0: [1], 1: [0]}; +const result = adjacencyListToMatrix(adjList); +const expected = [[0, 1], [1, 0]]; +assert.sameDeepOrderedMembers(result, expected); +``` + +When given the adjacency list `{0: [], 1: [], 2: []}`, the function should return `[[0, 0, 0], [0, 0, 0], [0, 0, 0]]`. + +```js +const adjList = {0: [], 1: [], 2: []}; +const result = adjacencyListToMatrix(adjList); +const expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]; +assert.sameDeepOrderedMembers(result, expected); +``` + +# --seed-- + +## --seed-contents-- + +```js + +``` + +# --solutions-- + +```js +function adjacencyListToMatrix(adjList) { + const n = Object.keys(adjList).length; + + const adjMatrix = Array.from({ length: n }, () => Array(n).fill(0)); + + for (const [srcStr, neighbors] of Object.entries(adjList)) { + const srcNode = Number(srcStr); + for (const destNode of neighbors) { + adjMatrix[srcNode][destNode] = 1; + } + } + + for (const row of adjMatrix) { + console.log(row); + } + + return adjMatrix; +} + +const adjList = { + 0: [1, 2], + 1: [2], + 2: [0, 3], + 3: [2] +}; + +const matrix = adjacencyListToMatrix(adjList); +``` diff --git a/curriculum/structure/blocks/lab-adjacency-list-to-matrix-converter-js.json b/curriculum/structure/blocks/lab-adjacency-list-to-matrix-converter-js.json new file mode 100644 index 00000000000..0eee776fa88 --- /dev/null +++ b/curriculum/structure/blocks/lab-adjacency-list-to-matrix-converter-js.json @@ -0,0 +1,15 @@ +{ + "name": "Build an Adjacency List to Matrix Converter", + "isUpcomingChange": true, + "dashedName": "lab-adjacency-list-to-matrix-converter-js", + "helpCategory": "JavaScript", + "blockLayout": "link", + "challengeOrder": [ + { + "id": "698b42492a7f5f737895892e", + "title": "Build an Adjacency List to Matrix Converter" + } + ], + "blockLabel": "lab", + "usesMultifileEditor": true +} diff --git a/curriculum/structure/superblocks/javascript-v9.json b/curriculum/structure/superblocks/javascript-v9.json index c78c3d7e5f9..7c014d38840 100644 --- a/curriculum/structure/superblocks/javascript-v9.json +++ b/curriculum/structure/superblocks/javascript-v9.json @@ -310,7 +310,10 @@ { "dashedName": "graphs-and-trees", "comingSoon": true, - "blocks": ["lecture-understanding-graphs-and-trees-js"] + "blocks": [ + "lecture-understanding-graphs-and-trees-js", + "lab-adjacency-list-to-matrix-converter-js" + ] }, { "dashedName": "dynamic-programming",