feat(curriculum): add adjacency convertor lab (#61298)

Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com>
Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
Zaira 2025-08-09 10:45:33 +05:00 committed by GitHub
parent b83c747ccd
commit 6451b572f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 245 additions and 4 deletions

View File

@ -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",

View File

@ -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.

View File

@ -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"
}

View File

@ -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)
```

View File

@ -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" },