tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
10
Task/Priority-queue/Mathematica/priority-queue-1.math
Normal file
10
Task/Priority-queue/Mathematica/priority-queue-1.math
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
push = Function[{queue, priority, item},
|
||||
queue = SortBy[Append[queue, {priority, item}], First], HoldFirst];
|
||||
pop = Function[queue,
|
||||
If[Length@queue == 0, Null,
|
||||
With[{item = queue[[-1, 2]]}, queue = Most@queue; item]],
|
||||
HoldFirst];
|
||||
peek = Function[queue,
|
||||
If[Length@queue == 0, Null, Max[queue[[All, 1]]]], HoldFirst];
|
||||
merge = Function[{queue1, queue2},
|
||||
SortBy[Join[queue1, queue2], First], HoldAll];
|
||||
11
Task/Priority-queue/Mathematica/priority-queue-2.math
Normal file
11
Task/Priority-queue/Mathematica/priority-queue-2.math
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
queue = {};
|
||||
push[queue, 3, "Clear drains"];
|
||||
push[queue, 4, "Feed cat"];
|
||||
push[queue, 5, "Make tea"];
|
||||
push[queue, 1, "Solve RC tasks"];
|
||||
push[queue, 2, "Tax return"];
|
||||
Print[peek[queue]];
|
||||
Print[pop[queue]];
|
||||
queue1 = {};
|
||||
push[queue1, 6, "Drink tea"];
|
||||
Print[merge[queue, queue1]];
|
||||
Loading…
Add table
Add a link
Reference in a new issue