Skip to content

Instantly share code, notes, and snippets.

@patrickbrandt
Created January 15, 2019 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickbrandt/85fb7ba3e12a203ab73c99898be796a0 to your computer and use it in GitHub Desktop.
Save patrickbrandt/85fb7ba3e12a203ab73c99898be796a0 to your computer and use it in GitHub Desktop.
My answer to rot13
// 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