-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary.rb
36 lines (25 loc) · 1.45 KB
/
summary.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
require "csv"
candidates = CSV.read(File.join(File.dirname(__FILE__), "data", "candidates.csv"), :headers => true)
candidate_count = candidates.group_by {|r| r["party"]}.reduce({}) {|m, (party, c)| m[party] = c.count; m }
locals_count = candidates.select{|c| c["ward"] == c["lives_in_ward"]}.group_by {|r| r["party"]}.reduce({}) {|m, (party, c)| m[party] = c.count; m }
scores = candidate_count.reduce({}) {|m, (party, count)| m[party] = (locals_count[party].to_f / count * 100).to_i; m }
count = 1
puts "| Rank | Party | Percentage | Candidates who live in same | Total candidates | Ratio |"
puts "| ---- | ----- | -----------| ------|"
scores.invert.sort.reverse.map do |rank, party|
candidates = candidate_count[party] || 0
locals = locals_count[party] || 0
puts "| #{count} | #{party} | #{rank}% | #{locals} | #{candidates} | (#{locals}/#{candidates}) |"
count += 1
end
# bourough_count = candidates.select{|c| c["lives_in_council"] == "Lambeth Borough Council"}.group_by {|r| r["party"]}.reduce({}) {|m, (party, c)| m[party] = c.count; m }
# p bourough_count
# borough_scores = candidate_count.reduce({}) {|m, (party, count)| m[party] = (bourough_count[party].to_f / count * 100).to_i; m }
# p borough_scores
# count = 1
# borough_scores.invert.sort.reverse.map do |rank, party|
# candidates = candidate_count[party] || 0
# borough = bourough_count[party] || 0
# puts "#{count}.\t#{party}:\t#{rank}%\t(#{borough}/#{candidates})"
# count += 1
# end