-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (58 loc) · 1.32 KB
/
main.go
File metadata and controls
63 lines (58 loc) · 1.32 KB
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
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
servers := make(map[string][]int)
fmt.Println(allocate("storage", servers))
fmt.Println(allocate("storage", servers))
deallocate("storage-1", servers)
fmt.Println(allocate("storage", servers))
fmt.Println(allocate("metrics", servers))
}
func smallestNum(arr []int) int {
numMaps := make(map[int]bool)
for _, num := range arr {
numMaps[num] = true
}
bigInt := 10000000
for x := 1; x < bigInt; x++ {
if !numMaps[x] {
return x
}
}
return -1
}
func allocate(name string, servers map[string][]int) string {
if _, ok := servers[name]; !ok {
serverId := []int{}
servers[name] = serverId
}
newId := smallestNum(servers[name])
servers[name] = append(servers[name], newId)
newName := fmt.Sprintf("%v-%v", name, newId)
return newName
}
func deallocate(name string, servers map[string][]int) error {
nameArray := strings.Split(name, "-")
serverName := nameArray[0]
serverId := nameArray[1]
serverIdInt, err := strconv.Atoi(serverId)
if err != nil {
panic(err)
}
if _, ok := servers[serverName]; !ok {
return fmt.Errorf("server name does not exist")
}
serverIds := servers[serverName]
newIds := []int{}
for _, id := range serverIds {
if id != serverIdInt {
newIds = append(newIds, id)
}
}
servers[serverName] = newIds
return nil
}