-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.go
75 lines (65 loc) · 1.6 KB
/
hello.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
67
68
69
70
71
72
73
74
75
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"os"
"strconv"
)
// NoOfStudents just checks if rollNumber is valid or not
// Can be 1 or 0
func NoOfStudents(rollNumber string) int {
if rollNumber != "-" {
return 1
}
return 0
}
// AddCount adds the student in department count
func AddCount(rollNumber string, department string, m map[string]int) {
m[department] = m[department] + 1
}
func displayData(currentFloor int, m map[string]int) {
totalCount := 0
for department, numberOfStudents := range m {
if department == "- -" {
continue
}
fmt.Printf("[%s]: [%d] ", department, numberOfStudents)
totalCount += numberOfStudents
delete(m, department)
}
fmt.Printf("\nTotal students in Floor %d: %d ", currentFloor, totalCount)
fmt.Println()
}
func main() {
FILE := os.Args[1]
STARTINGFLOOR := 1
fmt.Printf("-------- Analysing %s starting with floor %d -------- \n", FILE, STARTINGFLOOR)
csvFile, _ := os.Open(FILE)
data := csv.NewReader(bufio.NewReader(csvFile))
currentFloor := STARTINGFLOOR
m := make(map[string]int)
for {
record, err := data.Read()
if err == io.EOF {
break
}
roomNo := record[2]
rollNumber := record[3]
department := record[5] + " " + record[6]
val, _ := strconv.Atoi(roomNo)
if val/100 == currentFloor {
AddCount(rollNumber, department, m)
} else {
// Floor has been changed, display the data and reset
displayData(currentFloor, m)
// Actulally change the floor
currentFloor = val / 100
AddCount(rollNumber, department, m)
}
}
// for the last loop
displayData(currentFloor, m)
fmt.Printf("-------- DONE! -------- \n")
}