use array for election results instead of individual year
This commit is contained in:
parent
08571ee9b6
commit
18cb3710d9
53
main.go
53
main.go
|
@ -9,19 +9,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
Name string
|
Name string
|
||||||
Votes int
|
Votes int
|
||||||
Party2000 string
|
VictoriousPartyPerElection map[string]string
|
||||||
Party2004 string
|
|
||||||
Party2008 string
|
|
||||||
Party2012 string
|
|
||||||
Party2016 string
|
|
||||||
Party2020 string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readStates() ([]State, error) {
|
func readStates() ([]State, error) {
|
||||||
var states map[string]State = make(map[string]State)
|
var states map[string]State = make(map[string]State)
|
||||||
var years = []string{"2000", "2004", "2008", "2012", "2016", "2020"}
|
|
||||||
|
|
||||||
/* Electoral college votes for the 2024 election*/
|
/* Electoral college votes for the 2024 election*/
|
||||||
votes_file, err := os.Open("data/electoral-college-votes.csv")
|
votes_file, err := os.Open("data/electoral-college-votes.csv")
|
||||||
|
@ -45,11 +39,12 @@ func readStates() ([]State, error) {
|
||||||
}
|
}
|
||||||
state := csv_record[0]
|
state := csv_record[0]
|
||||||
if _, exists := states[state]; !exists {
|
if _, exists := states[state]; !exists {
|
||||||
states[state] = State{Name: state, Votes: votes}
|
states[state] = State{Name: state, Votes: votes, VictoriousPartyPerElection: make(map[string]string)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Election results */
|
/* Election results */
|
||||||
|
var years = []string{"2000", "2004", "2008", "2012", "2016", "2020"}
|
||||||
for _, year := range years {
|
for _, year := range years {
|
||||||
results_filename := fmt.Sprintf("data/results/%s.csv", year)
|
results_filename := fmt.Sprintf("data/results/%s.csv", year)
|
||||||
results_file, err := os.Open(results_filename)
|
results_file, err := os.Open(results_filename)
|
||||||
|
@ -72,31 +67,18 @@ func readStates() ([]State, error) {
|
||||||
continue // State not found in votes map, skip
|
continue // State not found in votes map, skip
|
||||||
}
|
}
|
||||||
// Update the party winning in the specific year
|
// Update the party winning in the specific year
|
||||||
switch year {
|
data.VictoriousPartyPerElection[year] = party
|
||||||
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
|
|
||||||
}
|
|
||||||
states[state] = data
|
states[state] = data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert statesData map to a slice for returning
|
// Convert statesData map to a slice for returning
|
||||||
var dataSlice []State
|
var states_slice []State
|
||||||
for _, data := range states {
|
for _, state := range states {
|
||||||
dataSlice = append(dataSlice, data)
|
states_slice = append(states_slice, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
return dataSlice, nil
|
return states_slice, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type VotesForEachParty struct {
|
type VotesForEachParty struct {
|
||||||
|
@ -124,15 +106,18 @@ func main() {
|
||||||
|
|
||||||
// Example states to query
|
// Example states to query
|
||||||
example_state_names := []string{"California", "Texas", "Florida"}
|
example_state_names := []string{"California", "Texas", "Florida"}
|
||||||
for _, states := range states {
|
|
||||||
|
for _, state := range states {
|
||||||
for _, example_state_name := range example_state_names {
|
for _, example_state_name := range example_state_names {
|
||||||
if strings.EqualFold(states.Name, example_state_name) {
|
if strings.EqualFold(state.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: ", state.Name, state.Votes)
|
||||||
states.Name, states.Votes,
|
for year, party := range state.VictoriousPartyPerElection {
|
||||||
states.Party2000, states.Party2004, states.Party2008,
|
fmt.Printf("[%s: %s] ", year, party)
|
||||||
states.Party2012, states.Party2016, states.Party2020)
|
}
|
||||||
|
fmt.Println()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user