From 0121a30a7d0fc67f54dabc56b0b4aa030ac3dac4 Mon Sep 17 00:00:00 2001 From: Andinus Date: Mon, 6 Apr 2020 22:10:32 +0530 Subject: Add package lexical --- lexical/slowsort.go | 17 +++++++++++++++++ lexical/sort.go | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 lexical/slowsort.go create mode 100644 lexical/sort.go diff --git a/lexical/slowsort.go b/lexical/slowsort.go new file mode 100644 index 0000000..79f0e62 --- /dev/null +++ b/lexical/slowsort.go @@ -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 +} diff --git a/lexical/sort.go b/lexical/sort.go new file mode 100644 index 0000000..5ad27f2 --- /dev/null +++ b/lexical/sort.go @@ -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 +} -- cgit 1.4.1-2-gfad0