patternpythonMinor
Python election challenge
Viewed 0 times
electionpythonchallenge
Problem
Basically, there is an election on 3 regions where either candidate needs to win 2 in order to win the election. Idea is to simulate 10000 elections to see which candidate wins. I think my solution works but I am not happy with the huge list of
if statements and wondering of if there is a better way for this.# Assignment simulate elections page 257
# 2 Candidates A and B
# Candidate A has following odds:
# 87% change of winning in region 1
# 65% changes of winning in region 2
# 17% changes of winning in region 3
from random import random
candidate_a_won = 0
candidate_b_won = 0
for i in range(0, 10000):
result = random()
candidate_a = 0
candidate_b = 0
if result + .87 >= 1:
candidate_a += 1
else:
candidate_b += 1
if result + .65 >= 1:
candidate_a += 1
else:
candidate_b += 1
if result + .17 >= 1:
candidate_a += 1
else:
candidate_b += 1
if candidate_a > candidate_b:
candidate_a_won += 1
else:
candidate_b_won += 1
print('Candidate A won elections {} times, candidate B won elections {} times'.format(candidate_a_won, candidate_b_won))Solution
-
Your code has a problem. Candidate A can loose region 1, but win region 3.
Where with your code they can only win region 3 if they win region 2 and 1.
This is as they are independent, and so you should
-
You want to move your chances outside the loop into an array.
-
You can simplify your
-
Candidate a wins if they have more than half the regions, you don't need to calculate the amount of regions candidate b wins.
And so you could change your code to the following:
If you want to extend on the above then it'll be hard if you decide to have more than two candidates, or even amounts of states.
But as this is a challenge, I don't think you need to worry about these situations.
Your code has a problem. Candidate A can loose region 1, but win region 3.
Where with your code they can only win region 3 if they win region 2 and 1.
This is as they are independent, and so you should
random in each check.-
You want to move your chances outside the loop into an array.
-
You can simplify your
ifs into one comprehension.-
Candidate a wins if they have more than half the regions, you don't need to calculate the amount of regions candidate b wins.
And so you could change your code to the following:
from random import random
AMOUNT = 10000
region_chances = [87, 65, 17]
region_chances = [1 - n / 100 for n in region_chances]
regions = len(region_chances)
candidate_a_won = sum(
sum(random() >= chance for chance in region_chances) * 2 > regions
for _ in range(AMOUNT)
)
candidate_b_won = AMOUNT - candidate_a_won
print('Candidate A won elections {} times, candidate B won elections {} times'.format(candidate_a_won, candidate_b_won))If you want to extend on the above then it'll be hard if you decide to have more than two candidates, or even amounts of states.
But as this is a challenge, I don't think you need to worry about these situations.
Code Snippets
from random import random
AMOUNT = 10000
region_chances = [87, 65, 17]
region_chances = [1 - n / 100 for n in region_chances]
regions = len(region_chances)
candidate_a_won = sum(
sum(random() >= chance for chance in region_chances) * 2 > regions
for _ in range(AMOUNT)
)
candidate_b_won = AMOUNT - candidate_a_won
print('Candidate A won elections {} times, candidate B won elections {} times'.format(candidate_a_won, candidate_b_won))Context
StackExchange Code Review Q#154364, answer score: 5
Revisions (0)
No revisions yet.