-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |