if have 2 candidates , 5 voters, , results of tendency vote are:
[ [: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] ] how @ each voter, , count largest tendency number vote candidate , store in variable final tally of votes per candidate?
hello! , responses. not new programming, i'm new stackoverflow forgive me if i'm not editing properly.
apologies.
reading guys put, don't know them, i'll put code came , try explain better trying achieve. bear me.
if have:
def stump_speech voter_list = { john: "progressive", raphael: "conservative", damon: "libertarian", elysee: "independent", griffin: "massachussetts democrat" } end and comparing to:
candidate_list = {} candidate_list = { clinton: "democrat", bush: "republican", } and runs is:
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 } } and outputs array above earlier posted, how grab numbers, john's, example, count vote bush, , raphael's clinton, etc., , throw array play final tally in determining winner?
in simplest way can manage, please? much! i'm ten days , grasping concepts isn't easy yet - hoping not discouraged.
starting following array of arrays:
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] ] first, use enumerable's group_by organize voter, first entry in each array.
tendencies.group_by {|t| t.first} or, equivalently,
tendencies.group_by(&:first) this results in following hash:
{ :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]] } now, i'm assuming want 1 candidate per voter. if @ each of values above there 2 options , want 1 max. since want 1 candidate per value, can use map. example, bush votes only, can following:
tendencies.group_by(&:first).values.map(&:last) this takes last (second) subarray each line above:
[ [:john, :bush, -8], [:raphael, :bush, -40], [:damon, :bush, 4], [:elysee, :bush, -36], [:griffin, :bush, 11] ] but don't want this, we? want 1 max tendency value. need use max_by. take max based on criteria specify, in case last value of each subarray (the tendency).
tendencies.group_by(&:first).values.map{ |v| v.max_by(&:last)} result:
[ [:john, :bush, -8], [:raphael, :clinton, -12], [:damon, :clinton, 71], [:elysee, :clinton, 13], [:griffin, :bush, 11] ] getting closer! can grab middle value (index [1]) of result using map:
tendencies.group_by(&:first).values.map{ |v| v.max_by(&:last)}.map{ |v| v[1]} result: [:bush, :clinton, :clinton, :clinton, :bush]
now depends on how want represent final tally. let's want counts per candidate. there a few ways it, i'm going use we've used above, 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]]
Comments
Post a Comment