Comment detail

議席数をドント方式で (Nested Flatten)
(dhondt 100 '(123 4 56 78))
 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
(defstruct party vote (seat 0))

(defun max-index (lst)
  (let ((max 0) (max-index 0))
    (loop for x in lst for i from 0
          when (< max x) 
          do (setf max-index i) 
             (setf max x))
    max-index))

(defun selection (parties)
  (let* ((selected (max-index 
                     (mapcar #'(lambda (x)
                                 (/ (party-vote x) (1+ (party-seat x)))) 
                             parties)))
         (selected-party (nth selected parties)))
    (setf (party-seat selected-party) (1+ (party-seat selected-party)))
    (loop for p in parties for i from 0
          collect (if (eql i selected) selected-party p))))

(defun dhondt (n votes)
  (labels ((rec (n ps)
                (if (zerop n)
                  ps
                  (rec (1- n) (selection ps)))))
    (mapcar #'party-seat 
            (rec n (mapcar #'(lambda (v) (make-party :vote v))
                           votes)))))

Index

Feed

Other

Link

Pathtraq

loading...