small style refactor
This commit is contained in:
parent
aad1cd725e
commit
87c48c5f61
54
main.go
54
main.go
|
@ -8,8 +8,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StateData struct {
|
type State struct {
|
||||||
State string
|
Name string
|
||||||
Votes int
|
Votes int
|
||||||
Party2000 string
|
Party2000 string
|
||||||
Party2004 string
|
Party2004 string
|
||||||
|
@ -19,11 +19,11 @@ type StateData struct {
|
||||||
Party2020 string
|
Party2020 string
|
||||||
}
|
}
|
||||||
|
|
||||||
func readStateData() ([]StateData, error) {
|
func readStates() ([]State, error) {
|
||||||
var statesData map[string]StateData = make(map[string]StateData)
|
var states map[string]State = make(map[string]State)
|
||||||
var years = []string{"2000", "2004", "2008", "2012", "2016", "2020"}
|
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"
|
votesFilename := "data/electoral-college-votes.csv"
|
||||||
votesFile, err := os.Open(votesFilename)
|
votesFile, err := os.Open(votesFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -44,8 +44,8 @@ func readStateData() ([]StateData, error) {
|
||||||
continue // Error in converting votes, skip this record
|
continue // Error in converting votes, skip this record
|
||||||
}
|
}
|
||||||
state := record[0]
|
state := record[0]
|
||||||
if _, exists := statesData[state]; !exists {
|
if _, exists := states[state]; !exists {
|
||||||
statesData[state] = StateData{State: state, Votes: votes}
|
states[state] = State{Name: state, Votes: votes}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ func readStateData() ([]StateData, error) {
|
||||||
break // EOF or an error
|
break // EOF or an error
|
||||||
}
|
}
|
||||||
state, party := record[0], record[1]
|
state, party := record[0], record[1]
|
||||||
data, exists := statesData[state]
|
data, exists := states[state]
|
||||||
if !exists {
|
if !exists {
|
||||||
continue // State not found in votes map, skip
|
continue // State not found in votes map, skip
|
||||||
}
|
}
|
||||||
|
@ -86,35 +86,51 @@ func readStateData() ([]StateData, error) {
|
||||||
case "2020":
|
case "2020":
|
||||||
data.Party2020 = party
|
data.Party2020 = party
|
||||||
}
|
}
|
||||||
statesData[state] = data
|
states[state] = data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert statesData map to a slice for returning
|
// Convert statesData map to a slice for returning
|
||||||
var dataSlice []StateData
|
var dataSlice []State
|
||||||
for _, data := range statesData {
|
for _, data := range states {
|
||||||
dataSlice = append(dataSlice, data)
|
dataSlice = append(dataSlice, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return dataSlice, nil
|
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() {
|
func main() {
|
||||||
stateDatas, err := readStateData()
|
states, err := readStates()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error:", err)
|
fmt.Println("Error:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example states to query
|
// Example states to query
|
||||||
states := []string{"California", "Texas", "Florida"}
|
example_state_names := []string{"California", "Texas", "Florida"}
|
||||||
for _, stateData := range stateDatas {
|
for _, states := range states {
|
||||||
for _, state := range states {
|
for _, example_state_name := range example_state_names {
|
||||||
if strings.EqualFold(stateData.State, state) {
|
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",
|
fmt.Printf("%s: Votes: %d, Winners: 2000 - %s, 2004 - %s, 2008 - %s, 2012 - %s, 2016 - %s, 2020 - %s\n",
|
||||||
stateData.State, stateData.Votes,
|
states.Name, states.Votes,
|
||||||
stateData.Party2000, stateData.Party2004, stateData.Party2008,
|
states.Party2000, states.Party2004, states.Party2008,
|
||||||
stateData.Party2012, stateData.Party2016, stateData.Party2020)
|
states.Party2012, states.Party2016, states.Party2020)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user