-
Notifications
You must be signed in to change notification settings - Fork 0
/
electoral_college.rb
55 lines (48 loc) · 1.19 KB
/
electoral_college.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
class ElectoralCollege
public
def calculate_winning_temperature(electorate)
return winning_temperature(collect_temperatures(electorate))
end
private
def collect_temperatures(electorate)
temperatures = Array.new
electorate.each do |voter|
case voter.category
when "SM"
next
when "RM"
temperatures.clear
temperatures.push(voter.desired_AC_temperature)
break
else
temperatures.push(voter.desired_AC_temperature)
end
end
return temperatures
end
def winning_temperature(temps)
winners = Array.new
winner_count = 0
for i in 17..28
current_count = temps.count(i)
if current_count > winner_count
winner_count = current_count
winners.clear
winners.push(i)
elsif current_count == winner_count
winners.push(i)
end
end
winner = 0
if winners.size == 1
winner = winners[0]
else
sum = 0
winners.each do |winner|
sum = sum + winner
end
winner = sum / winners.size
end
return winner
end
end