Skip to content

Instantly share code, notes, and snippets.

@jrmoserbaltimore
Last active September 2, 2018 15:26
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 jrmoserbaltimore/646496bcfd75173098ed78cf8e2aa408 to your computer and use it in GitHub Desktop.
Save jrmoserbaltimore/646496bcfd75173098ed78cf8e2aa408 to your computer and use it in GitHub Desktop.
protected virtual void OrderVotes()
{
int priorValue = 0;
Votes.Sort();
/* One vote, always rank 1 */
if (Votes.Count >= 1 && Votes[0].Value != 1)
{
priorValue = Votes[0].Value;
Votes[0] = new Vote(Votes[0].Candidate, 1);
}
/* Start at second vote and close gaps */
for (int i = 1; i < Votes.Count; i++)
{
int newValue = Votes[i].Value;
/* If was tied with previous value, set to previous value's current value */
if (Votes[i].Value == priorValue)
Votes[i] = new Vote(Votes[i].Candidate, Votes[i - 1].Value);
/* Else move up if not strictly sequential */
else if (Votes[i].Value > Votes[i - 1].Value + 1)
Votes[i] = new Vote(Votes[i].Candidate, Votes[i - 1].Value);
priorValue = newValue;
}
}
protected virtual void OrderVotes()
{
Dictionary<int, List<int>> voteMap = new Dictionary<int, List<int>>();
// Value x is at Votes indexes given by voteMap[x]
for (int i = 0; i < Votes.Count; i++)
{
int rank = Votes[i].Value;
if (!voteMap.ContainsKey(rank))
voteMap[rank] = new List<int>();
voteMap[rank].Add(i);
}
int[] ranks = voteMap.Keys.ToArray();
Array.Sort(ranks);
// Close all gaps in rankings
for (int i = 0; i < ranks.Length; i++)
{
for (int j = 0; j < voteMap[i].Count; j++)
{
int n = voteMap[i][j];
if (Votes[n].Value != i + 1)
Votes[n] = new Vote(Votes[n].Candidate, i + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment