-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecode.go
More file actions
156 lines (117 loc) · 2.57 KB
/
Copy pathrecode.go
File metadata and controls
156 lines (117 loc) · 2.57 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
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"
// "strings"
// )
// func main() {
// fmt.Println(wordFreq("go is go"))
// }
// func wordFreq(s string) map[string]int {
// word := make(map[string]int)
// for _, i := range strings.Fields(strings.ToLower(s)) {
// word[i]++
// }
// return word
// }
// package main
// import (
// "errors"
// "fmt"
// )
// func main() {
// fmt.Println(maxInSlice([]int{3, 1, 4, 1, 5, 9}))
// }
// func maxInSlice(nums []int) (int, error) {
// if len(nums) == 0 {
// return 0, errors.New("error")
// }
// n := nums[0]
// for i := 1; i <= len(nums[1:]); i++ {
// if nums[i] > n {
// n = nums[i]
// }
// }
// return n, errors.New("nil")
// }
// package main
// import (
// "fmt"
// "strings"
// )
// func main() {
// words := []string{"this", "is", "so", "exciting"}
// fmt.Println(uppercaseLastN(words, 2))
// }
// func uppercaseLastN(words []string, n int) []string {
// var result []string
// for i := 0; i < len(words); i++ {
// if i >= len(words)-n {
// result = append(result, strings.ToUpper(words[i]))
// continue
// }
// result = append(result, words[i])
// }
// return result
// }
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(isPalindrome("racecar")) // true
fmt.Println(isPalindrome("Go")) // false
fmt.Println(isPalindrome("RaceCar")) // true
fmt.Println(isPalindrome("")) // true
fmt.Println(isPalindrome("a")) // true
}
func isPalindrome(s string) bool {
s = strings.ToLower(s)
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
if s[i] != s[j] {
return false
}
}
return true
}
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(fixQuotes("' awesome '")) // 'awesome'
fmt.Println(fixQuotes("' hello world '")) // 'hello world'
fmt.Println(fixQuotes("say ' hi ' to ' me '")) // say 'hi' to 'me'
fmt.Println(fixQuotes("no quotes here")) // no quotes here
}
func fixQuotes(s string) string {
parts := strings.Split(s, "'")
for i := 1; i < len(parts); i += 2 {
parts[i] = strings.Join(strings.Fields(parts[i]), " ")
}
return strings.Join(parts, "'")
}