add past votes & go code to get electoral votes

This commit is contained in:
NunoSempere 2024-04-13 11:10:50 -04:00
parent d094a81580
commit 3622b3ea3e
7 changed files with 374 additions and 2 deletions

52
data/2000.csv Normal file
View File

@ -0,0 +1,52 @@
State,Winning Party
Alabama,R
Alaska,R
Arizona,R
Arkansas,R
California,D
Colorado,R
Connecticut,D
Delaware,D
District of Columbia,D
Florida,R
Georgia,R
Hawaii,D
Idaho,R
Illinois,D
Indiana,R
Iowa,D
Kansas,R
Kentucky,R
Louisiana,R
Maine,D
Maryland,D
Massachusetts,D
Michigan,D
Minnesota,D
Mississippi,R
Missouri,R
Montana,R
Nebraska,R
Nevada,R
New Hampshire,R
New Jersey,D
New Mexico,D
New York,D
North Carolina,R
North Dakota,R
Ohio,R
Oklahoma,R
Oregon,D
Pennsylvania,D
Rhode Island,D
South Carolina,R
South Dakota,R
Tennessee,R
Texas,R
Utah,R
Vermont,D
Virginia,R
Washington,D
West Virginia,R
Wisconsin,D
Wyoming,R
1 State Winning Party
2 Alabama R
3 Alaska R
4 Arizona R
5 Arkansas R
6 California D
7 Colorado R
8 Connecticut D
9 Delaware D
10 District of Columbia D
11 Florida R
12 Georgia R
13 Hawaii D
14 Idaho R
15 Illinois D
16 Indiana R
17 Iowa D
18 Kansas R
19 Kentucky R
20 Louisiana R
21 Maine D
22 Maryland D
23 Massachusetts D
24 Michigan D
25 Minnesota D
26 Mississippi R
27 Missouri R
28 Montana R
29 Nebraska R
30 Nevada R
31 New Hampshire R
32 New Jersey D
33 New Mexico D
34 New York D
35 North Carolina R
36 North Dakota R
37 Ohio R
38 Oklahoma R
39 Oregon D
40 Pennsylvania D
41 Rhode Island D
42 South Carolina R
43 South Dakota R
44 Tennessee R
45 Texas R
46 Utah R
47 Vermont D
48 Virginia R
49 Washington D
50 West Virginia R
51 Wisconsin D
52 Wyoming R

52
data/2004.csv Normal file
View File

@ -0,0 +1,52 @@
State,Party
Alabama,R
Alaska,R
Arizona,R
Arkansas,R
California,D
Colorado,R
Connecticut,D
Delaware,D
District of Columbia,D
Florida,R
Georgia,R
Hawaii,D
Idaho,R
Illinois,D
Indiana,R
Iowa,R
Kansas,R
Kentucky,R
Louisiana,R
Maine,D
Maryland,D
Massachusetts,D
Michigan,D
Minnesota,D
Mississippi,R
Missouri,R
Montana,R
Nebraska,R
Nevada,R
New Hampshire,D
New Jersey,D
New Mexico,R
New York,D
North Carolina,R
North Dakota,R
Ohio,R
Oklahoma,R
Oregon,D
Pennsylvania,D
Rhode Island,D
South Carolina,R
South Dakota,R
Tennessee,R
Texas,R
Utah,R
Vermont,D
Virginia,R
Washington,D
West Virginia,R
Wisconsin,D
Wyoming,R
1 State Party
2 Alabama R
3 Alaska R
4 Arizona R
5 Arkansas R
6 California D
7 Colorado R
8 Connecticut D
9 Delaware D
10 District of Columbia D
11 Florida R
12 Georgia R
13 Hawaii D
14 Idaho R
15 Illinois D
16 Indiana R
17 Iowa R
18 Kansas R
19 Kentucky R
20 Louisiana R
21 Maine D
22 Maryland D
23 Massachusetts D
24 Michigan D
25 Minnesota D
26 Mississippi R
27 Missouri R
28 Montana R
29 Nebraska R
30 Nevada R
31 New Hampshire D
32 New Jersey D
33 New Mexico R
34 New York D
35 North Carolina R
36 North Dakota R
37 Ohio R
38 Oklahoma R
39 Oregon D
40 Pennsylvania D
41 Rhode Island D
42 South Carolina R
43 South Dakota R
44 Tennessee R
45 Texas R
46 Utah R
47 Vermont D
48 Virginia R
49 Washington D
50 West Virginia R
51 Wisconsin D
52 Wyoming R

52
data/2008.csv Normal file
View File

@ -0,0 +1,52 @@
State,Party
Alabama,R
Alaska,R
Arizona,R
Arkansas,R
California,D
Colorado,D
Connecticut,D
Delaware,D
District of Columbia,D
Florida,D
Georgia,R
Hawaii,D
Idaho,R
Illinois,D
Indiana,D
Iowa,D
Kansas,R
Kentucky,R
Louisiana,R
Maine,D
Maryland,D
Massachusetts,D
Michigan,D
Minnesota,D
Mississippi,R
Missouri,R
Montana,R
Nebraska,Split
Nevada,D
New Hampshire,D
New Jersey,D
New Mexico,D
New York,D
North Carolina,D
North Dakota,R
Ohio,D
Oklahoma,R
Oregon,D
Pennsylvania,D
Rhode Island,D
South Carolina,R
South Dakota,R
Tennessee,R
Texas,R
Utah,R
Vermont,D
Virginia,D
Washington,D
West Virginia,R
Wisconsin,D
Wyoming,R
1 State Party
2 Alabama R
3 Alaska R
4 Arizona R
5 Arkansas R
6 California D
7 Colorado D
8 Connecticut D
9 Delaware D
10 District of Columbia D
11 Florida D
12 Georgia R
13 Hawaii D
14 Idaho R
15 Illinois D
16 Indiana D
17 Iowa D
18 Kansas R
19 Kentucky R
20 Louisiana R
21 Maine D
22 Maryland D
23 Massachusetts D
24 Michigan D
25 Minnesota D
26 Mississippi R
27 Missouri R
28 Montana R
29 Nebraska Split
30 Nevada D
31 New Hampshire D
32 New Jersey D
33 New Mexico D
34 New York D
35 North Carolina D
36 North Dakota R
37 Ohio D
38 Oklahoma R
39 Oregon D
40 Pennsylvania D
41 Rhode Island D
42 South Carolina R
43 South Dakota R
44 Tennessee R
45 Texas R
46 Utah R
47 Vermont D
48 Virginia D
49 Washington D
50 West Virginia R
51 Wisconsin D
52 Wyoming R

52
data/2012.csv Normal file
View File

@ -0,0 +1,52 @@
State,Party
Alabama,R
Alaska,R
Arizona,R
Arkansas,R
California,D
Colorado,D
Connecticut,D
Delaware,D
District of Columbia,D
Florida,D
Georgia,R
Hawaii,D
Idaho,R
Illinois,D
Indiana,R
Iowa,D
Kansas,R
Kentucky,R
Louisiana,R
Maine,D
Maryland,D
Massachusetts,D
Michigan,D
Minnesota,D
Mississippi,R
Missouri,R
Montana,R
Nebraska,R
Nevada,D
New Hampshire,D
New Jersey,D
New Mexico,D
New York,D
North Carolina,R
North Dakota,R
Ohio,D
Oklahoma,R
Oregon,D
Pennsylvania,D
Rhode Island,D
South Carolina,R
South Dakota,R
Tennessee,R
Texas,R
Utah,R
Vermont,D
Virginia,D
Washington,D
West Virginia,R
Wisconsin,D
Wyoming,R
1 State Party
2 Alabama R
3 Alaska R
4 Arizona R
5 Arkansas R
6 California D
7 Colorado D
8 Connecticut D
9 Delaware D
10 District of Columbia D
11 Florida D
12 Georgia R
13 Hawaii D
14 Idaho R
15 Illinois D
16 Indiana R
17 Iowa D
18 Kansas R
19 Kentucky R
20 Louisiana R
21 Maine D
22 Maryland D
23 Massachusetts D
24 Michigan D
25 Minnesota D
26 Mississippi R
27 Missouri R
28 Montana R
29 Nebraska R
30 Nevada D
31 New Hampshire D
32 New Jersey D
33 New Mexico D
34 New York D
35 North Carolina R
36 North Dakota R
37 Ohio D
38 Oklahoma R
39 Oregon D
40 Pennsylvania D
41 Rhode Island D
42 South Carolina R
43 South Dakota R
44 Tennessee R
45 Texas R
46 Utah R
47 Vermont D
48 Virginia D
49 Washington D
50 West Virginia R
51 Wisconsin D
52 Wyoming R

52
data/2016.csv Normal file
View File

@ -0,0 +1,52 @@
State,Party
Alabama,R
Alaska,R
Arizona,R
Arkansas,R
California,D
Colorado,D
Connecticut,D
Delaware,D
District of Columbia,D
Florida,R
Georgia,R
Hawaii,D
Idaho,R
Illinois,D
Indiana,R
Iowa,R
Kansas,R
Kentucky,R
Louisiana,R
Maine,Split
Maryland,D
Massachusetts,D
Michigan,R
Minnesota,D
Mississippi,R
Missouri,R
Montana,R
Nebraska,Split
Nevada,D
New Hampshire,D
New Jersey,D
New Mexico,D
New York,D
North Carolina,R
North Dakota,R
Ohio,R
Oklahoma,R
Oregon,D
Pennsylvania,R
Rhode Island,D
South Carolina,R
South Dakota,R
Tennessee,R
Texas,R
Utah,R
Vermont,D
Virginia,D
Washington,D
West Virginia,R
Wisconsin,R
Wyoming,R
1 State Party
2 Alabama R
3 Alaska R
4 Arizona R
5 Arkansas R
6 California D
7 Colorado D
8 Connecticut D
9 Delaware D
10 District of Columbia D
11 Florida R
12 Georgia R
13 Hawaii D
14 Idaho R
15 Illinois D
16 Indiana R
17 Iowa R
18 Kansas R
19 Kentucky R
20 Louisiana R
21 Maine Split
22 Maryland D
23 Massachusetts D
24 Michigan R
25 Minnesota D
26 Mississippi R
27 Missouri R
28 Montana R
29 Nebraska Split
30 Nevada D
31 New Hampshire D
32 New Jersey D
33 New Mexico D
34 New York D
35 North Carolina R
36 North Dakota R
37 Ohio R
38 Oklahoma R
39 Oregon D
40 Pennsylvania R
41 Rhode Island D
42 South Carolina R
43 South Dakota R
44 Tennessee R
45 Texas R
46 Utah R
47 Vermont D
48 Virginia D
49 Washington D
50 West Virginia R
51 Wisconsin R
52 Wyoming R

51
data/2020.csv Normal file
View File

@ -0,0 +1,51 @@
Alabama,R
Alaska,R
Arizona,D
Arkansas,R
California,D
Colorado,D
Connecticut,D
Delaware,D
District of Columbia,D
Florida,R
Georgia,D
Hawaii,D
Idaho,R
Illinois,D
Indiana,R
Iowa,R
Kansas,R
Kentucky,R
Louisiana,R
Maine,Split
Maryland,D
Massachusetts,D
Michigan,D
Minnesota,D
Mississippi,R
Missouri,R
Montana,R
Nebraska,Split
Nevada,D
New Hampshire,D
New Jersey,D
New Mexico,D
New York,D
North Carolina,R
North Dakota,R
Ohio,R
Oklahoma,R
Oregon,D
Pennsylvania,D
Rhode Island,D
South Carolina,R
South Dakota,R
Tennessee,R
Texas,R
Utah,R
Vermont,D
Virginia,D
Washington,D
West Virginia,R
Wisconsin,D
Wyoming,R
1 Alabama R
2 Alaska R
3 Arizona D
4 Arkansas R
5 California D
6 Colorado D
7 Connecticut D
8 Delaware D
9 District of Columbia D
10 Florida R
11 Georgia D
12 Hawaii D
13 Idaho R
14 Illinois D
15 Indiana R
16 Iowa R
17 Kansas R
18 Kentucky R
19 Louisiana R
20 Maine Split
21 Maryland D
22 Massachusetts D
23 Michigan D
24 Minnesota D
25 Mississippi R
26 Missouri R
27 Montana R
28 Nebraska Split
29 Nevada D
30 New Hampshire D
31 New Jersey D
32 New Mexico D
33 New York D
34 North Carolina R
35 North Dakota R
36 Ohio R
37 Oklahoma R
38 Oregon D
39 Pennsylvania D
40 Rhode Island D
41 South Carolina R
42 South Dakota R
43 Tennessee R
44 Texas R
45 Utah R
46 Vermont D
47 Virginia D
48 Washington D
49 West Virginia R
50 Wisconsin D
51 Wyoming R

65
main.go
View File

@ -1,9 +1,70 @@
package main
import (
"encoding/csv"
"fmt"
"os"
"strings"
)
func main() {
fmt.Println("Hello world")
/* Electoral votes */
type StateVotes struct {
State string
Votes string
}
func readElectoralVotes(filename string) ([]StateVotes, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening the file: %v", err)
}
defer file.Close()
var stateVotes []StateVotes
reader := csv.NewReader(file)
for {
record, err := reader.Read()
// End of file is expected, not an error in this context
if err != nil {
break
}
stateVotes = append(stateVotes, StateVotes{State: record[0], Votes: record[1]})
}
if len(stateVotes) == 0 {
return nil, fmt.Errorf("no data found")
}
return stateVotes, nil
}
func findElectoralVotes(stateVotes []StateVotes, stateName string) (string, error) {
for _, sv := range stateVotes {
if strings.EqualFold(sv.State, stateName) {
return sv.Votes, nil
}
}
return "", fmt.Errorf("state not found")
}
func main() {
filename := "data/electoral-college-votes.csv"
stateVotes, err := readElectoralVotes(filename)
if err != nil {
fmt.Println("Error:", err)
return
}
// Example states to query
states := []string{"California", "Texas", "Florida"}
for _, state := range states {
votes, err := findElectoralVotes(stateVotes, state)
if err != nil {
fmt.Printf("Error: %s\n", err)
} else {
fmt.Printf("%s has %s electoral votes.\n", state, votes)
}
}
}