-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
214 lines (198 loc) · 4.04 KB
/
functions.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package a1
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
"strconv"
)
type Time24 struct {
hour, minute, second uint8
}
func countPrimes(n int) int {
count := 0
if n <= 1 {
return count
}
boolArr := make([]bool, n+1)
for i := 2; i < n+1; i++ {
if boolArr[i] {
continue
}
for j := i * i; j < n+1; j += i {
boolArr[j] = true
}
count++
}
return count
}
func countStrings(filename string) map[string]int {
m := make(map[string]int)
absPath, _ := filepath.Abs(filename)
file, err := os.Open(absPath)
if err != nil {
log.Fatalf("Error (function 'countStrings') %v", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
if _, ok := m[scanner.Text()]; ok {
m[scanner.Text()]++
} else {
m[scanner.Text()] = 1
}
}
return m
}
func equalsTime24(a Time24, b Time24) bool {
return a.hour == b.hour && a.minute == b.minute && a.second == b.second
}
func lessThanTime24(a Time24, b Time24) bool {
if a.hour > b.hour {
return false
} else if a.hour == b.hour {
if a.minute > b.minute {
return false
} else if a.minute == b.minute {
if a.second < b.second {
return true
}
return false
} else {
return true
}
} else {
return true
}
}
func (t Time24) String() string {
var prefixHr, prefixMin, prefixSec string
if t.hour < 10 {
prefixHr = "0"
}
if t.minute < 10 {
prefixMin = "0"
}
if t.second < 10 {
prefixSec = "0"
}
return prefixHr + strconv.Itoa(int(t.hour)) + ":" + prefixMin + strconv.Itoa(int(t.minute)) + ":" + prefixSec + strconv.Itoa(int(t.second))
}
func (t Time24) validTime24() bool {
return t.hour < 24 && t.minute < 60 && t.second < 60
}
func minTime24(times []Time24) (Time24, error) {
minTime := Time24{hour: 23, minute: 59, second: 59}
if len(times) == 0 {
minTime := Time24{hour: 0, minute: 0, second: 0}
return minTime, errors.New("input must not be empty")
}
for i := 0; i < len(times); i++ {
if !times[i].validTime24() {
minTime := Time24{hour: 0, minute: 0, second: 0}
return minTime, fmt.Errorf("time '%s' at position '%v' is not valid", times[i].String(), i+1)
}
}
for i := 0; i < len(times); i++ {
if lessThanTime24(times[i], minTime) {
minTime = times[i]
}
}
return minTime, nil
}
func linearSearch(x interface{}, lst interface{}) int {
index := -1
switch reflect.TypeOf(lst).Kind() {
case reflect.Slice:
s := reflect.ValueOf(lst)
for i := 0; i < s.Len(); i++ {
if reflect.TypeOf(x) != s.Index(i).Type() {
message := "type '" + reflect.TypeOf(x).String() + "' of search value does not match type '" + s.Index(i).Type().String() + "' of list values"
panic(message)
}
switch x.(type) {
case int:
if int64(x.(int)) == s.Index(i).Int() {
index = i
}
case float64:
if x.(float64) == s.Index(i).Float() {
index = i
}
case string:
if x.(string) == s.Index(i).String() {
index = i
}
default:
panic("type is not supported")
}
}
}
return index
}
func binarySliceSize(n int) int {
if n <= 0 {
return 0
}
size := 2
for i := 1; i < n; i++ {
size *= 2
}
return size
}
func binarySequence(n int, bStr string) []int {
b := make([]int, n)
j := n - 1
for i := len(bStr) - 1; i >= 0; i-- {
b[j], _ = strconv.Atoi(string(bStr[i]))
j--
}
return b
}
func allBitSeqs(n int) [][]int {
b := make([][]int, binarySliceSize(n))
for i := 0; i < len(b); i++ {
b[i] = binarySequence(n, strconv.FormatInt(int64(i), 2))
}
return b
}
func sliceMatch(a []int, b []int) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func sliceContains(a [][]int, b []int) bool {
for i := range a {
if sliceMatch(a[i], b) {
return true
}
}
return false
}
func multiSliceMatch(a [][]int, b [][]int) (bool, []int) {
if len(a) != len(b) {
return false, nil
}
for i := range b {
if !sliceContains(a, b[i]) {
return false, b[i]
}
}
return true, nil
}