-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.go
66 lines (55 loc) · 1.8 KB
/
io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)
//ReadElectoralVotes takes in a filename from which to read electoral votes.
//It returns a map of state names to number of Electoral College Votes.
func ReadElectoralVotes(filename string) map[string]uint {
data, err := ioutil.ReadFile(filename)
Check(err)
// each line of the datadata is stored in the format:
// stateName, votes where votes = number of Electoral College votes.
lines := strings.Split(string(data), "\n")
//we create a map of strings to unsigned integers.
electoralVotes := make(map[string]uint)
//now, parse out data on each line and add to electoralVotes map
for j := range lines {
var line = strings.Split(lines[j], ",")
stateName := line[0]
fmt.Println(line[0], len(line))
votes, err := strconv.Atoi(line[1])
electoralVotes[stateName] = uint(votes)
Check(err)
}
return electoralVotes
}
//ReadPollingData takes in a filename from which to read data.
//It returns a map of state names to polling percentage for candidate 1.
func ReadPollingData(filename string) map[string]float64 {
data, err := ioutil.ReadFile(filename)
Check(err)
// each line of the datadata is stored in the format:
// stateName, x, y where x = candidate 1 percentage,
// y = candidate 2 percentage
lines := strings.Split(string(data), "\n")
candidate1Percentages := make(map[string]float64)
//now, parse out data on each line and add data to map.
for j := range lines {
var line = strings.Split(lines[j], ",")
stateName := line[0]
percentage1, err := strconv.ParseFloat(line[1], 64)
// normalize percentages between 0 and 1
candidate1Percentages[stateName] = percentage1 / 100.0
Check(err)
}
return candidate1Percentages
}
// Check for an error (really should not be exported!)
func Check(e error) {
if e != nil {
panic(e)
}
}