unify structs

This commit is contained in:
NunoSempere 2024-04-13 12:42:13 -04:00
parent 120d6f860d
commit aad1cd725e
7 changed files with 79 additions and 103 deletions

182
main.go
View File

@ -8,110 +8,99 @@ import (
"strings"
)
/* Electoral votes */
type StateVotes struct {
State string
Votes int
type StateData struct {
State string
Votes int
Party2000 string
Party2004 string
Party2008 string
Party2012 string
Party2016 string
Party2020 string
}
func readElectoralVotes(filename string) ([]StateVotes, error) {
var stateVotes []StateVotes
func readStateData() ([]StateData, error) {
var statesData map[string]StateData = make(map[string]StateData)
var years = []string{"2000", "2004", "2008", "2012", "2016", "2020"}
file, err := os.Open(filename)
/* Electoral votes for the 2004 election*/
votesFilename := "data/electoral-college-votes.csv"
votesFile, err := os.Open(votesFilename)
if err != nil {
return nil, fmt.Errorf("error opening the file: %v", err)
return nil, fmt.Errorf("error opening the votes file: %v", err)
}
defer file.Close()
reader := csv.NewReader(file)
// Skip the first row ('State, Votes' header)
if _, err := reader.Read(); err != nil {
return nil, fmt.Errorf("error reading header row: %v", err)
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)
}
for {
record, err := reader.Read()
// End of file is expected, not an error in this context
record, err := votesReader.Read()
if err != nil {
break
break // EOF or an error
}
votes, err := strconv.Atoi(record[1])
if err != nil {
return nil, fmt.Errorf("error converting votes for %s to int: %v", record[0], err)
continue // Error in converting votes, skip this record
}
stateVotes = append(stateVotes, StateVotes{State: record[0], Votes: votes})
}
if len(stateVotes) == 0 {
return nil, fmt.Errorf("no data found")
}
return stateVotes, nil
}
func findElectoralVotes(stateVotes []StateVotes, stateName string) (int, error) {
for _, sv := range stateVotes {
if strings.EqualFold(sv.State, stateName) {
return sv.Votes, nil
state := record[0]
if _, exists := statesData[state]; !exists {
statesData[state] = StateData{State: state, Votes: votes}
}
}
return 0, fmt.Errorf("state not found")
}
/* Results for the elections */
type StateResult struct {
State string
Party string
}
func readElectionResults(filename string) ([]StateResult, error) {
var results []StateResult
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening the file: %v", err)
}
defer file.Close()
reader := csv.NewReader(file)
// Skip the header
if _, err := reader.Read(); err != nil {
return nil, fmt.Errorf("error reading header row: %v", err)
}
for {
record, err := reader.Read()
/* Election results */
for _, year := range years {
resultsFilename := fmt.Sprintf("data/results/%s.csv", year)
resultsFile, err := os.Open(resultsFilename)
if err != nil {
break // End of file or an error
return nil, fmt.Errorf("error opening the results file for %s: %v", year, err)
}
results = append(results, StateResult{State: record[0], Party: record[1]})
}
if len(results) == 0 {
return nil, fmt.Errorf("no data found")
}
return results, nil
}
// findWinningParty searches the provided slice of StateResults for a given state and returns its winning party.
func findWinningParty(results []StateResult, stateName string) (string, error) {
for _, result := range results {
if strings.EqualFold(result.State, stateName) {
return result.Party, nil
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
}
}
// If the loop completes without finding the state, it's not in the list.
return "", fmt.Errorf("state not found")
// Convert statesData map to a slice for returning
var dataSlice []StateData
for _, data := range statesData {
dataSlice = append(dataSlice, data)
}
return dataSlice, nil
}
func main() {
filename := "data/2000.csv" // Update with the actual filename
results, err := readElectionResults(filename)
stateDatas, err := readStateData()
if err != nil {
fmt.Println("Error:", err)
return
@ -119,28 +108,15 @@ func main() {
// Example states to query
states := []string{"California", "Texas", "Florida"}
for _, state := range states {
party, err := findWinningParty(results, state)
if err != nil {
fmt.Printf("Error: %s\n", err)
} else {
fmt.Printf("%s won by %s in 2000 US election.\n", state, party)
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
}
}
}
filename = "data/electoral-college-votes.csv"
stateVotes, err := readElectoralVotes(filename)
if err != nil {
fmt.Println("Error:", err)
return
}
for _, state := range states {
votes, err := findElectoralVotes(stateVotes, state)
if err != nil {
fmt.Printf("Error: %s\n", err)
} else {
fmt.Printf("%s has %d electoral votes.\n", state, votes)
}
}
}