-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ballot.aplf
28 lines (24 loc) · 1.24 KB
/
Ballot.aplf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
res←candidates Ballot voters;rankings;abs;shuffled;unique
⍝ Create a simulated ballot for testing purposes.
⍝ Ballots are 2D nested arrays `ballot`, where
⍝ * Each element of ballot[;2] is a vector of preferences
⍝ * Each element of ballot[;1] is the number of voters with that preference
⍝ Generate the preliminary list (1 2 3 ... candidates) or (1 2 3 ... n 0 0 0) to use as rankings
:If (×candidates)=0
⎕SIGNAL 11
:ElseIf (×candidates)=1
⍝ All the candidates must be ranked
rankings←voters⍴⊂⍳candidates
:Else
abs←|candidates
⍝ Pick a random number ran from 0 to abs-1 representing the number of empty choices
⍝ Then concatenate ⍳(not empty choices) to ran/0 for each voter
rankings←{⍵+(⍳(abs-ran)),(ran←¯1+?abs)/0}¨voters⍴⊂abs⍴0
:EndIf
⍝ An alternative solution which covers both the cases (|candidates)=1 is
⍝ {⍵+(⍳((|v2)-ran)),(ran←(1-0⌈×v2)ׯ1+?|v2)/0}¨3⍴⊂(|v2)⍴0
⍝ However, the If..ElseIf..Else construct is more readable
⍝ Shuffle the rankings. More simply: ballot[(⍴ballot)?⍴ballot] for each ballot in rankings
shuffled←({⊂?⍨⍴⍵}⌷⊢)¨rankings
⍝ Generate table of rankings
res←{⍺,⍨≢⍵}⌸shuffled