-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelection.py
195 lines (190 loc) · 16 KB
/
election.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import random
import re
text = """
case 'AL':
description = '<b>Alabama: <font color=#ff0000>Republican: 97.8%</font> vs <font color=#0000ff>Democrat: 1.8%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font>+0.0% in last week. Electoral votes: 9</font></b>';
break;
case 'AK':
description = '<b>Alaska: <font color=#ff0000>Republican: 90.0%</font> vs <font color=#0000ff>Democrat: 10.0%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-1.2% in last week. Electoral votes: 3</font></b>';
break;
case 'AZ':
description = '<b>Arizona: <font color=#ff0000>Republican: 61.5%</font> vs <font color=#0000ff>Democrat: 38.5%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-1.0% in last week. Electoral votes: 11</font></b>';
break;
case 'AR':
description = '<b>Arkansas: <font color=#ff0000>Republican: 97.7%</font> vs <font color=#0000ff>Democrat: 2.1%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font>+0.0% in last week. Electoral votes: 6</font></b>';
break;
case 'CA':
description = '<b>California: <font color=#0000ff>Democrat: 97.6%</font> vs <font color=#ff0000>Republican: 1.9%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font>-0.0% in last week. Electoral votes: 54</font></b>';
break;
case 'CO':
description = '<b>Colorado: <font color=#0000ff>Democrat: 96.8%</font> vs <font color=#ff0000>Republican: 2.9%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 1.0% in last week. Electoral votes: 10</font></b>';
break;
case 'CT':
description = '<b>Connecticut: <font color=#0000ff>Democrat: 97.7%</font> vs <font color=#ff0000>Republican: 1.9%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 0.1% in last week. Electoral votes: 7</font></b>';
break;
case 'DE':
description = '<b>Delaware: <font color=#0000ff>Democrat: 97.2%</font> vs <font color=#ff0000>Republican: 2.6%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 0.2% in last week. Electoral votes: 3</font></b>';
break;
case 'FL':
description = '<b>Florida: <font color=#ff0000>Republican: 83.5%</font> vs <font color=#0000ff>Democrat: 15.5%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-1.5% in last week. Electoral votes: 30</font></b>';
break;
case 'GA':
description = '<b>Georgia: <font color=#ff0000>Republican: 58.5%</font> vs <font color=#0000ff>Democrat: 41.5%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-1.0% in last week. Electoral votes: 16</font></b>';
break;
case 'HI':
description = '<b>Hawaii: <font color=#0000ff>Democrat: 97.4%</font> vs <font color=#ff0000>Republican: 2.1%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="red.png" width="14" height="14">-0.1% in last week. Electoral votes: 4</font></b>';
break;
case 'ID':
description = '<b>Idaho: <font color=#ff0000>Republican: 98.2%</font> vs <font color=#0000ff>Democrat: 0.8%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-0.2% in last week. Electoral votes: 4</font></b>';
break;
case 'IL':
description = '<b>Illinois: <font color=#0000ff>Democrat: 97.6%</font> vs <font color=#ff0000>Republican: 2.1%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="red.png" width="14" height="14">-0.1% in last week. Electoral votes: 19</font></b>';
break;
case 'IN':
description = '<b>Indiana: <font color=#ff0000>Republican: 97.4%</font> vs <font color=#0000ff>Democrat: 2.4%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 0.2% in last week. Electoral votes: 11</font></b>';
break;
case 'IA':
description = '<b>Iowa: <font color=#ff0000>Republican: 90.5%</font> vs <font color=#0000ff>Democrat: 9.5%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-4.0% in last week. Electoral votes: 6</font></b>';
break;
case 'KS':
description = '<b>Kansas: <font color=#ff0000>Republican: 96.8%</font> vs <font color=#0000ff>Democrat: 3.0%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font>+0.0% in last week. Electoral votes: 6</font></b>';
break;
case 'KY':
description = '<b>Kentucky: <font color=#ff0000>Republican: 97.6%</font> vs <font color=#0000ff>Democrat: 1.7%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 0.4% in last week. Electoral votes: 8</font></b>';
break;
case 'LA':
description = '<b>Louisiana: <font color=#ff0000>Republican: 97.5%</font> vs <font color=#0000ff>Democrat: 2.1%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 0.1% in last week. Electoral votes: 8</font></b>';
break;
case 'ME2':
description = '<b>Maine (CD-2): <font color=#ff0000>Republican: 77.5%</font> vs <font color=#0000ff>Democrat: 22.0%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-0.5% in last week. Electoral votes: 1</font></b>';
break;
case 'ME':
description = '<b>Maine: <font color=#0000ff>Democrat: 89.0%</font> vs <font color=#ff0000>Republican: 10.5%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 2.5% in last week. Electoral votes: 3</font></b>';
break;
case 'MD':
description = '<b>Maryland: <font color=#0000ff>Democrat: 98.1%</font> vs <font color=#ff0000>Republican: 1.7%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 0.1% in last week. Electoral votes: 10</font></b>';
break;
case 'MA':
description = '<b>Massachusetts: <font color=#0000ff>Democrat: 98.0%</font> vs <font color=#ff0000>Republican: 1.8%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font>+0.0% in last week. Electoral votes: 11</font></b>';
break;
case 'MI':
description = '<b>Michigan: <font color=#0000ff>Democrat: 58.5%</font> vs <font color=#ff0000>Republican: 40.5%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 3.0% in last week. Electoral votes: 15</font></b>';
break;
case 'MN':
description = '<b>Minnesota: <font color=#0000ff>Democrat: 93.5%</font> vs <font color=#ff0000>Republican: 6.5%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 2.0% in last week. Electoral votes: 10</font></b>';
break;
case 'MS':
description = '<b>Mississippi: <font color=#ff0000>Republican: 97.4%</font> vs <font color=#0000ff>Democrat: 2.3%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-0.1% in last week. Electoral votes: 6</font></b>';
break;
case 'MO':
description = '<b>Missouri: <font color=#ff0000>Republican: 97.8%</font> vs <font color=#0000ff>Democrat: 1.8%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 0.5% in last week. Electoral votes: 10</font></b>';
break;
case 'MT':
description = '<b>Montana: <font color=#ff0000>Republican: 97.5%</font> vs <font color=#0000ff>Democrat: 2.2%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 0.1% in last week. Electoral votes: 4</font></b>';
break;
case 'NE':
description = '<b>Nebraska: <font color=#ff0000>Republican: 97.2%</font> vs <font color=#0000ff>Democrat: 2.6%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-0.1% in last week. Electoral votes: 4</font></b>';
break;
case 'NE2':
description = '<b>Nebraska (CD2): <font color=#0000ff>Democrat: 82.5%</font> vs <font color=#ff0000>Republican: 17.5%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 1.0% in last week. Electoral votes: 1</font></b>';
break;
case 'NV':
description = '<b>Nevada: <font color=#0000ff>Democrat: 50.5%</font> vs <font color=#ff0000>Republican: 48.5%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 5.0% in last week. Electoral votes: 6</font></b>';
break;
case 'NH':
description = '<b>New Hampshire: <font color=#0000ff>Democrat: 86.5%</font> vs <font color=#ff0000>Republican: 13.5%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 3.0% in last week. Electoral votes: 4</font></b>';
break;
case 'NJ':
description = '<b>New Jersey: <font color=#0000ff>Democrat: 96.4%</font> vs <font color=#ff0000>Republican: 3.6%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 0.1% in last week. Electoral votes: 14</font></b>';
break;
case 'NM':
description = '<b>New Mexico: <font color=#0000ff>Democrat: 90.5%</font> vs <font color=#ff0000>Republican: 9.5%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font>+0.0% in last week. Electoral votes: 5</font></b>';
break;
case 'NY':
description = '<b>New York: <font color=#0000ff>Democrat: 97.2%</font> vs <font color=#ff0000>Republican: 2.3%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="red.png" width="14" height="14">-0.1% in last week. Electoral votes: 28</font></b>';
break;
case 'NC':
description = '<b>North Carolina: <font color=#ff0000>Republican: 56.5%</font> vs <font color=#0000ff>Democrat: 42.5%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-6.5% in last week. Electoral votes: 16</font></b>';
break;
case 'ND':
description = '<b>North Dakota: <font color=#ff0000>Republican: 97.9%</font> vs <font color=#0000ff>Democrat: 1.4%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-0.1% in last week. Electoral votes: 3</font></b>';
break;
case 'OH':
description = '<b>Ohio: <font color=#ff0000>Republican: 92.0%</font> vs <font color=#0000ff>Democrat: 7.5%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font>+0.0% in last week. Electoral votes: 17</font></b>';
break;
case 'OK':
description = '<b>Oklahoma: <font color=#ff0000>Republican: 98.3%</font> vs <font color=#0000ff>Democrat: 1.0%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font>+0.0% in last week. Electoral votes: 7</font></b>';
break;
case 'OR':
description = '<b>Oregon: <font color=#0000ff>Democrat: 96.8%</font> vs <font color=#ff0000>Republican: 2.9%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 0.2% in last week. Electoral votes: 8</font></b>';
break;
case 'PA':
description = '<b>Pennsylvania: <font color=#0000ff>Democrat: 50.0%</font> vs <font color=#ff0000>Republican: 49.5%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 5.0% in last week. Electoral votes: 19</font></b>';
break;
case 'RI':
description = '<b>Rhode Island: <font color=#0000ff>Democrat: 97.7%</font> vs <font color=#ff0000>Republican: 2.1%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font>+0.0% in last week. Electoral votes: 4</font></b>';
break;
case 'SC':
description = '<b>South Carolina: <font color=#ff0000>Republican: 97.2%</font> vs <font color=#0000ff>Democrat: 2.3%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="red.png" width="14" height="14">-0.1% in last week. Electoral votes: 9</font></b>';
break;
case 'SD':
description = '<b>South Dakota: <font color=#ff0000>Republican: 98.1%</font> vs <font color=#0000ff>Democrat: 0.8%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font>+0.0% in last week. Electoral votes: 3</font></b>';
break;
case 'TN':
description = '<b>Tennessee: <font color=#ff0000>Republican: 96.9%</font> vs <font color=#0000ff>Democrat: 2.6%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 0.1% in last week. Electoral votes: 11</font></b>';
break;
case 'TX':
description = '<b>Texas: <font color=#ff0000>Republican: 87.5%</font> vs <font color=#0000ff>Democrat: 12.5%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 1.0% in last week. Electoral votes: 40</font></b>';
break;
case 'UT':
description = '<b>Utah: <font color=#ff0000>Republican: 97.4%</font> vs <font color=#0000ff>Democrat: 2.1%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 0.3% in last week. Electoral votes: 6</font></b>';
break;
case 'VT':
description = '<b>Vermont: <font color=#0000ff>Democrat: 97.4%</font> vs <font color=#ff0000>Republican: 2.2%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font>+0.0% in last week. Electoral votes: 3</font></b>';
break;
case 'VA':
description = '<b>Virginia: <font color=#0000ff>Democrat: 88.5%</font> vs <font color=#ff0000>Republican: 11.0%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 3.0% in last week. Electoral votes: 13</font></b>';
break;
case 'WA':
description = '<b>Washington: <font color=#0000ff>Democrat: 96.4%</font> vs <font color=#ff0000>Republican: 1.8%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 0.2% in last week. Electoral votes: 12</font></b>';
break;
case 'WV':
description = '<b>West Virginia: <font color=#ff0000>Republican: 98.2%</font> vs <font color=#0000ff>Democrat: 1.4%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 0.2% in last week. Electoral votes: 4</font></b>';
break;
case 'WI':
description = '<b>Wisconsin: <font color=#0000ff>Democrat: 60.5%</font> vs <font color=#ff0000>Republican: 39.5%</font></b><span style="font-size: 14pt;"><br><font color=#0000ff>Democrat: </font><img src="green.png" width="14" height="14"> 4.5% in last week. Electoral votes: 10</font></b>';
break;
case 'WY':
description = '<b>Wyoming: <font color=#ff0000>Republican: 98.4%</font> vs <font color=#0000ff>Democrat: 0.6%</font></b><span style="font-size: 14pt;"><br><font color=#ff0000>Republican: </font><img src="green.png" width="14" height="14"> 0.1% in last week. Electoral votes: 3</font></b>';
break;
"""
states = []
for state, chance, votes in re.findall(
r"case '\w+':\ndescription = '<b>([\w\(\-\) ]+):.+Republican: (\d{1,2}.\d{1,2}).+Electoral votes: (\d+)",
text,
):
states.append(
{
"name": state,
"chance": float(chance),
"votes": int(votes),
}
)
ATTEMPTS = 100000
republican_wins = 0
ties = 0
democrat_wins = 0
for _ in range(ATTEMPTS):
republicans = 0
for state in states:
if random.random() * 100 < state["chance"]:
republicans += state["votes"]
if republicans > 269:
republican_wins += 1
elif republicans == 269:
ties += 1
else:
democrat_wins += 1
print(
f"Democrats: {democrat_wins / ATTEMPTS}, Republicans: {republican_wins / ATTEMPTS}, ties: {ties / ATTEMPTS}"
)