From 1f45f8dea97dc9298d10b08c3e2029af200ea998 Mon Sep 17 00:00:00 2001 From: Konstantin Grachev Date: Sat, 27 Jan 2024 15:34:12 +0300 Subject: [PATCH] uniqueschars: init --- unique-chars/Makefile | 48 +++++++++++++++++++++++++++++++++++++++ unique-chars/go.mod | 10 ++++++++ unique-chars/go.sum | 9 ++++++++ unique-chars/main.go | 19 ++++++++++++++++ unique-chars/main_test.go | 43 +++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 unique-chars/Makefile create mode 100644 unique-chars/go.mod create mode 100644 unique-chars/go.sum create mode 100644 unique-chars/main.go create mode 100644 unique-chars/main_test.go diff --git a/unique-chars/Makefile b/unique-chars/Makefile new file mode 100644 index 0000000..4543fa9 --- /dev/null +++ b/unique-chars/Makefile @@ -0,0 +1,48 @@ +.DEFAULT_GOAL := help + +# ==================================================================================== # +# HELPERS +# ==================================================================================== # + +## help: print this help message +.PHONY: help +help: + @echo 'Usage:' + @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' + + +# ==================================================================================== # +# QUALITY CONTROL +# ==================================================================================== # + +## tidy: format code and tidy modfile +.PHONY: tidy +tidy: + go fmt ./... + go mod tidy -v + +## audit: run quality control checks +.PHONY: audit +audit: + go mod verify + go vet ./... + go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./... + go run golang.org/x/vuln/cmd/govulncheck@latest ./... + go test -race -buildvcs -vet=off ./... + go run github.com/golangci/golangci-lint/cmd/golangci-lint@latest run -v ./... + + +# ==================================================================================== # +# DEVELOPMENT +# ==================================================================================== # + +## test: run all tests +.PHONY: test +test: + go test -v -race -buildvcs ./... + +## test/cover: run all tests and display coverage +.PHONY: test/cover +test/cover: + go test -v -race -buildvcs -coverprofile=/tmp/coverage.out ./... + go tool cover -html=/tmp/coverage.out diff --git a/unique-chars/go.mod b/unique-chars/go.mod new file mode 100644 index 0000000..e0e56d6 --- /dev/null +++ b/unique-chars/go.mod @@ -0,0 +1,10 @@ +module uniqueschars + +go 1.21.6 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/unique-chars/go.sum b/unique-chars/go.sum new file mode 100644 index 0000000..8cf6655 --- /dev/null +++ b/unique-chars/go.sum @@ -0,0 +1,9 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/unique-chars/main.go b/unique-chars/main.go new file mode 100644 index 0000000..6d75a04 --- /dev/null +++ b/unique-chars/main.go @@ -0,0 +1,19 @@ +package uniqueschars + +import "unicode" + +func Unique(s string) bool { + m := make(map[rune]struct{}, 50) + + for _, c := range s { + c := unicode.ToLower(c) + + if _, ok := m[c]; ok { + return false + } + + m[c] = struct{}{} + } + + return true +} diff --git a/unique-chars/main_test.go b/unique-chars/main_test.go new file mode 100644 index 0000000..900cd05 --- /dev/null +++ b/unique-chars/main_test.go @@ -0,0 +1,43 @@ +package uniqueschars + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestUnique(t *testing.T) { + cases := []struct { + Name string + input string + expected bool + }{ + { + "eng alphabet", + "abcdefghijklmnopqrstuvwxyz", + true, + }, + { + "eng mixed case", + "aAbBfF", + false, + }, + { + "cyrillic alphabet", + "абвгдеёжзийклмнопрстуфхцчшщъыьэюя", + true, + }, + { + "cyrillic case mixed", + "аАбБвВ", + false, + }, + } + + for _, tc := range cases { + tc := tc + + t.Run(tc.Name, func(t *testing.T) { + assert.Equal(t, tc.expected, Unique(tc.input)) + }) + } +} -- 2.49.0