add past votes & go code to get electoral votes
This commit is contained in:
parent
d094a81580
commit
3622b3ea3e
52
data/2000.csv
Normal file
52
data/2000.csv
Normal 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
|
|
52
data/2004.csv
Normal file
52
data/2004.csv
Normal 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
|
|
52
data/2008.csv
Normal file
52
data/2008.csv
Normal 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
|
|
52
data/2012.csv
Normal file
52
data/2012.csv
Normal 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
|
|
52
data/2016.csv
Normal file
52
data/2016.csv
Normal 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
|
|
51
data/2020.csv
Normal file
51
data/2020.csv
Normal 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
|
|
65
main.go
65
main.go
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user