uniqueschars #2

Merged
grachevko merged 1 commits from uniqueschars into master 2024-01-29 11:05:07 +00:00
5 changed files with 129 additions and 0 deletions

48
unique-chars/Makefile Normal file
View File

@ -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

10
unique-chars/go.mod Normal file
View File

@ -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
)

9
unique-chars/go.sum Normal file
View File

@ -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=

19
unique-chars/main.go Normal file
View File

@ -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
}

43
unique-chars/main_test.go Normal file
View File

@ -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))
})
}
}