From 3dfd240682db3e132c03f321b5ed83e43a42c53f Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Sun, 7 Jun 2020 19:12:00 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=85=D0=BE=D0=B4=D1=8F=D1=82.=20=D0=A2=D0=B5?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B1=D1=8B=20=D0=BB=D0=B8=D0=BD?= =?UTF-8?q?=D1=82=D0=B5=D1=80=D1=8B=20=D0=BF=D0=BE=D0=B1=D0=B5=D0=B4=D0=B8?= =?UTF-8?q?=D1=82=D1=8C..?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw02_unpack_string/go.mod | 2 +- hw02_unpack_string/unpack.go | 54 +++++++++++++++++++++++++++++-- hw02_unpack_string/unpack_test.go | 30 ++++++++++++++++- 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/hw02_unpack_string/go.mod b/hw02_unpack_string/go.mod index 76b8115..7ab51b0 100644 --- a/hw02_unpack_string/go.mod +++ b/hw02_unpack_string/go.mod @@ -1,4 +1,4 @@ -module github.com/fixme_my_friend/hw02_unpack_string +module github.com/tiburon-777/hw02_unpack_string go 1.14 diff --git a/hw02_unpack_string/unpack.go b/hw02_unpack_string/unpack.go index 36c9b60..0b3650f 100644 --- a/hw02_unpack_string/unpack.go +++ b/hw02_unpack_string/unpack.go @@ -2,11 +2,59 @@ package hw02_unpack_string //nolint:golint,stylecheck import ( "errors" + "strconv" + "strings" + "unicode" ) var ErrInvalidString = errors.New("invalid string") -func Unpack(_ string) (string, error) { - // Place your code here - return "", nil +const slash = `\` + +func Unpack(str string) (string, error) { + var b strings.Builder + temp := []rune(str) + for i := 0; i < len(temp); i++ { + if unicode.IsLetter(temp[i]) { + // Ловим паттерн [letter][digit] + if i < len(temp)-1 && unicode.IsDigit(temp[i+1]) { + c, err := strconv.Atoi(string(temp[i+1])) + if err != nil { + return "", err + } + b.WriteString(strings.Repeat(string(temp[i]), c)) + i++ + // Ловим паттерн [letter]... + } else { + b.WriteRune(temp[i]) + } + } else if string(temp[i]) == slash { + // Ловим паттерн [\][letter][digit] + if i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]) { + c, err := strconv.Atoi(string(temp[i+2])) + if err != nil { + return "", err + } + b.WriteString(strings.Repeat(slash+string(temp[i+1]), c)) + i += 2 + // Ловим паттерн [\][digit или \][digit] + } else if i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) && unicode.IsDigit(temp[i+2]) { + c, err := strconv.Atoi(string(temp[i+2])) + if err != nil { + return "", err + } + b.WriteString(strings.Repeat(string(temp[i+1]), c)) + i += 2 + // Ловим паттерн [\][digit или \] + } else if i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) { + b.WriteRune(temp[i+1]) + i++ + } else { + return "", ErrInvalidString + } + } else { + return "", ErrInvalidString + } + } + return b.String(), nil } diff --git a/hw02_unpack_string/unpack_test.go b/hw02_unpack_string/unpack_test.go index c69affe..eb66ba8 100644 --- a/hw02_unpack_string/unpack_test.go +++ b/hw02_unpack_string/unpack_test.go @@ -45,6 +45,29 @@ func TestUnpack(t *testing.T) { input: "aaa0b", expected: "aab", }, + { + input: "fftcwerv ervev sva5scs", + expected: "", + err: ErrInvalidString, + }, + { + input: `||\||(8^`, + expected: "", + err: ErrInvalidString, + }, + { + input: "ис3куРАкс2еу", + expected: "иссскуРАкссеу", + }, + { + input: "рлрплотчрв", + expected: "рлрплотчрв", + }, + { + input: string([]rune{1234, 67, 45, 2324, 899, 353, 2345, 65}), + expected: "", + err: ErrInvalidString, + }, } { result, err := Unpack(tst.input) require.Equal(t, tst.err, err) @@ -53,7 +76,7 @@ func TestUnpack(t *testing.T) { } func TestUnpackWithEscape(t *testing.T) { - t.Skip() // Remove if task with asterisk completed + //t.Skip() // Remove if task with asterisk completed for _, tst := range [...]test{ { @@ -72,6 +95,11 @@ func TestUnpackWithEscape(t *testing.T) { input: `qwe\\\3`, expected: `qwe\3`, }, + { + input: `3\\cds\4`, + expected: ``, + err: ErrInvalidString, + }, } { result, err := Unpack(tst.input) require.Equal(t, tst.err, err) From 74a554bec3cc1828c26bc60f1c91f0dadf0c59be Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Sun, 7 Jun 2020 19:15:47 +0300 Subject: [PATCH 2/7] =?UTF-8?q?gomod=20=D0=BF=D0=BE=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw02_unpack_string/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw02_unpack_string/go.mod b/hw02_unpack_string/go.mod index 7ab51b0..1c9fc9b 100644 --- a/hw02_unpack_string/go.mod +++ b/hw02_unpack_string/go.mod @@ -1,4 +1,4 @@ -module github.com/tiburon-777/hw02_unpack_string +module github.com/tiburon-777/HW_OTUS/hw02_unpack_string go 1.14 From 3f8a699aa493917f16e269dfda3593d33425102e Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Sun, 7 Jun 2020 19:41:24 +0300 Subject: [PATCH 3/7] =?UTF-8?q?=D0=91=D0=BE=D1=80=D0=B5=D0=BC=D1=81=D1=8F?= =?UTF-8?q?=20=D1=81=20=D0=BB=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw02_unpack_string/go.mod | 3 +- hw02_unpack_string/unpack.go | 66 ++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/hw02_unpack_string/go.mod b/hw02_unpack_string/go.mod index 1c9fc9b..5588f22 100644 --- a/hw02_unpack_string/go.mod +++ b/hw02_unpack_string/go.mod @@ -1,5 +1,6 @@ module github.com/tiburon-777/HW_OTUS/hw02_unpack_string + go 1.14 -require github.com/stretchr/testify v1.5.0 +require github.com/stretchr/testify v1.5.0 \ No newline at end of file diff --git a/hw02_unpack_string/unpack.go b/hw02_unpack_string/unpack.go index 0b3650f..f7da01d 100644 --- a/hw02_unpack_string/unpack.go +++ b/hw02_unpack_string/unpack.go @@ -15,44 +15,38 @@ func Unpack(str string) (string, error) { var b strings.Builder temp := []rune(str) for i := 0; i < len(temp); i++ { - if unicode.IsLetter(temp[i]) { - // Ловим паттерн [letter][digit] - if i < len(temp)-1 && unicode.IsDigit(temp[i+1]) { - c, err := strconv.Atoi(string(temp[i+1])) - if err != nil { - return "", err - } - b.WriteString(strings.Repeat(string(temp[i]), c)) - i++ - // Ловим паттерн [letter]... - } else { - b.WriteRune(temp[i]) + switch { + // Ловим паттерн [\][letter][digit] + case string(temp[i]) == slash && i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]): + c, err := strconv.Atoi(string(temp[i+2])) + if err != nil { + return "", err } - } else if string(temp[i]) == slash { - // Ловим паттерн [\][letter][digit] - if i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]) { - c, err := strconv.Atoi(string(temp[i+2])) - if err != nil { - return "", err - } - b.WriteString(strings.Repeat(slash+string(temp[i+1]), c)) - i += 2 - // Ловим паттерн [\][digit или \][digit] - } else if i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) && unicode.IsDigit(temp[i+2]) { - c, err := strconv.Atoi(string(temp[i+2])) - if err != nil { - return "", err - } - b.WriteString(strings.Repeat(string(temp[i+1]), c)) - i += 2 - // Ловим паттерн [\][digit или \] - } else if i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) { - b.WriteRune(temp[i+1]) - i++ - } else { - return "", ErrInvalidString + b.WriteString(strings.Repeat(slash+string(temp[i+1]), c)) + i += 2 + // Ловим паттерн [\][digit или \][digit] + case string(temp[i]) == slash && i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) && unicode.IsDigit(temp[i+2]): + c, err := strconv.Atoi(string(temp[i+2])) + if err != nil { + return "", err } - } else { + b.WriteString(strings.Repeat(string(temp[i+1]), c)) + i += 2 + // Ловим паттерн [\][digit или \] + case string(temp[i]) == slash && i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash): + b.WriteRune(temp[i+1]) + i++ + case unicode.IsLetter(temp[i]) && i < len(temp)-1 && unicode.IsDigit(temp[i+1]): + c, err := strconv.Atoi(string(temp[i+1])) + if err != nil { + return "", err + } + b.WriteString(strings.Repeat(string(temp[i]), c)) + i++ + // Ловим паттерн [letter]... + case unicode.IsLetter(temp[i]): + b.WriteRune(temp[i]) + default: return "", ErrInvalidString } } From e2fd1e06ffd85aa0080ffa342f2e3e6c24b1e710 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Sun, 7 Jun 2020 19:54:00 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=D0=9B=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=8B?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B1=D0=B5=D0=B6=D0=B4=D0=B5=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw02_unpack_string/go.mod | 1 - hw02_unpack_string/unpack.go | 15 +++------------ 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/hw02_unpack_string/go.mod b/hw02_unpack_string/go.mod index 5588f22..01a3879 100644 --- a/hw02_unpack_string/go.mod +++ b/hw02_unpack_string/go.mod @@ -1,6 +1,5 @@ module github.com/tiburon-777/HW_OTUS/hw02_unpack_string - go 1.14 require github.com/stretchr/testify v1.5.0 \ No newline at end of file diff --git a/hw02_unpack_string/unpack.go b/hw02_unpack_string/unpack.go index f7da01d..e5f00cb 100644 --- a/hw02_unpack_string/unpack.go +++ b/hw02_unpack_string/unpack.go @@ -18,18 +18,12 @@ func Unpack(str string) (string, error) { switch { // Ловим паттерн [\][letter][digit] case string(temp[i]) == slash && i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]): - c, err := strconv.Atoi(string(temp[i+2])) - if err != nil { - return "", err - } + c, _ := strconv.Atoi(string(temp[i+2])) b.WriteString(strings.Repeat(slash+string(temp[i+1]), c)) i += 2 // Ловим паттерн [\][digit или \][digit] case string(temp[i]) == slash && i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) && unicode.IsDigit(temp[i+2]): - c, err := strconv.Atoi(string(temp[i+2])) - if err != nil { - return "", err - } + c, _ := strconv.Atoi(string(temp[i+2])) b.WriteString(strings.Repeat(string(temp[i+1]), c)) i += 2 // Ловим паттерн [\][digit или \] @@ -37,10 +31,7 @@ func Unpack(str string) (string, error) { b.WriteRune(temp[i+1]) i++ case unicode.IsLetter(temp[i]) && i < len(temp)-1 && unicode.IsDigit(temp[i+1]): - c, err := strconv.Atoi(string(temp[i+1])) - if err != nil { - return "", err - } + c, _ := strconv.Atoi(string(temp[i+1])) b.WriteString(strings.Repeat(string(temp[i]), c)) i++ // Ловим паттерн [letter]... From cb7d5146c66351143d8b99cd30b6893f5848cc42 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Sun, 7 Jun 2020 20:12:12 +0300 Subject: [PATCH 5/7] =?UTF-8?q?gomod=20=D1=87=D1=83=D0=B4=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw02_unpack_string/go.mod | 2 +- hw02_unpack_string/go.sum | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/hw02_unpack_string/go.mod b/hw02_unpack_string/go.mod index 01a3879..1c9fc9b 100644 --- a/hw02_unpack_string/go.mod +++ b/hw02_unpack_string/go.mod @@ -2,4 +2,4 @@ module github.com/tiburon-777/HW_OTUS/hw02_unpack_string go 1.14 -require github.com/stretchr/testify v1.5.0 \ No newline at end of file +require github.com/stretchr/testify v1.5.0 diff --git a/hw02_unpack_string/go.sum b/hw02_unpack_string/go.sum index 24ccb51..45c4353 100644 --- a/hw02_unpack_string/go.sum +++ b/hw02_unpack_string/go.sum @@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN 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= +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= From dfb22256a1a65f07728f582a2e2cf1c3e2ac2287 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Mon, 8 Jun 2020 11:12:16 +0300 Subject: [PATCH 6/7] HW2 is completed --- hw02_unpack_string/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/hw02_unpack_string/README.md b/hw02_unpack_string/README.md index c035cae..74b43c6 100644 --- a/hw02_unpack_string/README.md +++ b/hw02_unpack_string/README.md @@ -1,4 +1,5 @@ ## Домашнее задание №2 «Распаковка строки» +[![Build Status](https://travis-ci.com/tiburon-777/HW_OTUS.svg?branch=master)](https://travis-ci.com/tiburon-777/HW_OTUS) Необходимо написать Go функцию, осуществляющую примитивную распаковку строки, содержащую повторяющиеся символы/руны, например: From 3af00fd66bff0db353df9e779213383e425af656 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Wed, 10 Jun 2020 15:01:14 +0300 Subject: [PATCH 7/7] HW2 is completed --- hw02_unpack_string/unpack.go | 10 +++++----- hw02_unpack_string/unpack_test.go | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/hw02_unpack_string/unpack.go b/hw02_unpack_string/unpack.go index e5f00cb..3f5ab08 100644 --- a/hw02_unpack_string/unpack.go +++ b/hw02_unpack_string/unpack.go @@ -9,7 +9,7 @@ import ( var ErrInvalidString = errors.New("invalid string") -const slash = `\` +const slash = '\\' func Unpack(str string) (string, error) { var b strings.Builder @@ -17,17 +17,17 @@ func Unpack(str string) (string, error) { for i := 0; i < len(temp); i++ { switch { // Ловим паттерн [\][letter][digit] - case string(temp[i]) == slash && i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]): + case temp[i] == slash && i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]): c, _ := strconv.Atoi(string(temp[i+2])) - b.WriteString(strings.Repeat(slash+string(temp[i+1]), c)) + b.WriteString(strings.Repeat(string(slash)+string(temp[i+1]), c)) i += 2 // Ловим паттерн [\][digit или \][digit] - case string(temp[i]) == slash && i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) && unicode.IsDigit(temp[i+2]): + case temp[i] == slash && i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || temp[i+1] == slash) && unicode.IsDigit(temp[i+2]): c, _ := strconv.Atoi(string(temp[i+2])) b.WriteString(strings.Repeat(string(temp[i+1]), c)) i += 2 // Ловим паттерн [\][digit или \] - case string(temp[i]) == slash && i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash): + case temp[i] == slash && i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || temp[i+1] == slash): b.WriteRune(temp[i+1]) i++ case unicode.IsLetter(temp[i]) && i < len(temp)-1 && unicode.IsDigit(temp[i+1]): diff --git a/hw02_unpack_string/unpack_test.go b/hw02_unpack_string/unpack_test.go index eb66ba8..dfe72a1 100644 --- a/hw02_unpack_string/unpack_test.go +++ b/hw02_unpack_string/unpack_test.go @@ -76,7 +76,6 @@ func TestUnpack(t *testing.T) { } func TestUnpackWithEscape(t *testing.T) { - //t.Skip() // Remove if task with asterisk completed for _, tst := range [...]test{ {