I am looking for an efficient mechanism of randomizing the order of a list with the caveat that duplicate items must have at least 9 items between them, if the length of the list allows, otherwise they must be as far apart as the length of the list will allow.
Firstly this is only for small data-sets there will never be more than 150 items in a list and there could be as few as 1.
So the basic premise is this:
- Take a list of peoples names that may contain a few duplicates (typically there will never be more than 2 instances of any given name but there could be 3 in exceptional circumstances)
- Randomize the list (I have this part working already using a Fisher-Yates (or Knuth shuffle))
- If the list contains duplicates identify those that have less than 9 items between them and adjust if possible to make the gap at least 9 items.
Part 3 is the tricky bit, I have no guidelines on any business rules on how the system should ensure the spacing is correct just that it should be so where possible. At its simplest level an iterative loop that checks for violations and then moves a list element as appropriate would seem the way to go however I can imagine several scenarios where an adjustment made for one pairing then causes an issue with another and so on.
I am not looking for someone to write the code for this, I'm just really after some sensible ideas on a good, efficient way to approach this problem.
The source list will be a IList<string>
in C#
for reference.
Aucun commentaire:
Enregistrer un commentaire