-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
239 lines (182 loc) · 4.77 KB
/
Copy pathmain.go
File metadata and controls
239 lines (182 loc) · 4.77 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// // // // package main
// // // // import "fmt"
// // // // func chunk(s []int, size int) [][]int {
// // // // if size <= 0 {
// // // // return nil
// // // // }
// // // // var result [][]int
// // // // for i := 0; i < len(s); i += size {
// // // // end := min(i+size, len(s))
// // // // result = append(result, s[i:end])
// // // // }
// // // // return result
// // // // }
// // // // func main() {
// // // // fmt.Println(chunk([]int{1, 2, 3, 6, 4, 5}, 2)) // [[1 2] [3 4] [5]]
// // // // fmt.Println(chunk([]int{1, 2, 3, 4}, 2)) // [[1 2] [3 4]]
// // // // fmt.Println(chunk([]int{}, 3)) // []
// // // // fmt.Println(chunk([]int{1, 2, 3}, 0)) // []
// // // // }
// // // package main
// // // import (
// // // "fmt"
// // // "sort"
// // // "strings"
// // // )
// // // func groupAnagrams(words []string) [][]string {
// // // groups := make(map[string][]string)
// // // for _, w := range words {
// // // key := strings.Split(w, "")
// // // sort.Strings(key)
// // // k := strings.Join(key, "")
// // // groups[k] = append(groups[k], w)
// // // }
// // // result := make([][]string, 0, len(groups))
// // // for _, g := range groups {
// // // result = append(result, g)
// // // }
// // // return result
// // // }
// // // func main() {
// // // fmt.Println(groupAnagrams([]string{"eat", "tea", "tan", "ate", "nat", "bat"}))
// // // }
// // package main
// // import (
// // "fmt"
// // "regexp"
// // )
// // func fixEllipsis(s string) string {
// // spaces := regexp.MustCompile(`\s*\.\.\.`)
// // return spaces.ReplaceAllString(s, "...")
// // }
// // func main() {
// // tex := "wait ... now ... text ... "
// // fmt.Println(fixEllipsis(tex))
// // }
// package main
// import (
// "fmt"
// )
// func main() {
// tex := []string{"hello", "(hex)", "30", "(up)", "world"}
// fmt.Println(stripTags(tex))
// }
// func stripTags(words []string) []string {
// var result []string
// for _, word := range words {
// switch word {
// case "(hex)", "(up)", "(cap)", "(low)", "(bin)":
// continue
// default:
// result = append(result, word)
// }
// }
// return result
// }
// package main
// import (
// "fmt"
// )
// func main() {
// fmt.Println(caesarCipher("Hello, World!", 3))
// }
// func caesarCipher(s string, shift int) string {
// new := ""
// for _, char := range s {
// switch {
// case char >= 'a' && char <= 'z':
// x := 'a' + (char-'a'+rune(shift))%26
// new = new + string(x)
// case char >= 'A' && char <= 'Z':
// x := 'A' + (char-'A'+rune(shift))%26
// new = new + string(x)
// default:
// new = new + string(char)
// }
// }
// return new
// }
// package main
// import (
// "fmt"
// "sort"
// "strings"
// )
// func groupAnagrams(words []string) [][]string {
// groups := make(map[string][]string)
// for _, w := range words {
// key := strings.Split(w, "")
// sort.Strings(key)
// k := strings.Join(key, "")
// groups[k] = append(groups[k], w)
// }
// result := make([][]string, 0, len(groups))
// for _, g := range groups {
// result = append(result, g)
// }
// return result
// }
// func main() {
// fmt.Println(groupAnagrams([]string{"eat", "tea", "tan", "ate", "nat", "bat"}))
// }
// package main
// import (
// "fmt"
// "strings"
// )
// func splitPunctuation(s string) []string {
// puncs := ",.?!;:"
// idx := strings.IndexAny(s, puncs)
// if idx == -1 {
// return []string{s}
// }
// rest := strings.TrimLeft(s[idx:], puncs)
// return append([]string{s[:idx], s[idx : len(s)-len(rest)]}, splitPunctuation(rest)...)
// }
// func main() {
// fmt.Println(splitPunctuation("hello,")) // [hello ,]
// fmt.Println(splitPunctuation("hello...")) // [hello ...]
// fmt.Println(splitPunctuation("hello,world")) // [hello , world]
// }
// package main
// import (
// "fmt"
// "regexp"
// )
// var ellipsisRe = regexp.MustCompile(`\s+\.\.\.`)
// func fixEllipsis(s string) string {
// return ellipsisRe.ReplaceAllString(s, "...")
// }
// func main() {
// fmt.Println(fixEllipsis("wait ... now")) // wait... now
// fmt.Println(fixEllipsis("wait ... now")) // wait... now
// fmt.Println(fixEllipsis("wait...now")) // wait...now
// }
// Write a function that separates punctuation from words
// "hello," → ["hello", ","]
package main
import (
"fmt"
)
func main() {
fmt.Println(splitPunc("Hello!sjf,aih:di"))
}
func splitPunc(s string) []string {
arr := []string{}
var result string
for _, i := range s {
if i == ',' || i == '.' || i == '?' || i == '!' || i == ':' || i == ';' {
if result != "" {
arr = append(arr, result)
result = ""
}
arr = append(arr, string(i))
} else {
result = result + string(i)
}
}
if result != "" {
arr = append(arr, result)
}
return arr
}