Sha256: 671c66c1442b75c4bf73b21aae19975ffd2ba792c5661dc465839843d4444eff

Contents?: true

Size: 1.14 KB

Versions: 209

Compression:

Stored size: 1.14 KB

Contents

package atbash

import (
	"regexp"
	"strings"
)

var alphabet = "abcdefghijklmnopqrstuvwxyz"

func Atbash(s string) string {
	return chunk(convert(normalize(s)))
}

func chunk(s string) string {
	reg, _ := regexp.Compile(".{1,5}")
	value := reg.FindAllString(s, -1)
	s = strings.Join(value, " ")
	return s
}

func convert(s string) string {
	key := reverse(alphabet)
	inputSlice := strings.Split(s, "")
	originalSlice := strings.Split(alphabet, "")
	reversedSlice := strings.Split(key, "")
	result := ""
	for i := 0; i < len(s); i++ {
		char := inputSlice[i]
		index := indexOf(originalSlice, char)
		if index > -1 {
			result = result + reversedSlice[index]
		} else {
			result = result + char
		}
	}
	return result
}

func normalize(s string) string {
	s = strings.ToLower(s)
	reg, _ := regexp.Compile("[^a-z0-9]")
	s = reg.ReplaceAllString(s, "")
	return s
}

func reverse(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}

func indexOf(slice []string, string string) int {
	for p, v := range slice {
		if v == string {
			return p
		}
	}
	return -1
}

Version data entries

209 entries across 209 versions & 1 rubygems

Version Path
trackler-1.0.4.0 tracks/go/exercises/atbash-cipher/example.go
trackler-1.0.3.0 tracks/go/exercises/atbash-cipher/example.go
trackler-1.0.2.1 tracks/go/exercises/atbash-cipher/example.go
trackler-1.0.2.0 tracks/go/exercises/atbash-cipher/example.go
trackler-1.0.1.2 tracks/go/exercises/atbash-cipher/example.go
trackler-1.0.1.1 tracks/go/exercises/atbash-cipher/example.go
trackler-1.0.1.0 tracks/go/exercises/atbash-cipher/example.go
trackler-1.0.0.1 tracks/go/exercises/atbash-cipher/example.go
trackler-1.0.0 tracks/go/exercises/atbash-cipher/example.go