-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (51 loc) · 1.23 KB
/
main.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
/*
Caesar cipher for ASCII chars with an alphabet of 26 chars
*/
package main
import (
"fmt"
)
type test struct {
str1 string
str2 string
shift int32
}
var tests []test
func main() {
for _, t := range tests {
if caesarCipher(t.str1, t.str2, t.shift) {
fmt.Println(t.str2 + " is proper cipher of " + t.str1)
} else {
fmt.Println(t.str2 + " is not proper cipher of " + t.str1)
}
}
}
// We assume only lowercase ASCII and an alphabet of 26 letters
func caesarCipher(str1, str2 string, shift int32) bool {
var result []rune
for _, elem := range str1 {
if elem == ' ' {
result = append(result, ' ')
} else {
shift = shift % 26
newElem := elem + shift
if newElem > 'z' {
newElem = newElem - 26
}
result = append(result, newElem)
}
}
cipher := string(result)
if cipher == str2 {
return true
}
return false
}
func init() {
tests = make([]test, 0)
tests = append(tests, test{"interview question", "nsyjwanjb vzjxynts", 5})
tests = append(tests, test{"caesar cipher", "igkygx iovnkx", 6})
tests = append(tests, test{"modulus is elegant", "tvkbsbz pz lslnhua", 7})
tests = append(tests, test{"caesar cipher", "caesar cipher", 26})
tests = append(tests, test{"caesar cipher", "geiwev gmtliv", 30})
}