-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinverse.go
49 lines (36 loc) · 1.07 KB
/
inverse.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
package main
import "bytes"
func InverseSwapPosition(s []byte, x, y int) []byte {
return SwapPosition(s, y, x)
}
func InverseSwapLetters(s []byte, x, y byte) []byte {
return SwapLetters(s, y, x)
}
func InverseMove(s []byte, x, y int) []byte {
return Move(s, y, x)
}
func InverseReverse(s []byte, x, y int) []byte {
return Reverse(s, x, y)
}
// This function works only on 8-letters strings
// But the assignment itself use 4 as threshold
func InverseRotateBasedOnLetter(s []byte, x byte) []byte {
// cancel the +1 on all rotation
s = RotateLeft(s, 1)
idx := bytes.IndexByte(s, x)
// all numbers with idx >= 4 are moved to idx+idx+1, so 2*idx+1, so odd
// all numbers with idx<4 are moved to idx+idx, so 2*idx, so even
if idx%2 != 0 {
// this cancel the "+1" on idx>=4
idx++
// (idx + len) because we finished a loop and started again)
// /2 cancel the operation 2*
return RotateLeft(s, (idx+len(s))/2)
} else {
// idx/2 cancel the operation 2*
return RotateLeft(s, idx/2)
}
}
func InverseRotateLeft(s []byte, x int) []byte {
return RotateLeft(s, -x)
}