Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Return the factorial of the provided integer.

階乘通常用符號 `n!` 來表示。

例如:`5! = 1 * 2 * 3 * 4 * 5 = 120`
For example: `5! = 1 * 2 * 3 * 4 * 5 = 120`

在這個挑戰中,只有非負整數會作爲參數傳入函數。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ You will be <a href="https://gitpod.io/?autostart=true#https://github.com/freeCo

## Development

Write your code in `mean_var_std.py`. 對於開發,你可以使用 `main.py` 來測試你的代碼。
Write your code in `mean_var_std.py`. 對於開發,你可以使用 `main.py` 來測試你的代碼。 In order to run your code, type `python3 main.py` into the GitPod terminal and hit enter. This will cause the included CPython interpreter to run the `main.py` file.

## 測試

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ Another common intermediate sorting algorithm is merge sort. Like quick sort, me
`mergeSort` 應該是一個函數。

```js
assert(typeof mergeSort == 'function');
assert.isFunction(mergeSort);
```

`mergeSort` 應該返回一個已排序的數組(從小到大)。

```js
assert(
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
if(a[i] > a[i + 1])
return false;
return true;
}

assert.isTrue(
isSorted(
mergeSort([
1,
Expand Down Expand Up @@ -86,29 +93,17 @@ assert.sameMembers(
`mergeSort` 不應使用內置的 `.sort()` 方法。

```js
assert(isBuiltInSortUsed());
```

# --seed--

## --after-user-code--

```js
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
if(a[i] > a[i + 1])
return false;
return true;
}

function isBuiltInSortUsed(){
let sortUsed = false;
Array.prototype.sort = () => sortUsed = true;
mergeSort([0, 1]);
return !sortUsed;
return sortUsed;
}
assert.isFalse(isBuiltInSortUsed());
```

# --seed--

## --seed-contents--

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,117 +16,75 @@ So far, we've learned different ways of creating representations of graphs. What

一個有助於實現廣度優先搜索算法的重要數據結構是隊列。 這是一個數組,你可以添加元素到數組的一端並從另一端刪除元素。 這也稱爲 <dfn>FIFO</dfn> 或 <dfn>先進先出</dfn> 數據結構。

從視覺上來表達,以下是算法正在做的事情。 ![廣度優先搜索算法在樹上移動](https://camo.githubusercontent.com/2f57e6239884a1a03402912f13c49555dec76d06/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f342f34362f416e696d617465645f4246532e676966)
Visually, this is what the algorithm is doing:
<img alt="animation showing the breadth first search algorithm" src='https://cdn.freecodecamp.org/curriculum/coding-interview-prep/breadth-first-search.gif' />

灰色陰影代表一個添加到隊列中的節點,黑色陰影代表一個從隊列中刪除的節點。 注意每當某個節點從隊列中被移除時(節點轉爲黑色),它們的所有鄰居都會被添加到隊列中(節點轉爲灰色)。
The grey shading represents a node getting added into the queue and the black shading represents a node getting removed from the queue. See how every time a node gets removed from the queue (node turns black), all their neighbors get added into the queue (node turns grey).

要實現此算法,你需要輸入圖形結構和你想要開始的節點。
To implement this algorithm, you'll need to input a graph structure and a node you want to start at.

首先,您需要了解距起始節點的距離。 在開始的時候,你想要給所有的距離一個很大的數字,例如 `Infinity`。 這將防止當某個節點無法從你的起始節點到達時出現計算問題。 接下來,您將要從開始節點轉到其鄰居。 這些鄰居是一個邊緣的距離,此時你應該添加一個距離單位到你要跟蹤的距離。
First, you'll want to be aware of the distances from, or number of edges away from, the start node. You'll want to start all your distances with some large number, like `Infinity`. This prevents counting issues for when a node may not be reachable from your start node. Next, you'll want to go from the start node to its neighbors. These neighbors are one edge away and at this point you should add one unit of distance to the distances you're keeping track of.

# --instructions--

編寫一個函數 `bfs()` ,它將一個鄰接矩陣圖(二維數組)和一個標記爲根的節點標籤作爲參數。 節點標籤只是 `0` `n - 1` 之間節點的整數值,其中 `n` 是圖中節點的總數。
Write a function `bfs()` that takes an adjacency matrix graph (a two-dimensional array) and a node label root as parameters. The node label will just be the integer value of the node between `0` and `n - 1`, where `n` is the total number of nodes in the graph.

你的函數將輸出 JavaScript 對象鍵值對,即節點及其與根的距離。 如果無法到達節點,則其距離應爲`Infinity`
Your function will output a JavaScript object key-value pairs with the node and its distance from the root. If the node could not be reached, it should have a distance of `Infinity`.

# --hints--

輸入圖 `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]`,起始節點爲 `1`,應該返回 `{0: 1, 1: 0, 2: 1, 3: 2}`
The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]` with a start node of `1` should return `{0: 1, 1: 0, 2: 1, 3: 2}`

```js
assert(
(function () {
var graph = [
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 1],
[0, 0, 1, 0]
];
var results = bfs(graph, 1);
return isEquivalent(results, { 0: 1, 1: 0, 2: 1, 3: 2 });
})()
);
var graph = [
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 1],
[0, 0, 1, 0]
];
var results = bfs(graph, 1);
assert.deepEqual(results, { 0: 1, 1: 0, 2: 1, 3: 2 });
```

輸入圖 `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]`,起始節點爲 `1`,應該返回 `{0: 1, 1: 0, 2: 1, 3: Infinity}`
The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]` with a start node of `1` should return `{0: 1, 1: 0, 2: 1, 3: Infinity}`

```js
assert(
(function () {
var graph = [
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 0]
];
var results = bfs(graph, 1);
return isEquivalent(results, { 0: 1, 1: 0, 2: 1, 3: Infinity });
})()
);
var graph = [
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 0]
];
var results = bfs(graph, 1);
assert.deepEqual(results, { 0: 1, 1: 0, 2: 1, 3: Infinity });
```

輸入圖 `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]`,起始節點爲 `0`,應該返回 `{0: 0, 1: 1, 2: 2, 3: 3}`
The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]` with a start node of `0` should return `{0: 0, 1: 1, 2: 2, 3: 3}`

```js
assert(
(function () {
var graph = [
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 1],
[0, 0, 1, 0]
];
var results = bfs(graph, 0);
return isEquivalent(results, { 0: 0, 1: 1, 2: 2, 3: 3 });
})()
);
var graph = [
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 1],
[0, 0, 1, 0]
];
var results = bfs(graph, 0);
assert.deepEqual(results, { 0: 0, 1: 1, 2: 2, 3: 3 });
```

輸入圖 `[[0, 1], [1, 0]]`,其實節點爲 `0`,應該返回 `{0: 0, 1: 1}`
The input graph `[[0, 1], [1, 0]]` with a start node of `0` should return `{0: 0, 1: 1}`

```js
assert(
(function () {
var graph = [
[0, 1],
[1, 0]
];
var results = bfs(graph, 0);
return isEquivalent(results, { 0: 0, 1: 1 });
})()
);
var graph = [
[0, 1],
[1, 0]
];
var results = bfs(graph, 0);
assert.deepEqual(results, { 0: 0, 1: 1 });
```

# --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
Expand Down
Loading