From d1dc3a5e750e09ea4815acd987e8a4720d4e83ca Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 13 Apr 2024 15:26:18 -0400 Subject: [PATCH] get state by state sampling done --- main.go | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 853c7eb..98c9edc 100644 --- a/main.go +++ b/main.go @@ -3,17 +3,30 @@ package main import ( "encoding/csv" "fmt" + rand "math/rand/v2" "os" "strconv" - "strings" + // "strings" ) +/* Structs */ type State struct { Name string Votes int VictoriousPartyPerElection map[string]string } +type VotesForEachParty struct { + Democrats int + Republicans int +} + +type src = *rand.Rand + +/* Globals */ +var r = rand.New(rand.NewPCG(uint64(1), uint64(2))) + +/* Load data from csvs */ func readStates() ([]State, error) { var states map[string]State = make(map[string]State) @@ -81,11 +94,6 @@ func readStates() ([]State, error) { return states_slice, nil } -type VotesForEachParty struct { - Democrats int - Republicans int -} - func sampleFromState(state State) VotesForEachParty { switch state.Name { case "Nebraska": @@ -93,7 +101,20 @@ func sampleFromState(state State) VotesForEachParty { case "Maine": return VotesForEachParty{Democrats: 1, Republicans: 0} default: - return VotesForEachParty{Democrats: 1, Republicans: 0} + { + p_republican := 0.0 + for _, party := range state.VictoriousPartyPerElection { + if party == "R" { + p_republican++ + } + } + p_republican = p_republican / float64(len(state.VictoriousPartyPerElection)) + if r.Float64() < p_republican { + return VotesForEachParty{Democrats: 0, Republicans: state.Votes} + } else { + return VotesForEachParty{Democrats: state.Votes, Republicans: 0} + } + } } } @@ -104,20 +125,15 @@ func main() { return } - // Example states to query - example_state_names := []string{"California", "Texas", "Florida"} - for _, state := range states { - for _, example_state_name := range example_state_names { - if strings.EqualFold(state.Name, example_state_name) { - fmt.Printf("%s: Votes: %d, Winners: ", state.Name, state.Votes) - for year, party := range state.VictoriousPartyPerElection { - fmt.Printf("[%s: %s] ", year, party) - } - fmt.Println() - break - } + fmt.Printf("%s: Votes: %d,\n\tWinners: ", state.Name, state.Votes) + for year, party := range state.VictoriousPartyPerElection { + fmt.Printf("[%s: %s] ", year, party) } + election_sample := sampleFromState(state) + fmt.Printf("\n\tSample: Democrat seats: %d, Republican seats: %d", election_sample.Democrats, election_sample.Republicans) + fmt.Println() + } }