Skip to content

Instantly share code, notes, and snippets.

@edwardmp
Last active March 13, 2024 20:41
Show Gist options
  • Save edwardmp/3aca97114eb19089e18d to your computer and use it in GitHub Desktop.
Save edwardmp/3aca97114eb19089e18d to your computer and use it in GitHub Desktop.
A Tour of Go, exercise rot13Reader
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (rot *rot13Reader) Read(p []byte) (n int, err error) {
n, err = rot.r.Read(p)
for i := 0; i < len(p); i++ {
if p[i] >= 'A' && p[i] < 'Z' {
p[i] = 65 + (((p[i] - 65) + 13) % 26)
} else if p[i] >= 'a' && p[i] <= 'z' {
p[i] = 97 + (((p[i] - 97) + 13) % 26)
}
}
return
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
@vojtaripa
Copy link

package main

import (
    "io"
    "os"
    "strings"
    //"fmt"

)

//GIVEN INTERFACE FOR -> rot13Reader
type rot13Reader struct {
    r io.Reader
}

//MY READ METHOD: (needs to return (n int, err error))
func (passedR rot13Reader) Read(p []byte) (int, error){
    
    //FIRST GETTING VARIABLES FROM rot13Reader that was passed in.
    n, err := passedR.r.Read(p)
    
    //THEN LOOP length of p and get / change each letter (without the loop below the program works but stays the same!
    
    for i := 0; i < len(p); i++ {
	    
	    //DEPENDING ON INPUT, ADD OR SUBTRACT 13 ACCORDINGLY:
	    switch {
	    case(p[i]>='A' && p[i]<='M'):
		    p[i] += 13
        case(p[i]>='N' && p[i]<='Z'):
		    p[i] -= 13
	    case(p[i]>='a' && p[i]<='m'):
		    p[i] += 13
        case(p[i]>='n' && p[i]<='z'):
		    p[i] -= 13		
	    default:
		    p[i]=p[i]
        }
    }
    
    //FINALLY RETURN n and error
    return n, err
    
}

func main() {
    //MAKE A NEW STRING
    s := strings.NewReader("Lbh penpxrq gur pbqr!")
    
    //MAKE A NEW rot13Reader with string above
    r := rot13Reader{s}
    
    //now copy it?
    io.Copy(os.Stdout, &r)
}

OUTPUT:

You cracked the code!

@krapie
Copy link

krapie commented Dec 9, 2022

package main

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

type rot13Reader struct {
	r io.Reader
}

func rot13(b byte) byte {
	var a, z byte
	
	switch {
		case 'a' <= b && b <= 'z':
			a, z = 'a', 'z'
		case 'A' <= b && b <= 'Z':
			a, z = 'A', 'Z'
		default:
			return b
	}
	
	return (b-a+13)%(z-a+1) + a
}

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

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

@happen2me
Copy link

Easier for understanding

package main

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

type rot13Reader struct {
	r io.Reader
}

func (r rot13Reader) Read(b []byte) (int, error) {
	n, err := r.r.Read(b)
	for i := 0; i < n; i++ {
		switch {
		case b[i] >= byte('A') && b[i] < byte('N'):
			b[i] += 13
		case b[i] >= byte('a') && b[i] < byte('n'):
			b[i] += 13
		case b[i] >= byte('N') && b[i] <= byte('Z'):
			b[i] -= 13
		case b[i] >= byte('n') && b[i] <= byte('z'):
			b[i] -= 13
		}
	}
	return n, err
}

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

@curbol
Copy link

curbol commented Jun 9, 2023

package main

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

type rot13Reader struct {
	r io.Reader
}

func (rot *rot13Reader) Read(p []byte) (n int, err error) {
	n, err = rot.r.Read(p)
	if err != nil {
		return n, err
	}
	for i := range p {
		switch {
			case p[i] >= 'A' && p[i] <= 'M':
				p[i] += 13
			case p[i] >= 'N' && p[i] <= 'Z':
				p[i] -= 13
			case p[i] >= 'a' && p[i] <= 'm':
				p[i] += 13
			case p[i] >= 'n' && p[i] <= 'z':
				p[i] -= 13
		}
	}
	return
}

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

@wildwind123
Copy link

wildwind123 commented Jul 1, 2023

package main

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

const input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const output = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"

type rot13Reader struct {
	r io.Reader
}

func (r *rot13Reader) Read(b []byte) (int, error) {
	n, err := r.r.Read(b)
	if err != nil {
		return n, err
	}

	for i := range b {
		inputIndex := strings.Index(input, string(b[i]))
		if inputIndex < 0 {
			continue
		}
		b[i] = output[inputIndex]
	}

	return n, nil
}

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

@Max95Cohen
Copy link

package main

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

type rot13Reader struct {
	r io.Reader
}

func (r rot13Reader) Read(bytes []byte) (int, error) {
	n, err := r.r.Read(bytes)

	for i := 0; i < n; i++ {
		byte := bytes[i]

		if byte >= 'A' && byte <= 'M' {
			bytes[i] += 13
		}

		if byte >= 'N' && byte <= 'Z' {
			bytes[i] -= 13
		}

		if byte >= 'a' && byte <= 'm' {
			bytes[i] += 13
		}

		if byte >= 'n' && byte <= 'z' {
			bytes[i] -= 13
		}
	}

	return n, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}

	io.Copy(os.Stdout, &r)
}

@imtinanahmad-xpertdigi
Copy link

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 (rot rot13Reader) Read(b []byte) (int, error) {
v, err := rot.r.Read(b)

for i := 0; i < v; i++ {
	b[i] = rot13(string(b[i]))[0]
}

if err != nil {
	return 0, err
}

return v, nil

}

func rot13(s string) (string) {
s = strings.ToUpper(s)

for i := 0; i < len(s); i++ {

	if string(s[i]) < "M" {
		return string( s[i] + 13)
	}

	if string(s[i]) > "M" {
		return string( s[i] - 13)
	}
}

return s;

}

@rockswe
Copy link

rockswe commented Dec 29, 2023

package main

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

type rot13Reader struct {
r io.Reader
}

func (rot *rot13Reader) Read(p []byte) (n int, err error) {
n, err = rot.r.Read(p)
for i := 0; i < len(p); i++ {
if p[i] >= 'A' && p[i] < 'Z' {
p[i] = 65 + (((p[i] - 65) + 13) % 26)
} else if p[i] >= 'a' && p[i] <= 'z' {
p[i] = 97 + (((p[i] - 98) + 13) % 26)
}
}
return
}

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

@Atavixion
Copy link

Atavixion commented Jan 7, 2024

package main

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

type rot13Reader struct {
	r io.Reader
}

func (r *rot13Reader) Read(b []byte) (int, error) {
	if n, err := r.r.Read(b); err != nil {
		return n, err
	}
	
	for i, v := range b {
		if v > 64 && v < 91 {
			b[i] = (v - 65 + 13) % 26 + 65
		} else if v > 96 && v < 123 {
			b[i] = (v - 97 + 13) % 26 + 97
		}
	}
	return len(b), nil
}
	
func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@PowerPixel
Copy link

package main

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

type rot13Reader struct {
	r io.Reader
}

func (rot13 rot13Reader) Read(out []byte) (int, error) {
	n, err := rot13.r.Read(out)
	if err != nil {
		return n, err	
	}
	
	for i := 0; i < len(out); i++ {
		if out[i] >= 'A' && out[i] <= 'Z' {
			out[i] = 'A' + (((out[i] - 'A') + 13) % 26)
		}
		if out[i] >= 'a' && out[i] <= 'z' {
			out[i] = 'a' + (((out[i] - 'a') + 13) % 26)
		}
	}
	return n, nil
}

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

I wanted to do compact & efficient :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment