@@ 0,0 1,26 @@
+:Namespace vote
+ Ballot←{
+ voter←{
+ ⍺:?⍨⍵
+ n←?⍵
+ (⍳n)@(n↑?⍨⍵)⊢⍵⍴0
+ }
+ ,⍨∘≢⌸(⍺>0)voter¨⍵⍴|⍺
+ }
+ IRV←{
+ ⍵ IRVruns (⊂∘⍋⌷⊢)⍵[;1]{⍺,+⌿⍵}⌸⍨1⍳⍨¨⍵[;2]
+ }
+ IRVruns←{
+ eliminate←{
+ ⍝ A loser rating of 0 is worst,
+ ⍝ so give it a large value
+ res←⍵[;1],⍪↓⍉0@⍺⊢⍉r-r>⍤1 0⊢(⌈⌿,r)@(=∘0)(r←↑⍵[;2])[;⍺]
+ ⍝ remove the blank votes now
+ res⌿⍨~∨/res⍷⍨⊂0⍴⍨⊃⌽⍴r
+ }
+ 1=≢⍵:⊃⍵ ⍝ We have a winner
+ losers←⍵[(⍸⌊⌿⍷⊢)⍵[;2];1]
+ ⍵=⍥≢losers:⊂⍵ ⍝ it was a tie
+ (⊂⍵),IRV⊃eliminate⌿losers,⊂⍺ ⍝ Calculate next rounds with the loses removed
+ }
+:EndNamespace