forked from jasonmahony/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunoff_voting_incomplete.rb
executable file
·71 lines (64 loc) · 1.98 KB
/
runoff_voting_incomplete.rb
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env ruby
def runoff(voters)
# num_voters = voters.size
# count = Hash[voters[0].map { |i| [i, 0] }]
# num_candidates = count.size
# voters.each do |vote|
# count[vote[0]] += 1
# end
# sorted_array = count.sort_by {|k, v| v}
# if sorted_array[num_candidates - 1][1] > num_voters / 2
# return sorted_array[num_candidates - 1][0]
# elsif sorted_array[0][1] < sorted_array[num_candidates - 1][1]
#
#
# if sorted_array[0][1] < sorted_array[1][1]
#
# new_voters = voters.map {|vote| vote - [sorted_array[0][0]] }
# runoff(new_voters)
# elsif sorted_array[0][1] = sorted_array[2][1]
#
# new_voters = voters.map {|vote| vote - [sorted_array[0][0], sorted_array[1][0], sorted_array[2][0]] }
# runoff(new_voters)
#
# elsif sorted_array[0][1] = sorted_array[1][1]
# new_voters = voters.map {|vote| vote - [sorted_array[0][0], sorted_array[1][0]] }
# runoff(new_voters)
# end
#
# else
# return nil
# end
#0.upto(voters.size - 1) do |ballot|
# count = Hash[voters[0].map { |i| [i, 0] }]
# num_candidates = voters[0].size
num_voters = voters.size
num_candidates = voters[0].size
deleted = 0
count = Hash[voters[0].map {|i| [i, 0] }]
voters.each do |ballot|
count[ballot[0]] += 1
end
# [[:dem, 1], [:rep, 1], [:ind, 2]]
sorted_array = count.sort_by {|k, v| v}
if sorted_array[num_candidates - 1][1] > num_voters / 2
return sorted_array[num_candidates - 1][0]
else
0.upto(num_candidates - 1) do |del|
if sorted_array[del][1] == sorted_array[0][1]
deleted += 1
count.delete(sorted_array[del][0])
end
end
sorted_array = count.sort_by {|k, v| v}
return nil if sorted_array == nil
if sorted_array[num_candidates - deleted - 1][1] > num_voters / 2
return sorted_array[num_candidates - deleted - 1][0]
end
end
end
voters = [[:dem, :ind, :rep],
[:rep, :ind, :dem],
[:ind, :dem, :rep],
[:ind, :rep, :dem]]
puts runoff(voters)