Skip to content

Commit

Permalink
feat: Add run-length encoding code.
Browse files Browse the repository at this point in the history
  • Loading branch information
goloroden committed Feb 18, 2024
1 parent ff10151 commit 92dad01
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ So far, the following exercises have been covered:
- [Heap](./heap/) – implements a heap from scratch, without using the built-in `container/heap` package
- [Radix sort](./radixsort/) – implements radix sort
- [Remove k-th last element](./removekthlastelement/) – removes the k-th last element from a single-linked list
- [Run-length encoding](./runlengthencoding/) – encodes and decodes strings using run-length encoding
- [Running median](./runningmedian/) – calculates the running median of a sequence of numbers
- [Tic tac toe](./tictactoe/) – detects winnning states in a tic tac toe game

Expand Down
24 changes: 24 additions & 0 deletions runlengthencoding/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package runlengthencoding

import (
"strconv"
"strings"
"unicode"
)

func Decode(input string) string {
var result strings.Builder
var countStr strings.Builder

for _, char := range input {
if unicode.IsDigit(char) {
countStr.WriteRune(char)
} else {
count, _ := strconv.Atoi(countStr.String())
result.WriteString(strings.Repeat(string(char), count))
countStr.Reset()
}
}

return result.String()
}
36 changes: 36 additions & 0 deletions runlengthencoding/decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package runlengthencoding_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/thenativeweb/codingcircle/runlengthencoding"
)

func TestDecode(t *testing.T) {
t.Run("decodes an empty input", func(t *testing.T) {
result := runlengthencoding.Decode("")
assert.Equal(t, "", result)
})

t.Run("decodes a single character", func(t *testing.T) {
result := runlengthencoding.Decode("1A")
assert.Equal(t, "A", result)
})

t.Run("decodes multiple characters", func(t *testing.T) {
result := runlengthencoding.Decode("1A1B1C1D")
assert.Equal(t, "ABCD", result)
})

t.Run("decodes repeated characters", func(t *testing.T) {
result := runlengthencoding.Decode("1A2B3C4D")
assert.Equal(t, "ABBCCCDDDD", result)
})

t.Run("decodes characters repeated more than 9 times", func(t *testing.T) {
result := runlengthencoding.Decode("1A10B3C4D")
assert.Equal(t, "ABBBBBBBBBBCCCDDDD", result)
})

}
2 changes: 2 additions & 0 deletions runlengthencoding/documentation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package runlengthencoding encodes and decodes strings using run-length encoding.
package runlengthencoding
30 changes: 30 additions & 0 deletions runlengthencoding/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package runlengthencoding

import (
"strconv"
"strings"
)

func Encode(input string) string {
if len(input) == 0 {
return ""
}

var result strings.Builder

count := 1
for i := 1; i < len(input); i++ {
if input[i] == input[i-1] {
count++
} else {
result.WriteString(strconv.Itoa(count))
result.WriteByte(input[i-1])
count = 1
}
}

result.WriteString(strconv.Itoa(count))
result.WriteByte(input[len(input)-1])

return result.String()
}
35 changes: 35 additions & 0 deletions runlengthencoding/encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package runlengthencoding_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/thenativeweb/codingcircle/runlengthencoding"
)

func TestEncode(t *testing.T) {
t.Run("encodes an empty input", func(t *testing.T) {
result := runlengthencoding.Encode("")
assert.Equal(t, "", result)
})

t.Run("encodes a single character", func(t *testing.T) {
result := runlengthencoding.Encode("A")
assert.Equal(t, "1A", result)
})

t.Run("encodes multiple characters", func(t *testing.T) {
result := runlengthencoding.Encode("ABCD")
assert.Equal(t, "1A1B1C1D", result)
})

t.Run("encodes repeated characters", func(t *testing.T) {
result := runlengthencoding.Encode("ABBCCCDDDD")
assert.Equal(t, "1A2B3C4D", result)
})

t.Run("encodes characters repeated more than 9 times", func(t *testing.T) {
result := runlengthencoding.Encode("ABBBBBBBBBBCCCDDDD")
assert.Equal(t, "1A10B3C4D", result)
})
}

0 comments on commit 92dad01

Please sign in to comment.