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-2.2.1.180 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.179 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.178 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.177 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.176 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.175 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.174 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.173 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.172 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.171 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.170 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.169 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.167 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.166 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.165 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.164 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.163 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.162 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.161 tracks/go/exercises/atbash-cipher/example.go
trackler-2.2.1.160 tracks/go/exercises/atbash-cipher/example.go