Slimmed down and perfected victories_and_ties function in condorcet.rb to match the...
authorJustin Sharps <jlsharps@alum.mit.edu>
Tue, 21 Aug 2007 18:02:07 +0000 (18:02 +0000)
committerJustin Sharps <jlsharps@alum.mit.edu>
Tue, 21 Aug 2007 18:02:07 +0000 (18:02 +0000)
git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@48 1440c7f4-e209-0410-9a04-881b5eb134a8

lib/rubyvote/condorcet.rb

index d0210ef..0c6fd56 100644 (file)
@@ -123,33 +123,21 @@ class CondorcetResult < ElectionResult
   end
   
   def victories_and_ties
-    victors = Array.new
-    ties = Array.new
-    victories = Hash.new
+    victories_ties = {}
     candidates = @matrix.keys.sort
     
     candidates.each do |candidate|
       candidates.each do |challenger|
         next if candidate == challenger
         diff = @matrix[candidate][challenger] - @matrix[challenger][candidate]
-        if diff > 0 
-          victors << [candidate, challenger, diff]
-        elsif diff == 0 && ties.include?([challenger, candidate]) == false
-          ties << [candidate, challenger] 
+        victories_ties[candidate] = {} unless victories_ties.key?(candidate)
+        if diff >= 0
+          victories_ties[candidate][challenger] = diff
         end
       end
-    end  
-    
-    victors.each do |list|
-      if victories.has_key?(list[0])
-        victories[list[0]][list[1]] = list[2]       
-      else
-        victories[list[0]] = Hash.new
-        victories[list[0]][list[1]] = list[2]
-      end
     end
     
-    return victoriesties    
+    return victories_ties    
   end
 
   def ranked_candidates

Benjamin Mako Hill || Want to submit a patch?