small style refactor

This commit is contained in:
NunoSempere 2024-04-13 14:44:00 -04:00
parent aad1cd725e
commit 87c48c5f61

54
main.go
View File

@ -8,8 +8,8 @@ import (
"strings"
)
type StateData struct {
State string
type State struct {
Name string
Votes int
Party2000 string
Party2004 string
@ -19,11 +19,11 @@ type StateData struct {
Party2020 string
}
func readStateData() ([]StateData, error) {
var statesData map[string]StateData = make(map[string]StateData)
func readStates() ([]State, error) {
var states map[string]State = make(map[string]State)
var years = []string{"2000", "2004", "2008", "2012", "2016", "2020"}
/* Electoral votes for the 2004 election*/
/* Electoral college votes for the 2024 election*/
votesFilename := "data/electoral-college-votes.csv"
votesFile, err := os.Open(votesFilename)
if err != nil {
@ -44,8 +44,8 @@ func readStateData() ([]StateData, error) {
continue // Error in converting votes, skip this record
}
state := record[0]
if _, exists := statesData[state]; !exists {
statesData[state] = StateData{State: state, Votes: votes}
if _, exists := states[state]; !exists {
states[state] = State{Name: state, Votes: votes}
}
}
@ -67,7 +67,7 @@ func readStateData() ([]StateData, error) {
break // EOF or an error
}
state, party := record[0], record[1]
data, exists := statesData[state]
data, exists := states[state]
if !exists {
continue // State not found in votes map, skip
}
@ -86,35 +86,51 @@ func readStateData() ([]StateData, error) {
case "2020":
data.Party2020 = party
}
statesData[state] = data
states[state] = data
}
}
// Convert statesData map to a slice for returning
var dataSlice []StateData
for _, data := range statesData {
var dataSlice []State
for _, data := range states {
dataSlice = append(dataSlice, data)
}
return dataSlice, nil
}
type VotesForEachParty struct {
Democrats int
Republicans int
}
func sampleFromState(state State) VotesForEachParty {
switch state.Name {
case "Nebraska":
return VotesForEachParty{Democrats: 1, Republicans: 0}
case "Maine":
return VotesForEachParty{Democrats: 1, Republicans: 0}
default:
return VotesForEachParty{Democrats: 1, Republicans: 0}
}
}
func main() {
stateDatas, err := readStateData()
states, err := readStates()
if err != nil {
fmt.Println("Error:", err)
return
}
// Example states to query
states := []string{"California", "Texas", "Florida"}
for _, stateData := range stateDatas {
for _, state := range states {
if strings.EqualFold(stateData.State, state) {
example_state_names := []string{"California", "Texas", "Florida"}
for _, states := range states {
for _, example_state_name := range example_state_names {
if strings.EqualFold(states.Name, example_state_name) {
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)
states.Name, states.Votes,
states.Party2000, states.Party2004, states.Party2008,
states.Party2012, states.Party2016, states.Party2020)
break
}
}