-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (81 loc) · 3.03 KB
/
main.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
100
101
102
103
104
package main
import (
"fmt"
ds "various/data_structures"
"various/ex"
"various/ex/iter"
)
func main() {
// Parse a stream of tokens into a run-length encoding.
tokens := []string{"a", "a", "a", "b", "b", "c", "c", "d", "d", "e"}
rlePairs := ex.RLE_iter(tokens)
fmt.Printf("pairs: %v\n", rlePairs)
fmt.Printf("--------------------------------------------------------------\n")
// RLE with functional-like impl
rlePairs = ex.RLErec(tokens)
fmt.Printf("pairs: %v\n", rlePairs)
fmt.Printf("--------------------------------------------------------------\n")
// Pack consecutive duplicates of list elements into sublists.
packed := ex.Pack([]int{1, 1, 1, 2, 3, 3, 3, 4, 4, 4, 5})
fmt.Printf("packed: %v\n", packed)
fmt.Printf("--------------------------------------------------------------\n")
// Slice within.
sliceSrc := []string{
"nothing",
"something",
"anything",
"everything",
"something_again",
"everything_again",
"anything_again",
}
from, to := 2, 5
sliced := ex.SliceWithin(sliceSrc, from, to)
fmt.Printf("sliced: %v\n", sliced)
sliced = ex.SliceWithinIterative(sliceSrc, from, to) // impl super trivial
fmt.Printf("sliced: %v\n", sliced)
fmt.Printf("--------------------------------------------------------------\n")
// Rotate slice(list) right and left.
toRotate := []string{"a", "b", "c", "d", "e", "f", "g"}
places := 3
fmt.Printf("to_rotate by %d places: %v\n", places, toRotate)
rotatedRight := ex.RotateRight(toRotate, places)
fmt.Printf("rotated right : %v\n", rotatedRight)
rotatedLeft := ex.RotateLeft(toRotate, places)
fmt.Printf("rotated left : %v\n", rotatedLeft)
// Lotto: Draw N Different Random Numbers From the Set 1..M.
lottoNumbers := ex.EasyLotto(6, 50)
fmt.Printf("lotto_numbers: %v\n", lottoNumbers)
fmt.Printf("--------------------------------------------------------------\n")
// Binary search tree
bst := ds.NewBST("alpha")
sortedInsertSource := []string{"beta", "gamma", "delta"}
// This bulk insertion using a binary recursion algo is to ensure the tree
// is balanced. But it assumes the input is sorted. If it is not, it will
// run, but the resulting tree will not be balanced as intended.
bst.InsertFromSorted(sortedInsertSource)
bst.Insert("delta")
bst.Insert("gamma")
bst.Insert("beta")
bst.TraversePreOrder()
// Take another look at deletion..
toDelete := "gamma"
if ok := bst.Delete(toDelete); ok {
fmt.Printf("tree traversal following %q deletion\n", toDelete)
} else {
fmt.Printf("Could not delete %q from tree\n", toDelete)
}
bst.TraversePreOrder()
fmt.Printf("--------------------------------------------------------------\n")
chunks := ex.Chunk[string](
[]string{"aa", "ab", "ac", "ba", "bb", "bc", "ca", "cb", "cc"},
3,
)
fmt.Printf("chunks: %+v\n", chunks)
fmt.Printf("function iterators--------------------------------------------\n")
wordsOfWisdom := []string{"hello", "world", "you", "suck", "so", "bad"}
onlyEven := func(idx int) bool { return idx%2 == 0 }
for i, x := range iter.PickOnCond(wordsOfWisdom, onlyEven) {
fmt.Println(i, x)
}
}