投票算法
投票算法
多数表决
graph LR
A(votes)--> B[[count]] --> C[[sort]] --> D[[last]] --> E(winner)
投票选择制
- 淘汰第一权重票数最少的
- 过滤淘汰者, 自动补位
- 重复 1 2 直至 winner
graph LR
A(ballots) --> B{只剩一人?} --Yes--> E(winner)
B --No-->C[[淘汰第一权重票最少者]] --> D[[清除淘汰者票]] -->B
验证
> print (winner votes)
"Blue"
> print (winner' ballots)
"Green"
代码
import Data.List
-- 多数表决
votes :: [String]
votes = ["Red", "Blue", "Green", "Blue", "Blue", "Red"]
count :: Eq a => a -> [a] -> Int
count x = length . filter (== x)
result :: Ord a => [a] -> [(Int,a)] -- 计数结果
result vs = sort [(count v vs, v) | v <- nub vs]
winner :: Ord a => [a] -> a
winner = snd . last . result
-- 选择投票制
ballots :: [[String]]
ballots = [["Red", "Green"],
["Blue"],
["Green", "Red", "Blue"],
["Blue", "Green", "Red"],
["Green"]]
elim :: Eq a => a -> [[a]] -> [[a]]
elim x = map (filter (/= x))
rank :: Ord a => [[a]] -> [a] -- 候选人排序
rank = map snd . result . map head
winner' :: Ord a => [[a]] -> a
winner' bss = case rank (filter (/=[]) bss) of
[c] -> c
(c:cs) -> winner' (elim c bss)
参考: Programming in Haskell ch7.7