diff --git a/hw02_unpack_string/.sync b/hw02_unpack_string/.sync deleted file mode 100644 index e69de29..0000000 diff --git a/hw02_unpack_string/go.mod b/hw02_unpack_string/go.mod index 32a9588..082c9c5 100644 --- a/hw02_unpack_string/go.mod +++ b/hw02_unpack_string/go.mod @@ -1,10 +1,9 @@ -module github.com/fixme_my_friend/hw02_unpack_string +module github.com/sofiiakulish/hw/tree/hw02_unpack_string go 1.16 require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/stretchr/testify v1.7.0 - gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/hw02_unpack_string/go.sum b/hw02_unpack_string/go.sum index 62f8b7f..c221f64 100644 --- a/hw02_unpack_string/go.sum +++ b/hw02_unpack_string/go.sum @@ -1,20 +1,13 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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.5.0 h1:DMOzIV76tmoDNE9pX6RSN0aDtCYeCg5VueieJaAo1uw= -github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 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= diff --git a/hw02_unpack_string/unpack.go b/hw02_unpack_string/unpack.go index 41f003d..25ae945 100644 --- a/hw02_unpack_string/unpack.go +++ b/hw02_unpack_string/unpack.go @@ -2,11 +2,38 @@ package hw02unpackstring import ( "errors" + "strconv" + "strings" + "unicode" ) var ErrInvalidString = errors.New("invalid string") -func Unpack(_ string) (string, error) { - // Place your code here. - return "", nil +func Unpack(s string) (string, error) { + var b strings.Builder + var last rune + for _, r := range s { + if unicode.IsDigit(r) { + if last == 0 { + return "", ErrInvalidString + } + + counter, _ := strconv.Atoi(string(r)) + if counter > 1 { + b.WriteString(strings.Repeat(string(last), counter-1)) + } else if counter == 0 { + current := b.String() + current = current[:len(current)-len(string(last))] + b.Reset() + b.WriteString(current) + } + + last = 0 + } else { + b.WriteRune(r) + last = r + } + } + + return b.String(), nil } diff --git a/hw02_unpack_string/unpack_test.go b/hw02_unpack_string/unpack_test.go index 9799e18..cea2093 100644 --- a/hw02_unpack_string/unpack_test.go +++ b/hw02_unpack_string/unpack_test.go @@ -16,6 +16,8 @@ func TestUnpack(t *testing.T) { {input: "abccd", expected: "abccd"}, {input: "", expected: ""}, {input: "aaa0b", expected: "aab"}, + {input: "qweЯ0", expected: "qwe"}, + {input: "А2Б3", expected: "ААБББ"}, // uncomment if task with asterisk completed // {input: `qwe\4\5`, expected: `qwe45`}, // {input: `qwe\45`, expected: `qwe44444`},