Creating Serendipity with Python
blog.cloudflare.com
blog.cloudflare.com
If the goal is to just know the number of items in each slot, here is the first thing that came to my mind (and does not require any iteration):
groups = [n // g + n % g] + [n // g] * (g - 1)It looks like a blog post that arrives at "this is essentially what the modulo operator does for you", but with an O(n) algorithm.
However, add a step inside that iterative loop that chooses, at random and without repeats, the actual named employees that will be in these meetings (which I believe will be the point), and none of this matters anyway, since you will wind up with something that is worse than O(n) anyway. You intrinsically have to iterate over the entire list and then, somehow, also choose employees at random, which cannot be "free" in computational terms.
I think his very first 3 liner is the most readable version. If we're microoptimizing, his comprehension version also has a lot of extra divisions. I would write something like this instead:
q1, r1 = divmod(n, g)
q2, r2 = divmod(r1, q1)
print(n, [g + q2 + 1] * r2 + [g + q2] * (q1 - r2))Usage:
``` queues = grouper(names, minimum_number) serendipity_teams = roundrobin(queues) ```
> * for i in range(n):
> * groups[i % len(groups)] += 1
this is obvious
> * groups = [1+max(0,n-(i+1))//(n//g) for i in range(n//g)]
this is not
[n//(n//g)+(i<n%g) for i in range(n//g)]
Each one of (n//g) groups gets at least n//(n//g) members plus 1 if the remainder is >i.It woud be a true serendipity experience - meeting people in your company you do not know.
I will have to discuss this tomorrow with HR.
import random
import numpy
people = [...]
min_size = 3
random.shuffle(people)
groupings = numpy.array_split(people,len(people)//min_size)Can't wait? Read this instead: https://blog.cloudflare.com/cloudflare-workers-now-support-c...
I actually wrote that post because the internal blog pipeline at Cloudflare had dried up. By posting it and cajoling people I managed to get a whole load of posts to appear (I bet some of those people were thinking "I can do better than THAT!")
This problem has a name actually - there’s a solution called Bjorklund’s algorithm which was originally used in physics. It was picked up by a music researcher (Godfried Toussaint) who noticed that it generates satisfying rhythms too. https://en.m.wikipedia.org/wiki/Euclidean_rhythm I implemented it myself for Sonic Pi as the spread function.