diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index df6b4d109c5..84191f5f3b6 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -4500,9 +4500,11 @@ "title": "Build a Shortest Path Algorithm", "intro": [""] }, - "lab-adjacency-list-matrix-converter": { - "title": "Build an Adjacency List/Matrix Converter", - "intro": [""] + "lab-adjacency-list-to-matrix-converter": { + "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." + ] }, "workshop-breadth-first-search": { "title": "Build a Breadth First Search", diff --git a/client/src/pages/learn/full-stack-developer/lab-adjacency-list-to-matrix-converter/index.md b/client/src/pages/learn/full-stack-developer/lab-adjacency-list-to-matrix-converter/index.md new file mode 100644 index 00000000000..baf1c9f1aed --- /dev/null +++ b/client/src/pages/learn/full-stack-developer/lab-adjacency-list-to-matrix-converter/index.md @@ -0,0 +1,9 @@ +--- +title: Introduction to the Build an Adjacency List to Matrix Converter +block: lab-adjacency-list-to-matrix-converter +superBlock: full-stack-developer +--- + +## Introduction to the Build an Adjacency List to Matrix Converter + +In this lab, you will implement a function that converts an adjacency list representation of a graph into an adjacency matrix representation. diff --git a/curriculum/challenges/_meta/lab-adjacency-list-to-matrix-converter/meta.json b/curriculum/challenges/_meta/lab-adjacency-list-to-matrix-converter/meta.json new file mode 100644 index 00000000000..47ed35fff62 --- /dev/null +++ b/curriculum/challenges/_meta/lab-adjacency-list-to-matrix-converter/meta.json @@ -0,0 +1,15 @@ +{ + "name": "Build an Adjacency List to Matrix Converter", + "isUpcomingChange": true, + "dashedName": "lab-adjacency-list-to-matrix-converter", + "superBlock": "full-stack-developer", + "helpCategory": "Python", + "challengeOrder": [ + { + "id": "686fdcfe055bcda9f651dd2e", + "title": "Build an Adjacency List to Matrix Converter" + } + ], + "blockLayout": "link", + "blockType": "lab" +} \ No newline at end of file diff --git a/curriculum/challenges/english/25-front-end-development/lab-adjacency-list-to-matrix-converter/686fdcfe055bcda9f651dd2e.md b/curriculum/challenges/english/25-front-end-development/lab-adjacency-list-to-matrix-converter/686fdcfe055bcda9f651dd2e.md new file mode 100644 index 00000000000..6f30466228c --- /dev/null +++ b/curriculum/challenges/english/25-front-end-development/lab-adjacency-list-to-matrix-converter/686fdcfe055bcda9f651dd2e.md @@ -0,0 +1,215 @@ +--- +id: 686fdcfe055bcda9f651dd2e +title: Build an Adjacency List to Matrix Converter +challengeType: 27 +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 a dictionary where each key represents a node, and the corresponding value is a list 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: + +```py +{ + 0: [1, 2], + 1: [2], + 2: [0, 3], + 3: [2] +} +``` + +The corresponding adjacency matrix would be: + +```py +[ + [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 `adjacency_list_to_matrix` to convert an adjacency list to an adjacency matrix. +2. The function should take a dictionary 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, `adjacency_list_to_matrix({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 `adjacency_list_to_matrix`. + +```js +({ + test: () => assert(runPython(` + _Node(_code).has_function("adjacency_list_to_matrix") + `)) +}) +``` + +The `adjacency_list_to_matrix` function should have one parameter. + +```js +({ test: () => assert(runPython(` + import inspect + sig = inspect.signature(adjacency_list_to_matrix) + len(sig.parameters) == 1 + `)) +}) +``` + +The function should correctly determine the number of nodes from the adjacency list. + +```js +({ + test: () => runPython(` + adj_list = {0: [1], 1: [0], 2: []} + result = adjacency_list_to_matrix(adj_list) + assert len(result) == 3 + assert len(result[0]) == 3 + `) +}) +``` + +The function should correctly set matrix values to `1` for existing edges. + +```js +({ + test: () => runPython(` + adj_list = {0: [1], 1: [0]} + result = adjacency_list_to_matrix(adj_list) + assert result[0][1] == 1 + assert result[1][0] == 1 + assert result[0][0] == 0 + assert result[1][1] == 0 + `) +}) +``` + +The function should print each row of the matrix. + +```js +({ + test: () => runPython(` + import io + import sys + + captured_output = io.StringIO() + sys.stdout = captured_output + + adj_list = {0: [1], 1: []} + adjacency_list_to_matrix(adj_list) + + sys.stdout = sys.__stdout__ + output = captured_output.getvalue() + + assert "[0, 1]" in output + assert "[0, 0]" in output + `) +}) +``` + +The function should return the adjacency matrix. + +```js +({ + test: () => runPython(` + adj_list = {0: [1], 1: []} + result = adjacency_list_to_matrix(adj_list) + assert 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 +({ + test: () => runPython(` + adj_list = {0: [1, 2], 1: [2], 2: [0, 3], 3: [2]} + result = adjacency_list_to_matrix(adj_list) + expected = [[0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 1], [0, 0, 1, 0]] + assert result == expected + `) +}) +``` + +When given the adjacency list `{0: [1], 1: [0]}`, the function should return `[[0, 1], [1, 0]]`. + +```js +({ + test: () => runPython(` + adj_list = {0: [1], 1: [0]} + result = adjacency_list_to_matrix(adj_list) + expected = [[0, 1], [1, 0]] + assert result == expected + `) +}) +``` + +When given the adjacency list `{0: [], 1: [], 2: []}`, the function should return `[[0, 0, 0], [0, 0, 0], [0, 0, 0]]`. + +```js +({ + test: () => runPython(` + adj_list = {0: [], 1: [], 2: []} + result = adjacency_list_to_matrix(adj_list) + expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] + assert result == expected + `) +}) +``` + +# --seed-- + +## --seed-contents-- + +```py + +``` + +# --solutions-- + +```py +def adjacency_list_to_matrix(adj_list): + n = len(adj_list) + + adj_matrix = [[0] * n for _ in range(n)] + + for src_node, neighbors in adj_list.items(): + for dest_node in neighbors: + adj_matrix[src_node][dest_node] = 1 + + for row in adj_matrix: + print(row) + + return adj_matrix + +adj_list = { + 0: [1, 2], + 1: [2], + 2: [0, 3], + 3: [2] +} + +matrix = adjacency_list_to_matrix(adj_list) +``` diff --git a/curriculum/superblock-structure/full-stack.json b/curriculum/superblock-structure/full-stack.json index 248d2bc8acd..a4ade6bdc0f 100644 --- a/curriculum/superblock-structure/full-stack.json +++ b/curriculum/superblock-structure/full-stack.json @@ -1397,7 +1397,7 @@ "blocks": [ { "dashedName": "lecture-understanding-graphs-and-trees" }, { "dashedName": "workshop-shortest-path-algorithm" }, - { "dashedName": "lab-adjacency-list-matrix-converter" }, + { "dashedName": "lab-adjacency-list-to-matrix-converter" }, { "dashedName": "workshop-breadth-first-search" }, { "dashedName": "lab-depth-first-search" }, { "dashedName": "review-graphs-and-trees" },