-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
96 lines (75 loc) · 2.11 KB
/
number.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"math/rand"
"sync"
)
const (
randomNumberMax = 1000
randomNumberMin = 1
)
// NumberStorage for random number storage
type NumberStorage struct {
numbers []int
lock sync.Mutex
}
// HighestOccuringNumbers returns the highest occuring numbers
//
// only returns multiple numbers if the amount of times they occurred are the same
func (nm *NumberStorage) HighestOccuringNumbers() []int {
numberMap := make(map[int]int)
highestNumbers := make(map[int]int)
lastHighItem := 0
fmt.Printf("Amount of Numbers: %d\n", len(nm.numbers))
nm.lock.Lock()
defer nm.lock.Unlock()
for _, num := range nm.numbers {
if val, ok := numberMap[num]; ok {
numberMap[num] = val + 1
} else {
numberMap[num] = 1
}
if len(highestNumbers) == 0 {
highestNumbers[num] = numberMap[num]
lastHighItem = num
}
// If the number is already in the map make a new map
// Since its now the highest
if _, ok := highestNumbers[num]; ok {
highestNumbers = map[int]int{num: numberMap[num]}
lastHighItem = num
} else if numberMap[num] == highestNumbers[lastHighItem] {
// Lets add the entry to the number map since it tied
highestNumbers[num] = numberMap[num]
lastHighItem = num
}
}
numbers := make([]int, 0, len(highestNumbers))
for k := range highestNumbers {
numbers = append(numbers, k)
}
return numbers
}
func (nm *NumberStorage) worker(wg *sync.WaitGroup, worker int, batchCount int) {
defer wg.Done()
//fmt.Printf("Worker Start: %d\n", worker)
numbers := GetRandomNumbers(worker, batchCount)
nm.lock.Lock()
nm.numbers = append(nm.numbers, numbers...)
nm.lock.Unlock()
//fmt.Printf("Worker Finshed: %d\n", worker)
}
// GetRandomNumbers a function that gets some random numbers
func GetRandomNumbers(worker int, batchCount int) []int {
num := batchCount * worker
rand.Seed(int64(num))
times := (rand.Intn(num) + 1) * 100
// Correct Way
randNumbers := make([]int, 0, times)
// Wrong Way
// randNumbers := make([]int, 0, times)
for i := 0; i <= times; i++ {
randNumbers = append(randNumbers, (rand.Intn(randomNumberMax) + randomNumberMin))
}
return randNumbers
}