Working through the golang tour

Exercise: rot13Reader
https://tour.golang.org/methods/23

This was was tougher than I thought,  Mainly because you really have to understand the implied pieces that are needed.  I ended up solving this with a map rather than the github answer key I found.

package main

import (
"io"
"os"
"strings"
)

type rot13Reader struct {
r io.Reader
}

func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}


func (rot13 rot13Reader) Read(b []byte) (n int, err error) {
n,err = rot13.r.Read(b)
for i:=0;i<n;i++ {
b[i] = m[b[i]]
}
return n,err
}

var m = map[byte]byte{
'A': 'N', 'B': 'O', 'C': 'P', 'D': 'Q', 'E': 'R', 'F': 'S', 'G': 'T', 'H': 'U',
'I': 'V', 'J': 'W', 'K': 'X', 'L': 'Y', 'M': 'Z', 'N': 'A', 'O': 'B', 'P': 'C',
'Q': 'D', 'R': 'E', 'S': 'F', 'T': 'G', 'U': 'H', 'V': 'I', 'W': 'J', 'X': 'K',
'Y': 'L', 'Z': 'M', 'a': 'n', 'b': 'o', 'c': 'p', 'd': 'q', 'e': 'r', 'f': 's',
'g': 't', 'h': 'u', 'i': 'v', 'j': 'w', 'k': 'x', 'l': 'y', 'm': 'z', 'n': 'a',
'o': 'b', 'p': 'c', 'q': 'd', 'r': 'e', 's': 'f', 't': 'g', 'u': 'h', 'v': 'i',
'w': 'j', 'x': 'k', 'y': 'l', 'z': 'm', ' ': ' ',
}





Comments

Popular posts from this blog

Xubuntu Home Server on Dell XPS 13 9370

Cygwin + syslog-ng

Installing Fedora 21 on a (late 2014) Mac Mini