Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdd45619

Browse files
committed
Add alternate election challenge solution
1 parente6a93b3 commitdd45619

File tree

3 files changed

+101
-35
lines changed

3 files changed

+101
-35
lines changed

‎ch08-conditional-logic/9-challenge-simulate-an-election.py

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 8.9 - Challenge: Simulate an Election
2+
# Solution to challenge
3+
4+
5+
# Simulate the results of an election using a Monte Carlo simulation
6+
7+
fromrandomimportrandom
8+
9+
num_times_A_wins=0
10+
num_times_B_wins=0
11+
12+
num_trials=10_000
13+
fortrialinrange(0,num_trials):
14+
votes_for_A=0
15+
votes_for_B=0
16+
17+
# Determine who wins the 1st region
18+
ifrandom()<0.87:
19+
votes_for_A=votes_for_A+1
20+
else:
21+
votes_for_B=votes_for_B+1
22+
23+
# Determine who wins the 2nd region
24+
ifrandom()<0.65:
25+
votes_for_A=votes_for_A+1
26+
else:
27+
votes_for_B=votes_for_B+1
28+
29+
# Determine who wins the erd region
30+
ifrandom()<0.17:
31+
votes_for_A=votes_for_A+1
32+
else:
33+
votes_for_B=votes_for_B+1
34+
35+
# Determine overall election outcome
36+
ifvotes_for_A>votes_for_B:
37+
num_times_A_wins=num_times_A_wins+1
38+
else:
39+
num_times_B_wins=num_times_B_wins+1
40+
41+
print(f"Probability A wins:{num_times_A_wins/num_trials}")
42+
print(f"Probability B wins:{num_times_B_wins/num_trials}")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 8.9 - Challenge: Simulate an Election
2+
# Alternate solution to challenge
3+
4+
5+
# Simulate the results of an election using a Monte Carlo simulation
6+
7+
fromrandomimportrandom
8+
9+
10+
defrun_regional_election(chance_A_wins):
11+
"""Return the result of a regional election, either "A" or "B".
12+
13+
The chances of "A" winning are determined by chance_A_wins.
14+
"""
15+
ifrandom()<chance_A_wins:
16+
return"A"
17+
else:
18+
return"B"
19+
20+
21+
defrun_election(regional_chances):
22+
"""Return the result of an election, either "A" or "B".
23+
24+
regional_chances is a list or tuple of floats representing the
25+
chances that candidate "A" will win in each region.
26+
27+
For example, run_election([.2, .5, .7]) will run an election with
28+
three regions, where candidate "A" has a 20% chance to win in the
29+
first region, 50% in the second, and 70% in the third.
30+
"""
31+
num_regions_won_by_A=0
32+
forchance_A_winsinregional_chances:
33+
ifrun_regional_election(chance_A_wins)=="A":
34+
num_regions_won_by_A=num_regions_won_by_A+1
35+
36+
# Return the results. Note that the number of regions won by candidate
37+
# "B" is the total number of regions minus the number of regions won by
38+
# candidate "A". The total number of regions is the same as the length
39+
# of the regional_chances list.
40+
ifnum_regions_won_by_A>len(regional_chances)-num_regions_won_by_A:
41+
return"A"
42+
else:
43+
return"B"
44+
45+
46+
CHANCES_A_WINS_BY_REGION= [0.87,0.65,0.17]
47+
NUM_TRIALS=10_000
48+
49+
# Run the Monte-Carlo simulation
50+
num_times_A_wins=0
51+
fortrialinrange(NUM_TRIALS):
52+
ifrun_election(CHANCES_A_WINS_BY_REGION)=="A":
53+
num_times_A_wins=num_times_A_wins+1
54+
55+
# Display the probabilities that candidate A or candidate B wins the
56+
# election. Note the probability that B wins can be calculated by
57+
# subtracting the probability that A wins from 1.
58+
print(f"Probability A wins:{num_times_A_wins/num_trials}")
59+
print(f"Probability B wins:{1- (num_times_A_wins/num_trials)}")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp