2024-election-modelling/main.go

123 lines
3.0 KiB
Go
Raw Normal View History

2024-04-13 14:22:28 +00:00
package main
import (
"encoding/csv"
2024-04-13 14:22:28 +00:00
"fmt"
"os"
2024-04-13 15:19:35 +00:00
"strconv"
"strings"
2024-04-13 14:22:28 +00:00
)
2024-04-13 16:42:13 +00:00
type StateData struct {
State string
Votes int
Party2000 string
Party2004 string
Party2008 string
Party2012 string
Party2016 string
Party2020 string
}
2024-04-13 16:42:13 +00:00
func readStateData() ([]StateData, error) {
var statesData map[string]StateData = make(map[string]StateData)
var years = []string{"2000", "2004", "2008", "2012", "2016", "2020"}
2024-04-13 15:19:35 +00:00
2024-04-13 16:42:13 +00:00
/* Electoral votes for the 2004 election*/
votesFilename := "data/electoral-college-votes.csv"
votesFile, err := os.Open(votesFilename)
if err != nil {
2024-04-13 16:42:13 +00:00
return nil, fmt.Errorf("error opening the votes file: %v", err)
}
2024-04-13 16:42:13 +00:00
defer votesFile.Close()
votesReader := csv.NewReader(votesFile)
if _, err := votesReader.Read(); err != nil { // Skip header
return nil, fmt.Errorf("error reading votes header: %v", err)
2024-04-13 15:19:35 +00:00
}
for {
2024-04-13 16:42:13 +00:00
record, err := votesReader.Read()
if err != nil {
2024-04-13 16:42:13 +00:00
break // EOF or an error
}
2024-04-13 15:19:35 +00:00
votes, err := strconv.Atoi(record[1])
if err != nil {
2024-04-13 16:42:13 +00:00
continue // Error in converting votes, skip this record
2024-04-13 15:19:35 +00:00
}
2024-04-13 16:42:13 +00:00
state := record[0]
if _, exists := statesData[state]; !exists {
statesData[state] = StateData{State: state, Votes: votes}
}
}
2024-04-13 16:42:13 +00:00
/* Election results */
for _, year := range years {
resultsFilename := fmt.Sprintf("data/results/%s.csv", year)
resultsFile, err := os.Open(resultsFilename)
2024-04-13 16:35:52 +00:00
if err != nil {
2024-04-13 16:42:13 +00:00
return nil, fmt.Errorf("error opening the results file for %s: %v", year, err)
}
defer resultsFile.Close()
resultsReader := csv.NewReader(resultsFile)
if _, err := resultsReader.Read(); err != nil { // Skip header
return nil, fmt.Errorf("error reading results header for %s: %v", year, err)
}
for {
record, err := resultsReader.Read()
if err != nil {
break // EOF or an error
}
state, party := record[0], record[1]
data, exists := statesData[state]
if !exists {
continue // State not found in votes map, skip
}
// Update the party winning in the specific year
switch year {
case "2000":
data.Party2000 = party
case "2004":
data.Party2004 = party
case "2008":
data.Party2008 = party
case "2012":
data.Party2012 = party
case "2016":
data.Party2016 = party
case "2020":
data.Party2020 = party
}
statesData[state] = data
2024-04-13 16:35:52 +00:00
}
}
2024-04-13 16:42:13 +00:00
// Convert statesData map to a slice for returning
var dataSlice []StateData
for _, data := range statesData {
dataSlice = append(dataSlice, data)
2024-04-13 16:35:52 +00:00
}
2024-04-13 16:42:13 +00:00
return dataSlice, nil
2024-04-13 16:35:52 +00:00
}
2024-04-13 14:22:28 +00:00
func main() {
2024-04-13 16:42:13 +00:00
stateDatas, err := readStateData()
if err != nil {
fmt.Println("Error:", err)
return
}
// Example states to query
states := []string{"California", "Texas", "Florida"}
2024-04-13 16:42:13 +00:00
for _, stateData := range stateDatas {
for _, state := range states {
if strings.EqualFold(stateData.State, state) {
fmt.Printf("%s: Votes: %d, Winners: 2000 - %s, 2004 - %s, 2008 - %s, 2012 - %s, 2016 - %s, 2020 - %s\n",
stateData.State, stateData.Votes,
stateData.Party2000, stateData.Party2004, stateData.Party2008,
stateData.Party2012, stateData.Party2016, stateData.Party2020)
break
}
2024-04-13 16:35:52 +00:00
}
}
2024-04-13 14:22:28 +00:00
}