Last active
June 18, 2020 07:21
-
-
Save P-A-R-U-S/5a70c7d314c65c75fd1daa8f80da523d to your computer and use it in GitHub Desktop.
Facebook: Rotational Cipher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func rotationalCipher(input string, rotationFactor int) string { | |
b := []uint8(input) | |
for i:=0; i < len(input); i++ { | |
switch { | |
case 'a' <= b[i] && b[i] <= 'z': { | |
b[i] += uint8(rotationFactor) | |
for b[i] > 'z' { | |
b[i] = b[i] - 'z' + 'a' - 1 | |
} | |
} | |
case 'A' <= b[i] && b[i] <= 'Z': { | |
b[i] += uint8(rotationFactor) | |
for b[i] > 'Z' { | |
b[i] = b[i] - 'Z' + 'A' - 1 | |
} | |
} | |
case '0' <= b[i] && b[i] <= '9': { | |
b[i] += uint8(rotationFactor) | |
for b[i] > '9' { | |
b[i] = b[i] - '9' + '0' - 1 | |
} | |
} | |
} | |
} | |
return string(b) | |
} | |
func main() { | |
testDatas := []struct{ | |
input string | |
rotationFactor int | |
result string | |
} { | |
{ | |
"Zebra-493?", | |
3, | |
"Cheud-726?", | |
}, | |
{ | |
"abcdefghijklmNOPQRSTUVWXYZ0123456789", | |
39, | |
"nopqrstuvwxyzABCDEFGHIJKLM9012345678", | |
}, | |
{ | |
"1234567890-=!@#$%^&*()_+{}[]||\\<>:;'?/~`,.", | |
89765, | |
"6789012345-=!@#$%^&*()_+{}[]||\\<>:;'?/~`,.", | |
}, | |
{ | |
"1234567890-=!@#$%^&*()_+{}[]||\\<>:;'?/~`,.Zebra-493?", | |
3, | |
"4567890123-=!@#$%^&*()_+{}[]||\\<>:;'?/~`,.Cheud-726?", | |
}, | |
} | |
for _, td := range testDatas { | |
r := rotationalCipher(td.input, td.rotationFactor) | |
if r != td.result { | |
fmt.Printf("Failed: Source: %v ==> %d\n Expected:%v \n Actual: %v\n", | |
td.input, | |
td.rotationFactor, | |
td.result, | |
r) | |
} else { | |
fmt.Printf("Passed: Source: %v ==> %d\n", | |
td.input, | |
td.rotationFactor) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment