Skip to content

Instantly share code, notes, and snippets.

@Anticubex
Created October 30, 2025 01:48
Show Gist options
  • Select an option

  • Save Anticubex/ee820a9fd618293ca99232f03cf4a0fc to your computer and use it in GitHub Desktop.

Select an option

Save Anticubex/ee820a9fd618293ca99232f03cf4a0fc to your computer and use it in GitHub Desktop.
Uiua Ranked Pairs Voting
# Ranked Pairs Voting
# Uiua 0.18.0-dev.2
EnumerateRows ← ≡⌟˜⨂
GetRankedPairs ← (
⧅>2⇡
⤚≡⌟[⟜-⊃/+⧻♭>∩⌟(≡⊚⌕)°⊟]
∩⌞▽⊸(¬/=⍉) # Filter ties
≡(⨬∘∩⇌⊸(>°⊟))
⊏⍖⊢⍉
)
CausesCycle ← (
˜⊙(∩¤°˜⊟) # AdjMat Frontier Visited
⍢(⟜⊃(⊚/↥˜⊏)⋅⊂|¬↥⋅⊃(=0⧻|/↥∊))
≠0⧻⋅⊙◌
)
AdjancencyMatrix ← ∧(⨬⍜⊡⋅1◌◡CausesCycle)
# Winners must have outgoing
# but no ingoing edges
Winners ← ⊚׬∩/↥⟜⍉
RankedPairs ← ⊏⊸(
⊃⋅⧻EnumerateRows
⊃GetRankedPairs(↯[⟜∘]⊙0)
AdjancencyMatrix
Winners
)
# Test 1
# {"Alice" "Bob" "Charlie"}
# ⍥₃{"Alice" "Bob" "Charlie"}
# ⊂⍥₂{"Bob" "Charlie" "Alice"}
# ⊂⍥₄{"Charlie" "Alice""Bob"}
# RankedPairs# Charlie
# Test 2
{"Alice" "Bob" "Charlie" "David"}
[{"David" "Bob" "Charlie" "Alice"}
{"Bob" "Alice" "Charlie" "David"}]
RankedPairs # Bob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment