Skip to content

Instantly share code, notes, and snippets.

@MarcPer
Last active December 9, 2015 14:42
Show Gist options
  • Save MarcPer/f5750950f8cb7e7cc8a7 to your computer and use it in GitHub Desktop.
Save MarcPer/f5750950f8cb7e7cc8a7 to your computer and use it in GitHub Desktop.
Group tasks by priority and apply.
function orderTasks(num) {
var out = {},
fib, i = 0,
sum = idx = ct = 0,
tasks = Array.apply(null, {length: num}).map(Number.call, Number).reverse(); // http://stackoverflow.com/a/20066663
while (sum < num) {
idx++;
sum += getFib(idx);
}
for (i = 1; i <= idx; i++) {
fib = getFib(i);
out[fib] = tasks.slice(ct, ct + fib);
ct += fib;
}
return out;
}
// Get n-th Fibonacci number where the 0th and 1st are 1 and 2nd is 2
function getFib(n) {
if (n < 3)
return n || 1;
return getFib(n-2) + getFib(n-1);
}
@MarcPer
Copy link
Author

MarcPer commented Dec 9, 2015

Use orderTasks to group tasks according to priority.

Tasks are initially ordered sequentially by priority, so it suffices to provide the number of tasks. This function then divides them in priority groups, where the number of tasks in each group follows the Fibonacci sequence.

For instance, if 14 tasks are to be divided, this will be the final distribution (note the task number is 0-indexed):
1: 14
2: 13, 12
3: 11, 10, 9
5: 8, 7, 6, 5, 4
8: 3, 2, 1

Note that the last group (8) is not completely filled, because groups are filled starting from 1 and only go until there are no tasks left.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment