From 9c442d9f248c30aaa17737e5b44db1bb0ee89cd9 Mon Sep 17 00:00:00 2001 From: ailyntang Date: Tue, 27 Aug 2019 11:15:48 +1000 Subject: [PATCH 1/2] Implement letter frequency function for lab3 Includes tests --- 03_letters/ailyntang/letters.go | 33 +++++++++++++++++++ 03_letters/ailyntang/letters_test.go | 49 ++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 03_letters/ailyntang/letters.go create mode 100644 03_letters/ailyntang/letters_test.go diff --git a/03_letters/ailyntang/letters.go b/03_letters/ailyntang/letters.go new file mode 100644 index 000000000..1c431046f --- /dev/null +++ b/03_letters/ailyntang/letters.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "sort" + "strconv" + "strings" + "io" + "os" +) + +var out io.Writer = os.Stdout + +func main() { + fmt.Fprintln(out, strings.Join(sortLetters(letters("aba")), "\n")) +} + +func letters(s string) map[rune]int { + count := make(map[rune]int) + for _, r := range s { + count[r]++ + } + return count +} + +func sortLetters(m map[rune]int) []string { + output := []string{} + for rune, count := range m { + output = append(output, string(rune) + ":" + strconv.Itoa(count)) + } + sort.Strings(output) + return output +} diff --git a/03_letters/ailyntang/letters_test.go b/03_letters/ailyntang/letters_test.go new file mode 100644 index 000000000..e7ffbfe50 --- /dev/null +++ b/03_letters/ailyntang/letters_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestInputsToLetters(t *testing.T) { + assert := assert.New(t) + + assert.Equal(map[rune]int{}, letters(""), "empty string") + assert.Equal(map[rune]int{'F': 1}, letters("F"), "letter") + assert.Equal(map[rune]int{'\n': 1}, letters("\n"), "new line") + assert.Equal(map[rune]int{'*': 1}, letters("*"), "symbol") + assert.Equal(map[rune]int{'✌': 1}, letters("✌"), "emoticon") +} + +func TestOutputToLetters(t *testing.T) { + assert := assert.New(t) + longString := "################################################" + expectedOutputForMixedInput := map[rune]int{'H': 1, 'h': 1, 'e': 4, 'r': 2, ' ': 2, '!': 1, '2': 1} + + assert.Equal(map[rune]int{'#': 48}, letters(longString), "long string") + assert.Equal(expectedOutputForMixedInput, letters("Here here 2!"), "mixed input") +} + +func TestSortLetters(t *testing.T) { + assert := assert.New(t) + + longInput := "5555555555555555bbbbbbbbbbbbbbbbbbb✌✌✌✌✌✌✌✌✌✌✌????????????" + output1 := []string{" :8", "T:1", "a:1", "b:1", "c:1", "d:1", "e:3", "f:1", "g:1", "h:2", "i:1", "j:1", "k:1", "l:1"} + output2 := []string{"m:1", "n:1", "o:4", "p:1", "q:1", "r:2", "s:1", "t:1", "u:2", "v:1", "w:1", "x:1", "y:1", "z:1"} + outputManyChars := append(output1, output2...) + + assert.Equal([]string{"a:1", "b:1", "c:1", "d:1", "e:1"}, sortLetters(letters("edcba")), "unsorted input") + assert.Equal([]string{"5:16", "?:12", "b:19", "✌:11"}, sortLetters(letters(longInput)), "long input") + assert.Equal(outputManyChars, sortLetters(letters("The quick brown fox jumps over the lazy dog")), "many characters") +} + +func TestMainOutput(t *testing.T) { + var buf bytes.Buffer + out = &buf + + main() + + assert.Equal(t, "a:2\nb:1\n", buf.String()) +} From 8f8c4f0e58af71d728442bb835a7d3124d570f5c Mon Sep 17 00:00:00 2001 From: Ai-Lyn Tang Date: Tue, 27 Aug 2019 17:08:01 +1000 Subject: [PATCH 2/2] Update 03_letters/ailyntang/letters.go Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com> --- 03_letters/ailyntang/letters.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/03_letters/ailyntang/letters.go b/03_letters/ailyntang/letters.go index 1c431046f..7a1d7c8e5 100644 --- a/03_letters/ailyntang/letters.go +++ b/03_letters/ailyntang/letters.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "io" + "os" "sort" "strconv" "strings"