Created
January 15, 2019 11:43
-
-
Save patrickbrandt/85fb7ba3e12a203ab73c99898be796a0 to your computer and use it in GitHub Desktop.
My answer to rot13
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
// my answer: https://tour.golang.org/methods/23 | |
// their answer: https://github.com/golang/tour/blob/master/solutions/rot13.go | |
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
"bytes" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (mr rot13Reader) Read(b []byte) (int, error) { | |
alpha := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") | |
rot13 := []byte("NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm") | |
n, err := mr.r.Read(b) | |
for index, value := range b { | |
alphaIndex := bytes.IndexByte(alpha, value) | |
if alphaIndex > 0 { | |
b[index] = rot13[alphaIndex] | |
} | |
} | |
return n, err | |
} | |
func main() { | |
s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment