Skip to content

Commit

Permalink
hw02 основное задание
Browse files Browse the repository at this point in the history
  • Loading branch information
DimVlas authored and DimVlas committed Apr 13, 2024
1 parent 14c3130 commit 48a1e12
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
64 changes: 60 additions & 4 deletions hw02_unpack_string/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,67 @@ package hw02unpackstring

import (
"errors"
"fmt"
"slices"
"strconv"
"strings"
)

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

func Unpack(_ string) (string, error) {
// Place your code here.
return "", nil
nums = []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}

numZero = '0'
)

// True - если руна является цифрой.
func IsDigit(r rune) bool {
return slices.Contains(nums, r)
}

func Unpack(text string) (string, error) {
if text == "" { // с пустой строкой ничего не делаем
return "", nil
}

runes := []rune(text)

if IsDigit(runes[0]) { // если первая руна цифра
return "", ErrInvalidString
}

var res strings.Builder

lenRunes := len(runes)
for i := 0; i < lenRunes; i++ {
if IsDigit(runes[i]) {
return "", ErrInvalidString
}

if i == lenRunes-1 { // это последний символ
res.WriteRune(runes[i])
break
}
if !IsDigit(runes[i+1]) { // следующий символ не цифра
res.WriteRune(runes[i])
continue
}
if IsDigit(runes[i+1]) { // следующий символ цифра
if runes[i+1] == numZero { // следующий символ '0'
i++
continue
}
n, err := strconv.Atoi(string(runes[i+1]))
if err != nil {
return "", fmt.Errorf("error converting rune '%q' to number: %w", runes[i+1], err)
}

res.WriteString(strings.Repeat(string(runes[i]), n))
i++
continue
}
}

return res.String(), nil
}
25 changes: 24 additions & 1 deletion hw02_unpack_string/unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func TestUnpack(t *testing.T) {
expected string
}{
{input: "a4bc2d5e", expected: "aaaabccddddde"},
{input: "a4bc2d5e3", expected: "aaaabccdddddeee"},
{input: "a4bc2d5e0", expected: "aaaabccddddd"},
{input: "a+3b", expected: "a+++b"},
{input: "abccd", expected: "abccd"},
{input: "", expected: ""},
{input: "aaa0b", expected: "aab"},
Expand All @@ -34,7 +37,7 @@ func TestUnpack(t *testing.T) {
}

func TestUnpackInvalidString(t *testing.T) {
invalidStrings := []string{"3abc", "45", "aaa10b"}
invalidStrings := []string{"3abc", "45", "aaa10b", "aaa+b10"}
for _, tc := range invalidStrings {
tc := tc
t.Run(tc, func(t *testing.T) {
Expand All @@ -43,3 +46,23 @@ func TestUnpackInvalidString(t *testing.T) {
})
}
}

func TestIsDigit(t *testing.T) {
tests := []struct {
input rune
expected bool
}{
{input: 'a', expected: false},
{input: '5', expected: true},
{input: '!', expected: false},
{input: '\n', expected: false},
}

for _, tc := range tests {
tc := tc
t.Run(string(tc.input), func(t *testing.T) {
result := IsDigit(tc.input)
require.Equal(t, tc.expected, result)
})
}
}

0 comments on commit 48a1e12

Please sign in to comment.