Since Cody, I dislike for-loops, but your solution is way simpler than my own algorithm. Mission accomplished...
Nevertheless, I would hope for a non-looped piece of code. Let's see in phase 3.
Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
%%
dependencies = [
0 0
1 0
];
order = calculation_order(dependencies);
order_correct = [ 1 2 ];
assert(isequal(order_correct,order));
|
2 | Pass |
%%
dependencies = [
0 1 0 0 0
0 0 0 0 0
1 0 0 0 0
1 0 0 0 0
1 1 1 1 0
];
order = calculation_order(dependencies);
order_correct = sortrows([ 2 1 4 3 5 ; 2 1 3 4 5 ]);
assert(isequal(order_correct,order));
|
3 | Pass |
%%
dependencies = [
0 1 0 0 0
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
1 1 1 1 0
];
order = calculation_order(dependencies);
assert(isequal([],order));
|
4 | Pass |
%%
dependencies = [
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1
0 0 0 0 0
];
order = calculation_order(dependencies);
ordered = dependencies(order,order);
assert(~nnz(triu(ordered-diag(diag(ordered)))));
|
5 | Pass |
%%
dependencies_ = [
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
];
order_ = randperm(size(dependencies_,1));
dependencies = dependencies_(order_,order_);
order_ = 0;
order = calculation_order(dependencies);
assert(isequal(size(unique(order,'rows'),1),30));
for ii = 1:size(order,1)
ordered = dependencies(order(ii,:),order(ii,:));
assert(~nnz(triu(ordered-diag(diag(ordered)))));
end
|
607 Solvers
207 Solvers
Side of an equilateral triangle
2600 Solvers
Create matrix with common dates from a number of structs -inner join on dates
12 Solvers
Find two triangular numbers whose sum is input.
72 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!