feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
This commit is contained in:
committed by
GitHub
parent
0095583028
commit
ee1e8abd87
@ -3,6 +3,7 @@ id: 587d825c367417b2b2512c90
|
||||
title: 广度优先搜索
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: breadth-first-search
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -81,5 +82,85 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
// Source: http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html
|
||||
function isEquivalent(a, b) {
|
||||
// Create arrays of property names
|
||||
var aProps = Object.getOwnPropertyNames(a);
|
||||
var bProps = Object.getOwnPropertyNames(b);
|
||||
// If number of properties is different,
|
||||
// objects are not equivalent
|
||||
if (aProps.length != bProps.length) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < aProps.length; i++) {
|
||||
var propName = aProps[i];
|
||||
// If values of same property are not equal,
|
||||
// objects are not equivalent
|
||||
if (a[propName] !== b[propName]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// If we made it this far, objects
|
||||
// are considered equivalent
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function bfs(graph, root) {
|
||||
var nodesLen = {};
|
||||
|
||||
return nodesLen;
|
||||
};
|
||||
|
||||
var exBFSGraph = [
|
||||
[0, 1, 0, 0],
|
||||
[1, 0, 1, 0],
|
||||
[0, 1, 0, 1],
|
||||
[0, 0, 1, 0]
|
||||
];
|
||||
console.log(bfs(exBFSGraph, 3));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function bfs(graph, root) {
|
||||
var nodesLen = {};
|
||||
// Set all distances to infinity
|
||||
for (var i = 0; i < graph.length; i++) {
|
||||
nodesLen[i] = Infinity;
|
||||
}
|
||||
nodesLen[root] = 0; // ...except root node
|
||||
var queue = [root]; // Keep track of nodes to visit
|
||||
var current; // Current node traversing
|
||||
// Keep on going until no more nodes to traverse
|
||||
while (queue.length !== 0) {
|
||||
current = queue.shift();
|
||||
// Get adjacent nodes from current node
|
||||
var curConnected = graph[current]; // Get layer of edges from current
|
||||
var neighborIdx = []; // List of nodes with edges
|
||||
var idx = curConnected.indexOf(1); // Get first edge connection
|
||||
while (idx !== -1) {
|
||||
neighborIdx.push(idx); // Add to list of neighbors
|
||||
idx = curConnected.indexOf(1, idx + 1); // Keep on searching
|
||||
}
|
||||
// Loop through neighbors and get lengths
|
||||
for (var j = 0; j < neighborIdx.length; j++) {
|
||||
// Increment distance for nodes traversed
|
||||
if (nodesLen[neighborIdx[j]] === Infinity) {
|
||||
nodesLen[neighborIdx[j]] = nodesLen[current] + 1;
|
||||
queue.push(neighborIdx[j]); // Add new neighbors to queue
|
||||
}
|
||||
}
|
||||
}
|
||||
return nodesLen;
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user