Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab3 #478

Merged
merged 4 commits into from
Jul 24, 2019
Merged

Lab3 #478

Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions 03_letters/nickolee/letter_frequency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

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

var out io.Writer = os.Stdout

func letters(s string) map[rune]int {
count := map[rune]int{}
for _, char := range s {
count[char]++
anzboi marked this conversation as resolved.
Show resolved Hide resolved
}
return count
}

func sortLetters(m map[rune]int) []string {
result := make([]string, len(m))
keys := []int{}

// loop to get keys out of m. Ranging through m to extract keys to fill up keys slice
for char := range m {
keys = append(keys, int(char))
}
sort.Ints(keys)
// building the result []string
for i, key := range keys {
result[i] = fmt.Sprintf("%c:%d", key, m[rune(key)])
// note that Sprintf returns u a formatted string it doesn't print anything
}
return result
}

func main() {
// the strings.Join is to concatenate the new space to every char in s
fmt.Fprintln(out, strings.Join(sortLetters(letters("aba")), "\n"))
}
95 changes: 95 additions & 0 deletions 03_letters/nickolee/letter_frequency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"bytes"
"fmt"
"reflect"
"strconv"
"testing"
)

func TestMainOutput(t *testing.T) {
var buf bytes.Buffer
out = &buf
main()
expected := strconv.Quote(`a:2
b:1
`)
actual := strconv.Quote(buf.String())
if expected != actual {
t.Errorf("Unexpected output in main()\nexpected: %q\nactual: %q", expected, actual)
}
}

func TestLettersFunc(t *testing.T) {
type test struct {
name string
input string
expected map[rune]int
}

tests := []test{
{name: "empty string", input: "", expected: map[rune]int{}}, // expect empty map
{name: "ASCII string", input: " ``~8I,dzye[uY6<mCh<n 9Otefp0fX0-@2<C)z)}.go-Hq{n]LX 8uKnRxj (92@08b9P ",
expected: map[rune]int{
anzboi marked this conversation as resolved.
Show resolved Hide resolved
'@': 2, 'C': 2, 'H': 1, 'I': 1, 'K': 1, 'L': 1, 'O': 1, 'P': 1, 'R': 1, 'X': 2, 'Y': 1, '[': 1,
' ': 6, '(': 1, ')': 2, ',': 1, '-': 2, '.': 1, '0': 3, '2': 2, '6': 1, '8': 3, '9': 3, '<': 3,
'p': 1, 'q': 1, 't': 1, 'u': 2, 'x': 1, 'y': 1, 'z': 2, '{': 1, '}': 1, '~': 1, 'o': 1,
']': 1, '`': 2, 'b': 1, 'd': 1, 'e': 2, 'f': 2, 'g': 1, 'h': 1, 'j': 1, 'm': 1, 'n': 3,
}},
{name: "special characters", input: "无马e😊e马👍👍a👌😒",
expected: map[rune]int{'a': 1, '马': 2, '👍': 2, '👌': 1, 'e': 2, '无': 1, '😊': 1, '😒': 1}},
{name: "correct counts", input: "abbcccddddeeeee", expected: map[rune]int{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}},
}

for _, testCase := range tests {
actual := letters(testCase.input)
fmt.Println(testCase.name, "expected: ", testCase.expected)
fmt.Println(testCase.name, "actual: ", actual)
if !reflect.DeepEqual(testCase.expected, actual) {
t.Fatalf("test case: %s failed. expected: %v, got: %v", testCase.name, testCase.expected, actual)
}
}
}

func TestSortLettersFunction(t *testing.T) {
type test struct {
name string
input map[rune]int
expected []string
}

tests := []test{
{name: "empty map", input: map[rune]int{}, expected: []string{}},
{name: "ASCII map", input: map[rune]int{
'@': 2, 'C': 2, 'H': 1, 'I': 1, 'K': 1, 'L': 1, 'O': 1, 'P': 1, 'R': 1, 'X': 2, 'Y': 1, '[': 1,
' ': 6, '(': 1, ')': 2, ',': 1, '-': 2, '.': 1, '0': 3, '2': 2, '6': 1, '8': 3, '9': 3, '<': 3,
'p': 1, 'q': 1, 't': 1, 'u': 2, 'x': 1, 'y': 1, 'z': 2, '{': 1, '}': 1, '~': 1, 'o': 1,
']': 1, '`': 2, 'b': 1, 'd': 1, 'e': 2, 'f': 2, 'g': 1, 'h': 1, 'j': 1, 'm': 1, 'n': 3,
}, expected: []string{
" :6", "(:1", "):2", ",:1", "-:2", ".:1", "0:3", "2:2", "6:1", "8:3", "9:3", "<:3", "@:2", "C:2",
"H:1", "I:1", "K:1", "L:1", "O:1", "P:1", "R:1", "X:2", "Y:1", "[:1", "]:1", "`:2", "b:1", "d:1",
"e:2", "f:2", "g:1", "h:1", "j:1", "m:1", "n:3", "o:1", "p:1", "q:1", "t:1", "u:2", "x:1", "y:1",
"z:2", "{:1", "}:1", "~:1",
}},
{name: "special characters: Han Script", input: map[rune]int{'⽖': 1, '⽉': 2, '⽏': 3, '⽐': 2, '⽕': 1},
expected: []string{
"⽉:2", "⽏:3", "⽐:2", "⽕:1", "⽖:1",
}},
{name: "special characters: Emojis", input: map[rune]int{'🏦': 1, '🔫': 4, '💰': 1, '🚗': 1, '😬': 1,
'🚓': 7, '😱': 1, '🚒': 1, '🚑': 1, '😨': 1, '😢': 7, '😰': 1,
'🗯': 1, '🏛': 1, '⏸': 1, '🔒': 1, '👮': 1}, expected: []string{
"⏸:1", "🏛:1", "🏦:1", "👮:1", "💰:1", "🔒:1", "🔫:4",
"🗯:1", "😢:7", "😨:1", "😬:1", "😰:1", "😱:1", "🚑:1",
"🚒:1", "🚓:7", "🚗:1"},
}}

for _, testCase := range tests {
actual := sortLetters(testCase.input)
fmt.Println(testCase.name, "expected: ", testCase.expected)
fmt.Println(testCase.name, "actual: ", actual)
if !reflect.DeepEqual(testCase.expected, actual) {
anzboi marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("test case: %s failed. expected: %v, got: %v", testCase.name, testCase.expected, actual)
}
}
}