mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
fix(curriculum): preventing hardcoding answers in lab-depth-first-search (#65507)
Co-authored-by: zaira <zairahira@gmail.com> Co-authored-by: Zaira <33151350+zairahira@users.noreply.github.com>
This commit is contained in:
+30
@@ -90,6 +90,36 @@ You should have a function named `dfs` that takes two arguments.
|
||||
`) })
|
||||
```
|
||||
|
||||
The `dfs` function should return the correct results.
|
||||
|
||||
```js
|
||||
({ test: () => runPython(`
|
||||
import random
|
||||
|
||||
def solve(g, s):
|
||||
v, q = set(), [s]
|
||||
while q:
|
||||
u = q.pop()
|
||||
if u not in v:
|
||||
v.add(u)
|
||||
q.extend([i for i, x in enumerate(g[u]) if x == 1 and i not in v])
|
||||
return v
|
||||
|
||||
random.seed(0)
|
||||
|
||||
for _ in range(10):
|
||||
n = 6
|
||||
mat = [[0]*n for _ in range(n)]
|
||||
for i in range(n):
|
||||
for j in range(i+1, n):
|
||||
if random.random() > 0.5:
|
||||
mat[i][j] = mat[j][i] = 1
|
||||
|
||||
start = random.randint(0, n-1)
|
||||
assert set(dfs(mat, start)) == solve(mat, start), "Random graph check failed"
|
||||
`) })
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
Reference in New Issue
Block a user