unify structs
This commit is contained in:
parent
120d6f860d
commit
aad1cd725e
162
main.go
162
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error opening the file: %v", err)
|
return nil, fmt.Errorf("error opening the results file for %s: %v", year, err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer resultsFile.Close()
|
||||||
|
resultsReader := csv.NewReader(resultsFile)
|
||||||
reader := csv.NewReader(file)
|
if _, err := resultsReader.Read(); err != nil { // Skip header
|
||||||
// Skip the header
|
return nil, fmt.Errorf("error reading results header for %s: %v", year, 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 := resultsReader.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break // End of file or an error
|
break // EOF or an error
|
||||||
}
|
}
|
||||||
results = append(results, StateResult{State: record[0], Party: record[1]})
|
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
|
||||||
if len(results) == 0 {
|
switch year {
|
||||||
return nil, fmt.Errorf("no data found")
|
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
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 _, stateData := range stateDatas {
|
||||||
for _, state := range states {
|
for _, state := range states {
|
||||||
party, err := findWinningParty(results, state)
|
if strings.EqualFold(stateData.State, state) {
|
||||||
if err != nil {
|
fmt.Printf("%s: Votes: %d, Winners: 2000 - %s, 2004 - %s, 2008 - %s, 2012 - %s, 2016 - %s, 2020 - %s\n",
|
||||||
fmt.Printf("Error: %s\n", err)
|
stateData.State, stateData.Votes,
|
||||||
} else {
|
stateData.Party2000, stateData.Party2004, stateData.Party2008,
|
||||||
fmt.Printf("%s won by %s in 2000 US election.\n", state, party)
|
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