如果我有两个候选人和五个选民,投票倾向的结果是:
[
[:John, :Clinton, -27],
[:John, :Bush, -8],
[:Raphael, :Clinton, -12],
[:Raphael, :Bush, -40],
[:Damon, :Clinton, 71],
[:Damon, :Bush, 4],
[:Elysee, :Clinton, 13],
[:Elysee, :Bush, -36],
[:Griffin, :Clinton, -1],
[:Griffin, :Bush, 11]
]我如何看待每个投票者,并计算出最大的倾向数作为该候选人的选票,并将其存储在一个变量中,用于每个候选人的最终计票?
你好!非常感谢你的回复。我不仅是编程新手,也是Stackoverflow新手,所以如果我没有正确编辑,请原谅。
很抱歉。
读了你们放的东西,我甚至不知道该怎么处理它们,所以我会把我想出来的代码放进去,试着更好地解释我想要实现的是什么。听我说。
如果我有:
def stump_speech
voter_list = {
John: "Progressive",
Raphael: "Conservative",
Damon: "Libertarian",
Elysee: "Independent",
Griffin: "Massachussetts Democrat"
}
end并将其与以下内容进行比较:
candidate_list = {}
candidate_list = {
Clinton: "Democrat",
Bush: "Republican",
}运行它的是:
voter_list.each { |voter_name, politics|
candidate_list.each { |candidate_name, party|
if
politics == "Progressive" && party == "Republican"
voting_prob = rand(0..100) - 25
decision
elsif
politics == "Progressive" && party == "Democrat"
voting_prob = rand(0..100) - 75
decision
elsif
politics == "Conservative" && party == "Republican"
voting_prob = rand(0..100) - 75
decision
elsif
politics == "Conservative" && party == "Democrat"
voting_prob = rand(0..100) - 25
decision
elsif
politics == "Independent" && party == "Republican"
voting_prob = rand(0..100) - 50
decision
elsif
politics == "Independent" && party == "Democrat"
voting_prob = rand(0..100) - 50
decision
elsif
politics == "Libertarian" && party == "Republican"
voting_prob = rand(0..100) - 90
decision
elsif
politics == "Libertarian" && party == "Democrat"
voting_prob = rand(0..100) - 10
decision
elsif
politics == "Massachussetts Democrat" && party == "Republican"
voting_prob = rand(0..100) - 10
decision
elsif
politics == "Massachussetts Democrat" && party == "Democrat"
voting_prob = rand(0..100) - 90
decision
else
end
}
}它输出了前面发布的数组,我如何获取数字,例如约翰的,这样它就会算作布什的选票,拉斐尔的选票,等等,并将这些放入一个数组中,我可以用它们来计算最终的计票,以确定获胜者?
用你能做到的最简单的方式,好吗?非常感谢!我已经学了十天了,即使是掌握概念也不容易--希望不要灰心丧气。
发布于 2015-07-11 23:25:21
从以下数组开始:
tendencies = [
[:John, :Clinton, -27],
[:John, :Bush, -8],
[:Raphael, :Clinton, -12],
[:Raphael, :Bush, -40],
[:Damon, :Clinton, 71],
[:Damon, :Bush, 4],
[:Elysee, :Clinton, 13],
[:Elysee, :Bush, -36],
[:Griffin, :Clinton, -1],
[:Griffin, :Bush, 11]
]首先,我将使用Enumerable的group_by按投票者组织每个数组中的first条目。
tendencies.group_by {|t| t.first}或者,等同地,
tendencies.group_by(&:first)这将导致以下哈希:
{
:John =>[[:John, :Clinton, -27], [:John, :Bush, -8]],
:Raphael =>[[:Raphael, :Clinton, -12], [:Raphael, :Bush, -40]]
:Damon =>[[:Damon, :Clinton, 71], [:Damon, :Bush, 4]],
:Elysee =>[[:Elysee, :Clinton, 13], [:Elysee, :Bush, -36]],
:Griffin =>[[:Griffin, :Clinton, -1], [:Griffin, :Bush, 11]]
}现在,我假设你希望每个投票者都有一个候选人。因此,如果我们查看上面的每个values,有两个选项,我们想要一个带有max的选项。因为我们希望每个值都有一个候选值,所以我们可以使用map。例如,为了只获得布什的选票,我们可以执行以下操作:
tendencies.group_by(&:first).values.map(&:last)这将获取上面每行的最后一个(第二个)子数组:
[
[:John, :Bush, -8],
[:Raphael, :Bush, -40],
[:Damon, :Bush, 4],
[:Elysee, :Bush, -36],
[:Griffin, :Bush, 11]
]但我们不想这样,不是吗?我们想要具有max趋势值的那个。所以我们需要使用max_by。这将根据我们指定的任何条件获取最大值,在本例中为每个子数组的last值(趋势)。
tendencies.group_by(&:first).values.map{ |v| v.max_by(&:last)}结果:
[
[:John, :Bush, -8],
[:Raphael, :Clinton, -12],
[:Damon, :Clinton, 71],
[:Elysee, :Clinton, 13],
[:Griffin, :Bush, 11]
]越来越近了!您只需使用另一个map获取结果的中间值(索引[1]
tendencies.group_by(&:first).values.map{ |v| v.max_by(&:last)}.map{ |v| v[1]}结果:[:Bush, :Clinton, :Clinton, :Clinton, :Bush]
现在它只是取决于你想要如何表示这个最终的计数。假设您想要每个候选人的计数。有一些a few ways可以做到这一点,但我将使用我们在上面使用过的group_by和map。
votes = [:Bush, :Clinton, :Clinton, :Clinton, :Bush]
votes.group_by{|v| v}
# => {:Bush=>[:Bush, :Bush], :Clinton=>[:Clinton, :Clinton, :Clinton]}
votes.group_by{|v| v}.map{|candidate, votes| [candidate, votes.count]}
# => [[:Bush, 2], [:Clinton, 3]]发布于 2015-07-11 22:20:59
array =
[[:John, :Clinton, -27],
[:John, :Bush, -8],
[:Raphael, :Clinton, -12],
[:Raphael, :Bush, -40],
[:Damon, :Clinton, 71],
[:Damon, :Bush, 4],
[:Elysee, :Clinton, 13],
[:Elysee, :Bush, -36],
[:Griffin, :Clinton, -1],
[:Griffin, :Bush, 11]]要查看John的do:
array.select { |rec| rec[0] == :John } # => [[:John, :Clinton, -27], [:John, :Bush, -8]]还是为了获得布什的总票数
vote_sum = 0
array.select { |rec| rec[1] == :Bush }.each { |rec| vote_sum += rec[-1] }请注意,sum在Rails中可用,但在纯Ruby中不可用(您也可以使用inject)
https://stackoverflow.com/questions/31356598
复制相似问题