MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / getKey

Function getKey

cipher/transposition/transposition.go:24–41  ·  view source on GitHub ↗
(keyWord string)

Source from the content-addressed store, hash-verified

22const placeholder = ' '
23
24func getKey(keyWord string) []int {
25 keyWord = strings.ToLower(keyWord)
26 word := []rune(keyWord)
27 var sortedWord = make([]rune, len(word))
28 copy(sortedWord, word)
29 sort.Slice(sortedWord, func(i, j int) bool { return sortedWord[i] < sortedWord[j] })
30 usedLettersMap := make(map[rune]int)
31 wordLength := len(word)
32 resultKey := make([]int, wordLength)
33 for i := 0; i < wordLength; i++ {
34 char := word[i]
35 numberOfUsage := usedLettersMap[char]
36 resultKey[i] = getIndex(sortedWord, char) + numberOfUsage + 1 //+1 -so that indexing does not start at 0
37 numberOfUsage++
38 usedLettersMap[char] = numberOfUsage
39 }
40 return resultKey
41}
42
43func getIndex(wordSet []rune, subString rune) int {
44 n := len(wordSet)

Callers 2

EncryptFunction · 0.85
DecryptFunction · 0.85

Calls 1

getIndexFunction · 0.85

Tested by

no test coverage detected