-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
49 lines (37 loc) · 877 Bytes
/
types.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
package wrand
import "sort"
type Item struct {
Value int
Weight int
}
type ItemsCollection struct {
Items []Item
}
func NewItemsCollection(items []Item) *ItemsCollection {
return &ItemsCollection{Items: items}
}
//@todo: need to check for uniqueness `val`
func (ic *ItemsCollection) Add(val int, weight int) *ItemsCollection {
ic.Items = append(ic.Items, Item{Value: val, Weight: weight})
return ic
}
func (ic *ItemsCollection) GetWeightSum() int {
sum := 0
for _, item := range ic.Items {
sum += item.Weight
}
return sum
}
func (ic *ItemsCollection) SortByWeightDesc() *ItemsCollection {
//sort items by weight
sort.Slice(ic.Items, func(i, j int) bool {
return ic.Items[i].Weight < ic.Items[j].Weight
})
return ic
}
func (ic *ItemsCollection) Count() int {
return len(ic.Items)
}
func (ic *ItemsCollection) GetAll() []Item {
return ic.Items
}