get state by state sampling done

This commit is contained in:
NunoSempere 2024-04-13 15:26:18 -04:00
parent 18cb3710d9
commit d1dc3a5e75

54
main.go
View File

@ -3,17 +3,30 @@ package main
import ( import (
"encoding/csv" "encoding/csv"
"fmt" "fmt"
rand "math/rand/v2"
"os" "os"
"strconv" "strconv"
"strings" // "strings"
) )
/* Structs */
type State struct { type State struct {
Name string Name string
Votes int Votes int
VictoriousPartyPerElection map[string]string 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) { func readStates() ([]State, error) {
var states map[string]State = make(map[string]State) var states map[string]State = make(map[string]State)
@ -81,11 +94,6 @@ func readStates() ([]State, error) {
return states_slice, nil return states_slice, nil
} }
type VotesForEachParty struct {
Democrats int
Republicans int
}
func sampleFromState(state State) VotesForEachParty { func sampleFromState(state State) VotesForEachParty {
switch state.Name { switch state.Name {
case "Nebraska": case "Nebraska":
@ -93,7 +101,20 @@ func sampleFromState(state State) VotesForEachParty {
case "Maine": case "Maine":
return VotesForEachParty{Democrats: 1, Republicans: 0} return VotesForEachParty{Democrats: 1, Republicans: 0}
default: 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 return
} }
// Example states to query
example_state_names := []string{"California", "Texas", "Florida"}
for _, state := range states { for _, state := range states {
for _, example_state_name := range example_state_names { fmt.Printf("%s: Votes: %d,\n\tWinners: ", state.Name, state.Votes)
if strings.EqualFold(state.Name, example_state_name) { for year, party := range state.VictoriousPartyPerElection {
fmt.Printf("%s: Votes: %d, Winners: ", state.Name, state.Votes) fmt.Printf("[%s: %s] ", year, party)
for year, party := range state.VictoriousPartyPerElection {
fmt.Printf("[%s: %s] ", year, party)
}
fmt.Println()
break
}
} }
election_sample := sampleFromState(state)
fmt.Printf("\n\tSample: Democrat seats: %d, Republican seats: %d", election_sample.Democrats, election_sample.Republicans)
fmt.Println()
} }
} }