-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_example_test.go
99 lines (81 loc) · 2.5 KB
/
counter_example_test.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
97
98
99
package godino
import "fmt"
func ExampleNewCounter() {
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"}
counter := NewCounter(fruits)
fmt.Println(counter.Elements())
// Output:
// [{apple 1} {banana 3} {orange 2}]
}
func ExampleCounter_Add() {
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"}
counter := NewCounter(fruits)
fmt.Println(counter.Get("apple"))
// Adding an element already in the counter
counter.Add("apple")
fmt.Println(counter.Get("apple"))
// Adding an element not yet in the counter
counter.Add("grape")
fmt.Println(counter.Get("grape"))
// Output:
// 1
// 2
// 1
}
func ExampleCounter_Elements() {
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"}
counter := NewCounter(fruits)
fmt.Println(counter.Elements())
// Output:
// [{apple 1} {banana 3} {orange 2}]
}
func ExampleCounter_Get() {
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"}
counter := NewCounter(fruits)
fmt.Println(counter.Get("banana"))
fmt.Println(counter.Get("grape"))
// Output:
// 3
// 0
}
func ExampleCounter_MostCommon() {
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"}
counter := NewCounter(fruits)
top2 := counter.MostCommon(2)
fmt.Println(top2)
counter.Add("orange") // Ties are broken by the order elements were added
sortedElements := counter.MostCommon(-1)
fmt.Println(sortedElements)
// Output:
// [{banana 3} {orange 2}]
// [{banana 3} {orange 3} {apple 1}]
}
func ExampleCounter_Subtract() {
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"}
counter := NewCounter(fruits)
// Subtracting an element in the counter
counter.Subtract("banana")
fmt.Println(counter.Get("banana"))
// Subtracting an element not in the counter
counter.Subtract("grape")
fmt.Println(counter.Get("grape"))
// Output:
// 2
// -1
}
func ExampleCounter_Total() {
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"}
counter := NewCounter(fruits)
fmt.Println(counter.Total())
// Output: 6
}
func ExampleCounter_Update() {
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"}
counter := NewCounter(fruits)
moreFruits := []string{"apple", "banana", "orange", "grape", "grape"}
evenMoreFruits := []string{"apple", "banana", "pineapple"}
counter.Update(moreFruits, evenMoreFruits)
fmt.Println(counter.Elements())
// Output:
// [{apple 3} {banana 5} {orange 3} {grape 2} {pineapple 1}]
}