Sha256: 603509286fe3d557033fe36d19014189726c37969c28f6383a685e5617dee7fc

Contents?: true

Size: 1.16 KB

Versions: 106

Compression:

Stored size: 1.16 KB

Contents

package atbash

import (
	"regexp"
	"strings"
)

const testVersion = 2

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

106 entries across 106 versions & 1 rubygems

Version Path
trackler-2.2.1.36 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.35 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.34 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.33 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.32 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.31 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.30 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.29 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.28 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.27 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.26 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.25 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.24 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.23 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.22 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.21 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.20 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.19 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.18 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.17 tracks/go/exercises/atbash-cipher/example.go