Skip to content

Commit 92dad01

Browse files
committed
feat: Add run-length encoding code.
1 parent ff10151 commit 92dad01

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ So far, the following exercises have been covered:
1313
- [Heap](./heap/) – implements a heap from scratch, without using the built-in `container/heap` package
1414
- [Radix sort](./radixsort/) – implements radix sort
1515
- [Remove k-th last element](./removekthlastelement/) – removes the k-th last element from a single-linked list
16+
- [Run-length encoding](./runlengthencoding/) – encodes and decodes strings using run-length encoding
1617
- [Running median](./runningmedian/) – calculates the running median of a sequence of numbers
1718
- [Tic tac toe](./tictactoe/) – detects winnning states in a tic tac toe game
1819

runlengthencoding/decode.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package runlengthencoding
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
"unicode"
7+
)
8+
9+
func Decode(input string) string {
10+
var result strings.Builder
11+
var countStr strings.Builder
12+
13+
for _, char := range input {
14+
if unicode.IsDigit(char) {
15+
countStr.WriteRune(char)
16+
} else {
17+
count, _ := strconv.Atoi(countStr.String())
18+
result.WriteString(strings.Repeat(string(char), count))
19+
countStr.Reset()
20+
}
21+
}
22+
23+
return result.String()
24+
}

runlengthencoding/decode_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package runlengthencoding_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/thenativeweb/codingcircle/runlengthencoding"
8+
)
9+
10+
func TestDecode(t *testing.T) {
11+
t.Run("decodes an empty input", func(t *testing.T) {
12+
result := runlengthencoding.Decode("")
13+
assert.Equal(t, "", result)
14+
})
15+
16+
t.Run("decodes a single character", func(t *testing.T) {
17+
result := runlengthencoding.Decode("1A")
18+
assert.Equal(t, "A", result)
19+
})
20+
21+
t.Run("decodes multiple characters", func(t *testing.T) {
22+
result := runlengthencoding.Decode("1A1B1C1D")
23+
assert.Equal(t, "ABCD", result)
24+
})
25+
26+
t.Run("decodes repeated characters", func(t *testing.T) {
27+
result := runlengthencoding.Decode("1A2B3C4D")
28+
assert.Equal(t, "ABBCCCDDDD", result)
29+
})
30+
31+
t.Run("decodes characters repeated more than 9 times", func(t *testing.T) {
32+
result := runlengthencoding.Decode("1A10B3C4D")
33+
assert.Equal(t, "ABBBBBBBBBBCCCDDDD", result)
34+
})
35+
36+
}

runlengthencoding/documentation.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package runlengthencoding encodes and decodes strings using run-length encoding.
2+
package runlengthencoding

runlengthencoding/encode.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package runlengthencoding
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
func Encode(input string) string {
9+
if len(input) == 0 {
10+
return ""
11+
}
12+
13+
var result strings.Builder
14+
15+
count := 1
16+
for i := 1; i < len(input); i++ {
17+
if input[i] == input[i-1] {
18+
count++
19+
} else {
20+
result.WriteString(strconv.Itoa(count))
21+
result.WriteByte(input[i-1])
22+
count = 1
23+
}
24+
}
25+
26+
result.WriteString(strconv.Itoa(count))
27+
result.WriteByte(input[len(input)-1])
28+
29+
return result.String()
30+
}

runlengthencoding/encode_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package runlengthencoding_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/thenativeweb/codingcircle/runlengthencoding"
8+
)
9+
10+
func TestEncode(t *testing.T) {
11+
t.Run("encodes an empty input", func(t *testing.T) {
12+
result := runlengthencoding.Encode("")
13+
assert.Equal(t, "", result)
14+
})
15+
16+
t.Run("encodes a single character", func(t *testing.T) {
17+
result := runlengthencoding.Encode("A")
18+
assert.Equal(t, "1A", result)
19+
})
20+
21+
t.Run("encodes multiple characters", func(t *testing.T) {
22+
result := runlengthencoding.Encode("ABCD")
23+
assert.Equal(t, "1A1B1C1D", result)
24+
})
25+
26+
t.Run("encodes repeated characters", func(t *testing.T) {
27+
result := runlengthencoding.Encode("ABBCCCDDDD")
28+
assert.Equal(t, "1A2B3C4D", result)
29+
})
30+
31+
t.Run("encodes characters repeated more than 9 times", func(t *testing.T) {
32+
result := runlengthencoding.Encode("ABBBBBBBBBBCCCDDDD")
33+
assert.Equal(t, "1A10B3C4D", result)
34+
})
35+
}

0 commit comments

Comments
 (0)