From 3622b3ea3e26e70e71b4936c86b3602b378b3691 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 13 Apr 2024 11:10:50 -0400 Subject: [PATCH] add past votes & go code to get electoral votes --- data/2000.csv | 52 +++++++++++++++++++++++++++++++++++++++++ data/2004.csv | 52 +++++++++++++++++++++++++++++++++++++++++ data/2008.csv | 52 +++++++++++++++++++++++++++++++++++++++++ data/2012.csv | 52 +++++++++++++++++++++++++++++++++++++++++ data/2016.csv | 52 +++++++++++++++++++++++++++++++++++++++++ data/2020.csv | 51 ++++++++++++++++++++++++++++++++++++++++ main.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 374 insertions(+), 2 deletions(-) create mode 100644 data/2000.csv create mode 100644 data/2004.csv create mode 100644 data/2008.csv create mode 100644 data/2012.csv create mode 100644 data/2016.csv create mode 100644 data/2020.csv diff --git a/data/2000.csv b/data/2000.csv new file mode 100644 index 0000000..d509f88 --- /dev/null +++ b/data/2000.csv @@ -0,0 +1,52 @@ +State,Winning Party +Alabama,R +Alaska,R +Arizona,R +Arkansas,R +California,D +Colorado,R +Connecticut,D +Delaware,D +District of Columbia,D +Florida,R +Georgia,R +Hawaii,D +Idaho,R +Illinois,D +Indiana,R +Iowa,D +Kansas,R +Kentucky,R +Louisiana,R +Maine,D +Maryland,D +Massachusetts,D +Michigan,D +Minnesota,D +Mississippi,R +Missouri,R +Montana,R +Nebraska,R +Nevada,R +New Hampshire,R +New Jersey,D +New Mexico,D +New York,D +North Carolina,R +North Dakota,R +Ohio,R +Oklahoma,R +Oregon,D +Pennsylvania,D +Rhode Island,D +South Carolina,R +South Dakota,R +Tennessee,R +Texas,R +Utah,R +Vermont,D +Virginia,R +Washington,D +West Virginia,R +Wisconsin,D +Wyoming,R diff --git a/data/2004.csv b/data/2004.csv new file mode 100644 index 0000000..7fe15f3 --- /dev/null +++ b/data/2004.csv @@ -0,0 +1,52 @@ +State,Party +Alabama,R +Alaska,R +Arizona,R +Arkansas,R +California,D +Colorado,R +Connecticut,D +Delaware,D +District of Columbia,D +Florida,R +Georgia,R +Hawaii,D +Idaho,R +Illinois,D +Indiana,R +Iowa,R +Kansas,R +Kentucky,R +Louisiana,R +Maine,D +Maryland,D +Massachusetts,D +Michigan,D +Minnesota,D +Mississippi,R +Missouri,R +Montana,R +Nebraska,R +Nevada,R +New Hampshire,D +New Jersey,D +New Mexico,R +New York,D +North Carolina,R +North Dakota,R +Ohio,R +Oklahoma,R +Oregon,D +Pennsylvania,D +Rhode Island,D +South Carolina,R +South Dakota,R +Tennessee,R +Texas,R +Utah,R +Vermont,D +Virginia,R +Washington,D +West Virginia,R +Wisconsin,D +Wyoming,R diff --git a/data/2008.csv b/data/2008.csv new file mode 100644 index 0000000..04388ad --- /dev/null +++ b/data/2008.csv @@ -0,0 +1,52 @@ +State,Party +Alabama,R +Alaska,R +Arizona,R +Arkansas,R +California,D +Colorado,D +Connecticut,D +Delaware,D +District of Columbia,D +Florida,D +Georgia,R +Hawaii,D +Idaho,R +Illinois,D +Indiana,D +Iowa,D +Kansas,R +Kentucky,R +Louisiana,R +Maine,D +Maryland,D +Massachusetts,D +Michigan,D +Minnesota,D +Mississippi,R +Missouri,R +Montana,R +Nebraska,Split +Nevada,D +New Hampshire,D +New Jersey,D +New Mexico,D +New York,D +North Carolina,D +North Dakota,R +Ohio,D +Oklahoma,R +Oregon,D +Pennsylvania,D +Rhode Island,D +South Carolina,R +South Dakota,R +Tennessee,R +Texas,R +Utah,R +Vermont,D +Virginia,D +Washington,D +West Virginia,R +Wisconsin,D +Wyoming,R diff --git a/data/2012.csv b/data/2012.csv new file mode 100644 index 0000000..e9d6cdc --- /dev/null +++ b/data/2012.csv @@ -0,0 +1,52 @@ +State,Party +Alabama,R +Alaska,R +Arizona,R +Arkansas,R +California,D +Colorado,D +Connecticut,D +Delaware,D +District of Columbia,D +Florida,D +Georgia,R +Hawaii,D +Idaho,R +Illinois,D +Indiana,R +Iowa,D +Kansas,R +Kentucky,R +Louisiana,R +Maine,D +Maryland,D +Massachusetts,D +Michigan,D +Minnesota,D +Mississippi,R +Missouri,R +Montana,R +Nebraska,R +Nevada,D +New Hampshire,D +New Jersey,D +New Mexico,D +New York,D +North Carolina,R +North Dakota,R +Ohio,D +Oklahoma,R +Oregon,D +Pennsylvania,D +Rhode Island,D +South Carolina,R +South Dakota,R +Tennessee,R +Texas,R +Utah,R +Vermont,D +Virginia,D +Washington,D +West Virginia,R +Wisconsin,D +Wyoming,R diff --git a/data/2016.csv b/data/2016.csv new file mode 100644 index 0000000..4b06c3e --- /dev/null +++ b/data/2016.csv @@ -0,0 +1,52 @@ +State,Party +Alabama,R +Alaska,R +Arizona,R +Arkansas,R +California,D +Colorado,D +Connecticut,D +Delaware,D +District of Columbia,D +Florida,R +Georgia,R +Hawaii,D +Idaho,R +Illinois,D +Indiana,R +Iowa,R +Kansas,R +Kentucky,R +Louisiana,R +Maine,Split +Maryland,D +Massachusetts,D +Michigan,R +Minnesota,D +Mississippi,R +Missouri,R +Montana,R +Nebraska,Split +Nevada,D +New Hampshire,D +New Jersey,D +New Mexico,D +New York,D +North Carolina,R +North Dakota,R +Ohio,R +Oklahoma,R +Oregon,D +Pennsylvania,R +Rhode Island,D +South Carolina,R +South Dakota,R +Tennessee,R +Texas,R +Utah,R +Vermont,D +Virginia,D +Washington,D +West Virginia,R +Wisconsin,R +Wyoming,R diff --git a/data/2020.csv b/data/2020.csv new file mode 100644 index 0000000..19abdcb --- /dev/null +++ b/data/2020.csv @@ -0,0 +1,51 @@ +Alabama,R +Alaska,R +Arizona,D +Arkansas,R +California,D +Colorado,D +Connecticut,D +Delaware,D +District of Columbia,D +Florida,R +Georgia,D +Hawaii,D +Idaho,R +Illinois,D +Indiana,R +Iowa,R +Kansas,R +Kentucky,R +Louisiana,R +Maine,Split +Maryland,D +Massachusetts,D +Michigan,D +Minnesota,D +Mississippi,R +Missouri,R +Montana,R +Nebraska,Split +Nevada,D +New Hampshire,D +New Jersey,D +New Mexico,D +New York,D +North Carolina,R +North Dakota,R +Ohio,R +Oklahoma,R +Oregon,D +Pennsylvania,D +Rhode Island,D +South Carolina,R +South Dakota,R +Tennessee,R +Texas,R +Utah,R +Vermont,D +Virginia,D +Washington,D +West Virginia,R +Wisconsin,D +Wyoming,R diff --git a/main.go b/main.go index 24dcac8..4d30a24 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,70 @@ package main import ( + "encoding/csv" "fmt" + "os" + "strings" ) -func main() { - fmt.Println("Hello world") +/* Electoral votes */ +type StateVotes struct { + State string + Votes string +} + +func readElectoralVotes(filename string) ([]StateVotes, error) { + file, err := os.Open(filename) + if err != nil { + return nil, fmt.Errorf("error opening the file: %v", err) + } + defer file.Close() + + var stateVotes []StateVotes + + reader := csv.NewReader(file) + for { + record, err := reader.Read() + // End of file is expected, not an error in this context + if err != nil { + break + } + stateVotes = append(stateVotes, StateVotes{State: record[0], Votes: record[1]}) + } + + if len(stateVotes) == 0 { + return nil, fmt.Errorf("no data found") + } + + return stateVotes, nil +} + +func findElectoralVotes(stateVotes []StateVotes, stateName string) (string, error) { + for _, sv := range stateVotes { + if strings.EqualFold(sv.State, stateName) { + return sv.Votes, nil + } + } + + return "", fmt.Errorf("state not found") +} + +func main() { + filename := "data/electoral-college-votes.csv" + stateVotes, err := readElectoralVotes(filename) + if err != nil { + fmt.Println("Error:", err) + return + } + + // Example states to query + states := []string{"California", "Texas", "Florida"} + for _, state := range states { + votes, err := findElectoralVotes(stateVotes, state) + if err != nil { + fmt.Printf("Error: %s\n", err) + } else { + fmt.Printf("%s has %s electoral votes.\n", state, votes) + } + } }