Skip to content

Commit

Permalink
Create a function that sort letters and prints frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
hsy3418 committed Aug 4, 2019
1 parent 7984f47 commit 4f06d71
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 03_letters/hsy3418/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"io"
"os"
"sort"
"strings"
)

var out io.Writer = os.Stdout

//letters is a function returns a mapping of each letter to its frequency.
func letters(s string) map[rune]int {
m := make(map[rune]int)
for _, r := range s {
m[r]++
}
return m
}

//sorLetters is a function returns a sorted slice of strings with elements
func sortLetters(m map[rune]int) []string {
sortSlices := make([]string, 0, len(m))
for key, val := range m {
sortSlices = append(sortSlices, fmt.Sprintf("%c:%d", key, val))
}
sort.Strings(sortSlices)
return sortSlices
}

func main() {
fmt.Fprintln(out, strings.Join(sortLetters(letters("aba")), "\n"))
}
65 changes: 65 additions & 0 deletions 03_letters/hsy3418/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"bytes"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
)

var letterTests = []struct {
input string
want map[rune]int
}{
{input: "abcdefghijk",
want: map[rune]int{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1, 'h': 1, 'i': 1, 'j': 1, 'k': 1}},
{input: "894431kdk",
want: map[rune]int{'8': 1, '9': 1, '4': 2, '3': 1, '1': 1, 'k': 2, 'd': 1}},
{input: "好好学习天天向上",
want: map[rune]int{'好': 2, '学': 1, '习': 1, '天': 2, '向': 1, '上': 1}},
{input: "🏦🔫🗯💰😬🚓🚓🚓🚓",
want: map[rune]int{'🏦': 1, '🔫': 1, '🗯': 1, '💰': 1, '😬': 1, '🚓': 4}},
}

var sortletterTests = []struct {
input map[rune]int
want []string
}{
{input: map[rune]int{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1, 'h': 1, 'i': 1, 'j': 1, 'k': 1},
want: []string{"a:1", "b:1", "c:1", "d:1", "e:1", "f:1", "g:1", "h:1", "i:1", "j:1", "k:1"}},
{input: map[rune]int{'8': 1, '9': 1, '4': 2, '3': 1, '1': 1, 'k': 2, 'd': 1},
want: []string{"1:1", "3:1", "4:2", "8:1", "9:1", "d:1", "k:2"}},
{input: map[rune]int{'好': 2, '学': 1, '习': 1, '天': 2, '向': 1, '上': 1},
want: []string{"上:1", "习:1", "向:1", "天:2", "好:2", "学:1"}},
{input: map[rune]int{'🏦': 1, '🔫': 1, '🗯': 1, '💰': 1, '😬': 1, '🚓': 4},
want: []string{"🏦:1", "💰:1", "🔫:1", "🗯:1", "😬:1", "🚓:4"}},
}

func TestLetters(t *testing.T) {
for _, test := range letterTests {
actual := letters(test.input)
assert.Equal(t, test.want, actual)
}
}

func TestSortLetters(t *testing.T) {
for _, test := range sortletterTests {
actual := sortLetters(test.input)
assert.Equal(t, test.want, actual)
}
}

func TestMainOutput(t *testing.T) {
var buf bytes.Buffer
out = &buf

main()

expected := strconv.Quote("a:2\nb:1\n")
actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("Unexpected output in main()\nexpected: %q\nactual: %q", expected, actual)
}
}

0 comments on commit 4f06d71

Please sign in to comment.