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