1
0
Fork 0

Add package lexical

This commit is contained in:
Andinus 2020-04-06 22:10:32 +05:30
parent 2e4e8910eb
commit 0121a30a7d
Signed by: andinus
GPG Key ID: B67D55D482A799FD
2 changed files with 36 additions and 0 deletions

17
lexical/slowsort.go Normal file
View File

@ -0,0 +1,17 @@
package lexical
import (
"sort"
"strings"
)
// SlowSort returns string in lexical order. This function is slower
// than Lexical.
func SlowSort(word string) (sorted string) {
// Convert word to a slice, sort the slice.
t := strings.Split(word, "")
sort.Strings(t)
sorted = strings.Join(t, "")
return
}

19
lexical/sort.go Normal file
View File

@ -0,0 +1,19 @@
package lexical
import "sort"
// Sort takes a string as input and returns the lexical order.
func Sort(word string) (sorted string) {
// Convert the string to []rune.
var r []rune
for _, char := range word {
r = append(r, char)
}
sort.Slice(r, func(i, j int) bool {
return r[i] < r[j]
})
sorted = string(r)
return
}