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

HW2 is completed #2

Merged
merged 7 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ jobs:
run: echo "BRANCH=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ~1.22

- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Linters
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v4
with:
version: v1.57.2
working-directory: ${{ env.BRANCH }}
Expand All @@ -38,12 +38,12 @@ jobs:
run: echo "BRANCH=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ^1.22

- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Unit tests
run: go test -v -count=1 -race -timeout=1m ./...
Expand All @@ -65,12 +65,12 @@ jobs:
if: contains(github.ref, 'calendar')
steps:
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ^1.22

- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: make lint
run: make lint
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ issues:
- dupl
- gocyclo
- gosec
- depguard

linters:
disable-all: true
Expand Down
Empty file removed hw02_unpack_string/.sync
Empty file.
4 changes: 2 additions & 2 deletions hw02_unpack_string/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module github.com/fixme_my_friend/hw02_unpack_string

go 1.22

require github.com/stretchr/testify v1.7.0
require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 4 additions & 7 deletions hw02_unpack_string/go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
43 changes: 40 additions & 3 deletions hw02_unpack_string/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,48 @@ package hw02unpackstring

import (
"errors"
"strconv"
"strings"
)

var ErrInvalidString = errors.New("invalid string")

func Unpack(_ string) (string, error) {
// Place your code here.
return "", nil
func Unpack(s string) (string, error) {
var result strings.Builder
k := false

for i, j := 0, 1; i < len(s); i, j = i+1, j+1 {
if !k {
_, err := strconv.Atoi(string(s[i]))
if err == nil {
return "", ErrInvalidString
}
}

b := string(s[i])

if b == "\\" && !k {
k = true
continue
}

if j < len(s) {
num, err := strconv.Atoi(string(s[j]))

if err == nil {
repstr := strings.Repeat(b, num)
result.WriteString(repstr)
i++
j++
} else {
result.WriteString(string(s[i]))
}
} else {
result.WriteString(string(s[i]))
}

k = false
}

return result.String(), nil
}
14 changes: 9 additions & 5 deletions hw02_unpack_string/unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ func TestUnpack(t *testing.T) {
{input: "abccd", expected: "abccd"},
{input: "", expected: ""},
{input: "aaa0b", expected: "aab"},
{input: "a0", expected: ""},
// uncomment if task with asterisk completed
// {input: `qwe\4\5`, expected: `qwe45`},
// {input: `qwe\45`, expected: `qwe44444`},
// {input: `qwe\\5`, expected: `qwe\\\\\`},
// {input: `qwe\\\3`, expected: `qwe\3`},
{input: `qwe\4\5`, expected: `qwe45`},
{input: `qwe\45`, expected: `qwe44444`},
{input: `qwe\\5`, expected: `qwe\\\\\`},
{input: `qwe\\\3`, expected: `qwe\3`},
{input: `a\00`, expected: `a`},
{input: `\0\\\`, expected: `0\`},
{input: `\0\\0`, expected: `0`},
}

for _, tc := range tests {
Expand All @@ -34,7 +38,7 @@ func TestUnpack(t *testing.T) {
}

func TestUnpackInvalidString(t *testing.T) {
invalidStrings := []string{"3abc", "45", "aaa10b"}
invalidStrings := []string{"3abc", "45", "aaa10b", "a0a0a01"}
for _, tc := range invalidStrings {
tc := tc
t.Run(tc, func(t *testing.T) {
Expand Down
Loading