diff --git a/04_numeronym/sirigithub/main.go b/04_numeronym/sirigithub/main.go new file mode 100644 index 000000000..86e535aa5 --- /dev/null +++ b/04_numeronym/sirigithub/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "io" + "os" +) + +var out io.Writer = os.Stdout + +func numeronyms(vals ...string) []string { + result := make([]string, len(vals)) + for i, val := range vals { + r := []rune(val) + runeLength := len(r) + if runeLength > 3 { + val = fmt.Sprintf("%c%d%c", r[0], runeLength-2, r[runeLength-1]) + } + result[i] = val + } + return result +} +func main() { + fmt.Fprintln(out, numeronyms("accessibility", "Kubernetes", "abc")) +} diff --git a/04_numeronym/sirigithub/main_test.go b/04_numeronym/sirigithub/main_test.go new file mode 100644 index 000000000..679796548 --- /dev/null +++ b/04_numeronym/sirigithub/main_test.go @@ -0,0 +1,46 @@ +package main + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMainOutput(t *testing.T) { + var buf bytes.Buffer + out = &buf + main() + actual := buf.String() + expected := "[a11y K8s abc]\n" + assert.Equal(t, expected, actual) + +} +func TestNumeronyms(t *testing.T) { + tests := []struct { + description string + input, + expected []string + }{ + {description: "Empty string", input: []string{""}, expected: []string{""}}, + {description: "Short strings", input: []string{"sss", "ab", "a"}, expected: []string{"sss", "ab", "a"}}, + {description: "Alphanumeric string", input: []string{"sss1245sdfg"}, expected: []string{"s9g"}}, + {description: "Back slashes and spaces", input: []string{"sss\\", "A long string with spaces"}, + expected: []string{"s2\\", "A23s"}}, + {description: "Emoji strings", input: []string{"πŸ˜„πŸ·πŸ™ˆπŸ·πŸƒπŸ˜„", "πŸ˜„πŸƒπŸ˜„"}, + expected: []string{"πŸ˜„4πŸ˜„", "πŸ˜„πŸƒπŸ˜„"}}, + {description: "Unicode Strings", input: []string{"ζ—₯本θͺžζ—₯本θͺž", "Γ€ΓΆΓŸβ‚¬β€™ΓΌΓΌΓΌΓΆΓ€ΓŸΓŸ"}, + expected: []string{"ζ—₯4θͺž", "Γ€10ß"}}, + {description: "Strings with special characters", + input: []string{"a##67&a$", "**(]]"}, + expected: []string{"a6$", "*3]"}}, + } + for _, test := range tests { + test := test + t.Run(test.description, func(t *testing.T) { + actual := numeronyms(test.input...) + expected := test.expected + assert.Equal(t, actual, expected, "actual %v but expected %v", actual, expected) + }) + } +}