fix(curriculum): improve description and tests descriptions (#42408)
* fix: add input and output to tests descriptions * fix: remove results from background code * fix: clarify input and output of function * fix: corrections from review Co-authored-by: Sem Bauke <46919888+Sembauke@users.noreply.github.com> Co-authored-by: Sem Bauke <46919888+Sembauke@users.noreply.github.com>
This commit is contained in:
@ -8,19 +8,19 @@ dashedName: topological-sort
|
|||||||
|
|
||||||
# --description--
|
# --description--
|
||||||
|
|
||||||
Given a mapping between items, and items they depend on, a [topological sort](<https://en.wikipedia.org/wiki/Topological sorting> "wp: Topological sorting") orders items so that no item precedes an item it depends upon. The compiling of a library in the [VHDL](https://en.wikipedia.org/wiki/VHDL "wp: VHDL") language has the constraint that a library must be compiled after any library it depends on.
|
Given a mapping between items, and items they depend on, a topological sort orders items so that no item precedes an item it depends upon. There are two popular algorithms for topological sorting: Kahn's (1962) topological sort and depth-first search.
|
||||||
|
|
||||||
# --instructions--
|
# --instructions--
|
||||||
|
|
||||||
Write a function that will return a valid compile order of VHDL libraries from their dependencies.
|
Write a function that will return a list with valid compile order of libraries from their dependencies.
|
||||||
|
|
||||||
|
- Assume library names are single words.
|
||||||
|
- Items mentioned as only dependents have no dependents of their own, but their order of compiling must be given.
|
||||||
|
- Any self dependencies should be ignored.
|
||||||
|
- Any un-orderable dependencies should be ignored.
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li>Assume library names are single words.</li>
|
|
||||||
<li>Items mentioned as only dependents have no dependents of their own, but their order of compiling must be given.</li>
|
|
||||||
<li>Any self dependencies should be ignored.</li>
|
|
||||||
<li>Any un-orderable dependencies should be ignored.</li>
|
|
||||||
</ul>
|
|
||||||
Use the following data as an example:
|
Use the following data as an example:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
LIBRARY LIBRARY DEPENDENCIES
|
LIBRARY LIBRARY DEPENDENCIES
|
||||||
======= ====================
|
======= ====================
|
||||||
@ -38,16 +38,18 @@ ramlib std ieee
|
|||||||
std_cell_lib ieee std_cell_lib
|
std_cell_lib ieee std_cell_lib
|
||||||
synopsys
|
synopsys
|
||||||
</pre>
|
</pre>
|
||||||
<small>Note: the above data would be un-orderable if, for example, <code>dw04</code> is added to the list of dependencies of <code>dw01</code>.</small>
|
|
||||||
<strong>C.f.:</strong>
|
The compiling of a library in the VHDL language has the constraint that a library must be compiled after any library it depends on. The above data would be un-orderable if, for example, `dw04` is added to the list of dependencies of `dw01`.
|
||||||
<ul>
|
|
||||||
<li><a href="https://rosettacode.org/wiki/Topological sort/Extracted top item" title="Topological sort/Extracted top item" target="_blank">Topological sort/Extracted top item</a>.</li>
|
The input of the function will be a multiline string, each line will consist of the name of the library, followed by its dependencies (if exist).
|
||||||
</ul>
|
|
||||||
There are two popular algorithms for topological sorting:
|
For example:
|
||||||
<ul>
|
|
||||||
<li><a href="https://en.wikipedia.org/wiki/Topological sorting" title="wp: Topological sorting" target="_blank">Kahn's 1962 topological sort</a></li>
|
```js
|
||||||
<li><a href="https://www.embeddedrelated.com/showarticle/799.php" target="_blank">depth-first search</a></li>
|
const libsSimple =
|
||||||
</ul>
|
`aaa bbb
|
||||||
|
bbb`;
|
||||||
|
```
|
||||||
|
|
||||||
# --hints--
|
# --hints--
|
||||||
|
|
||||||
@ -57,28 +59,34 @@ There are two popular algorithms for topological sorting:
|
|||||||
assert(typeof topologicalSort === 'function');
|
assert(typeof topologicalSort === 'function');
|
||||||
```
|
```
|
||||||
|
|
||||||
`topologicalSort` should return correct library order.
|
`topologicalSort(libsSimple)` should return an array.
|
||||||
|
|
||||||
|
```js
|
||||||
|
assert(Array.isArray(topologicalSort(libsSimple)));
|
||||||
|
```
|
||||||
|
|
||||||
|
`topologicalSort(libsSimple)` should return `['bbb', 'aaa']`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
assert.deepEqual(topologicalSort(libsSimple), ['bbb', 'aaa']);
|
assert.deepEqual(topologicalSort(libsSimple), ['bbb', 'aaa']);
|
||||||
```
|
```
|
||||||
|
|
||||||
`topologicalSort` should return correct library order.
|
`topologicalSort(libsVHDL)` should return `['ieee', 'std_cell_lib', 'gtech', 'dware', 'dw07', 'dw06', 'dw05', 'dw02', 'dw01', 'dw04', 'std', 'ramlib', 'synopsys', 'dw03', 'des_system_lib']`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
assert.deepEqual(topologicalSort(libsVHDL), solutionVHDL);
|
assert.deepEqual(topologicalSort(libsVHDL), ['ieee', 'std_cell_lib', 'gtech', 'dware', 'dw07', 'dw06', 'dw05', 'dw02', 'dw01', 'dw04', 'std', 'ramlib', 'synopsys', 'dw03', 'des_system_lib']);
|
||||||
```
|
```
|
||||||
|
|
||||||
`topologicalSort` should return correct library order.
|
`topologicalSort(libsCustom)` should return `['base', 'c', 'd', 'b', 'a']`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
assert.deepEqual(topologicalSort(libsCustom), solutionCustom);
|
assert.deepEqual(topologicalSort(libsCustom), ['base', 'c', 'd', 'b', 'a']);
|
||||||
```
|
```
|
||||||
|
|
||||||
`topologicalSort` should ignore unorderable dependencies.
|
`topologicalSort` should ignore unorderable dependencies.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
assert.deepEqual(topologicalSort(libsUnorderable), solutionUnorderable);
|
assert.deepEqual(topologicalSort(libsUnorderable), ['Base']);
|
||||||
```
|
```
|
||||||
|
|
||||||
# --seed--
|
# --seed--
|
||||||
@ -105,26 +113,17 @@ const libsVHDL =
|
|||||||
std_cell_lib ieee std_cell_lib
|
std_cell_lib ieee std_cell_lib
|
||||||
synopsys`;
|
synopsys`;
|
||||||
|
|
||||||
const solutionVHDL = [
|
|
||||||
'ieee', 'std_cell_lib', 'gtech', 'dware', 'dw07', 'dw06',
|
|
||||||
'dw05', 'dw02', 'dw01', 'dw04', 'std', 'ramlib', 'synopsys',
|
|
||||||
'dw03', 'des_system_lib'
|
|
||||||
];
|
|
||||||
|
|
||||||
const libsCustom =
|
const libsCustom =
|
||||||
`a b c d
|
`a b c d
|
||||||
b c d
|
b c d
|
||||||
d c
|
d c
|
||||||
c base
|
c base
|
||||||
base`;
|
base`;
|
||||||
const solutionCustom = ['base', 'c', 'd', 'b', 'a'];
|
|
||||||
|
|
||||||
const libsUnorderable =
|
const libsUnorderable =
|
||||||
`TestLib Base MainLib
|
`TestLib Base MainLib
|
||||||
MainLib TestLib
|
MainLib TestLib
|
||||||
Base`;
|
Base`;
|
||||||
|
|
||||||
const solutionUnorderable = ['Base'];
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## --seed-contents--
|
## --seed-contents--
|
||||||
|
Reference in New Issue
Block a user