-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirect.go
78 lines (69 loc) · 1.47 KB
/
direct.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
package main
import "bytes"
func SwapPosition(s []byte, x, y int) []byte {
newBuf := make([]byte, len(s))
copy(newBuf, s)
newBuf[x], newBuf[y] = s[y], s[x]
return newBuf
}
func SwapLetters(s []byte, x, y byte) []byte {
newBuf := make([]byte, len(s))
for i := range s {
switch s[i] {
case x:
newBuf[i] = y
case y:
newBuf[i] = x
default:
newBuf[i] = s[i]
}
}
return newBuf
}
func Move(s []byte, x, y int) []byte {
newBuf := make([]byte, 0, len(s))
if x < y {
newBuf = append(newBuf, s[:x]...)
newBuf = append(newBuf, s[x+1:y+1]...)
newBuf = append(newBuf, s[x])
newBuf = append(newBuf, s[y+1:]...)
} else {
newBuf = append(newBuf, s[:y]...)
newBuf = append(newBuf, s[x])
newBuf = append(newBuf, s[y:x]...)
newBuf = append(newBuf, s[x+1:]...)
}
return newBuf
}
func reverse(a []byte) []byte {
b := make([]byte, len(a))
for i := 0; i < len(a); i++ {
opp := len(a) - i - 1
b[i] = a[opp]
}
return b
}
func Reverse(s []byte, x, y int) []byte {
newBuf := make([]byte, 0, len(s))
newBuf = append(newBuf, s[:x]...)
newBuf = append(newBuf, reverse(s[x:y+1])...)
newBuf = append(newBuf, s[y+1:]...)
return newBuf
}
func RotateBasedOnLetter(s []byte, x byte) []byte {
idx := bytes.IndexByte(s, x)
if idx >= 4 {
idx++
}
return RotateLeft(s, -(1 + idx))
}
func RotateLeft(s []byte, x int) []byte {
for x < 0 {
x += 10 * len(s)
}
newBuf := make([]byte, len(s))
for i := 0; i < len(s); i++ {
newBuf[i] = s[(i+x)%len(s)]
}
return newBuf
}