Skip to content

Instantly share code, notes, and snippets.

@ollie314
Created November 14, 2020 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ollie314/939bf37bc5fb69401ed2cbb153a45693 to your computer and use it in GitHub Desktop.
Save ollie314/939bf37bc5fb69401ed2cbb153a45693 to your computer and use it in GitHub Desktop.
Simple implementation of last step of the random condorcet toss
const echo = m => console.log(m);
const rand = (max, min = 0) => Math.ceil(Math.random() * (max - min) + min);
const even = x => x % 2 == 0;
const rank = (stack) => {
const f = (b) => {
if(b.length == 0) return; // shouldn't happens
if(b.length == 1) {
echo(b[0]);
return;
}
const x = rand(b.length - 1);
const p = b[x];
echo(p);
f(b.filter((v, i) => i !== x));
};
stack.forEach(e => f(e));
};
// example
rank([[1], [2, 3, 4], [5, 6, 8]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment