Skip to content

Commit 77c6878

Browse files
committed
Compression
1 parent e626798 commit 77c6878

20 files changed

+73752
-149
lines changed

.github/workflows/lint-test.yaml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Lint and Test
2+
3+
on: push
4+
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
- name: Setup Go
12+
uses: actions/setup-go@v5
13+
with:
14+
go-version: "1.21.x"
15+
- name: Install dependencies
16+
run: go get .
17+
- name: Install linters
18+
run: |
19+
go install honnef.co/go/tools/cmd/staticcheck@latest
20+
go install mvdan.cc/unparam@latest
21+
- name: go vet
22+
run: go vet ${{ inputs.path }}
23+
- name: staticcheck
24+
run: staticcheck ${{ inputs.path }}
25+
- name: unparam
26+
run: unparam ${{ inputs.path }}
27+
28+
test:
29+
runs-on: ubuntu-latest
30+
31+
steps:
32+
- uses: actions/checkout@v4
33+
- name: Setup Go
34+
uses: actions/setup-go@v5
35+
with:
36+
go-version: "1.21.x"
37+
- name: Install dependencies
38+
run: go get .
39+
- name: Run Tests
40+
run: go test -v ./...

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- [x] ([@shogg](https://github.com/shogg)) [brokennode](https://github.com/plutov/practice-go/tree/master/brokennode)
3535
- [x] ([@shogg](https://github.com/shogg)) [nasacollage](https://github.com/plutov/practice-go/tree/master/nasacollage)
3636
- [x] ([@shogg](https://github.com/shogg)) [node_degree](https://github.com/plutov/practice-go/tree/master/node_degree)
37+
- [ ] [compression](https://github.com/plutov/practice-go/tree/master/compression)
3738

3839
### Run tests with benchmarks
3940

anagram/anagram.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func (r MyRune) Less(i, j int) bool { return r[i] < r[j] }
1414

1515
func Normalize(s string) string {
1616
var r MyRune
17-
for _, b := range []rune(s) {
17+
for _, b := range s {
1818
b = unicode.ToLower(b)
1919
if b >= 'a' && b <= 'z' {
2020
r = append(r, b)
@@ -32,7 +32,7 @@ func FindAnagrams(dictionary []string, word string) (result []string) {
3232
var res []string
3333
for _, s := range dictionary {
3434
n2 := Normalize(s)
35-
if n == n2 && strings.ToLower(s) != strings.ToLower(word) {
35+
if n == n2 && !strings.EqualFold(s, word) {
3636
res = append(res, s)
3737
}
3838
}

anagram/anagram_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package anagram
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"os"
66
"strings"
77
"testing"
88
)
@@ -25,7 +25,7 @@ var tests = []struct {
2525
}
2626

2727
func init() {
28-
content, err := ioutil.ReadFile("dictionary.txt")
28+
content, err := os.ReadFile("dictionary.txt")
2929
if err != nil {
3030
panic(err)
3131
}

chess/chess.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ func CanKnightAttack(white, black string) (bool, error) {
1111

1212
// Check if pieces are on board
1313
if white[0]-'a' > 7 || white[1]-'1' > 7 {
14-
return false, errors.New("Invalid white position")
14+
return false, errors.New("invalid white position")
1515
}
1616
if black[0]-'a' > 7 || black[1]-'1' > 7 {
17-
return false, errors.New("Invalid black position")
17+
return false, errors.New("invalid black position")
1818
}
1919

2020
// Calculate distance between knights in each axis
@@ -25,7 +25,7 @@ func CanKnightAttack(white, black string) (bool, error) {
2525
if (d0 == 1 && d1 == 2) || (d0 == 2 && d1 == 1) {
2626
return true, nil
2727
} else if d0 == 0 && d1 == 0 {
28-
return false, errors.New("You can't stack 'em you silly.")
28+
return false, errors.New("you cannot stack them you silly")
2929
}
3030
return false, nil
3131
}

compression/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### Build a compression
2+
3+
This challenge focuses on creating a simple lossless compression tool from scratch. You'll need to implement an algorithm to achieve compression without relying on external libraries, you can use a known algorithm though (for example Huffman). The goal is to balance compression ratio (how much smaller the compressed file is compared to the original) and compression/decompression speed.
4+
5+
This challenge will help you understand the concepts of data compression, explore different algorithms, and practice efficient data structure design. You'll also gain experience in balancing performance and resource usage.
6+
7+
### Sample data set
8+
9+
This challenge includes a sample `dataset.txt` (3.2MB) file which will be used in benchmarking.
10+
11+
### Functions
12+
13+
- `Encode(s string) string`
14+
- `Decode(s string) string`
15+
16+
### Run tests with benchmarks
17+
18+
```
19+
go test -bench .
20+
```

compression/compression.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package compression
2+
3+
func Encode(s string) string {
4+
return s
5+
}
6+
7+
func Decode(s string) string {
8+
return s
9+
}

compression/compression_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package compression
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
)
8+
9+
var (
10+
originalContent string
11+
originalLength int64
12+
)
13+
14+
func init() {
15+
code, _ := os.ReadFile("dataset.txt")
16+
originalContent = string(code)
17+
18+
originalLength = int64(len(originalContent))
19+
}
20+
21+
func TestEncodeDecode(t *testing.T) {
22+
encoded := Encode(originalContent)
23+
decoded := Decode(encoded)
24+
25+
if originalContent != decoded {
26+
t.Errorf("Decode(Encode(originalContent)) = %s, want %s", decoded, originalContent)
27+
}
28+
29+
// print compression ratio
30+
encodedLength := int64(len(encoded))
31+
fmt.Printf("\nOriginal length: %d, Encoded length: %d, Compression ratio: %.2f\n\n", originalLength, encodedLength, float64(encodedLength)/float64(originalLength))
32+
}
33+
34+
func BenchmarkEncodeDecode(b *testing.B) {
35+
for i := 0; i < b.N; i++ {
36+
encoded := Encode(originalContent)
37+
Decode(encoded)
38+
}
39+
}

0 commit comments

Comments
 (0)