Slimmed down and perfected victories_and_ties function in condorcet.rb to match the...
[rubyvote] / lib / rubyvote / condorcet.rb
index 85cb897df9dc975e8c00238bfc81388d97aba417..0c6fd5647bd921e5543838b42ddc297b57ca72cd 100644 (file)
@@ -1,10 +1,6 @@
 # election library -- a ruby library for elections
 # copyright © 2005 MIT Media Lab and Benjamin Mako Hill
 
-require 'rubygems'
-require 'ruby-debug'
-Debugger.start
-
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation; either version 2 of the License, or
@@ -127,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?