Skip to content

Commit dbfbe8a

Browse files
authored
feat: sorting exercise (zhravan#138)
1 parent 04e8077 commit dbfbe8a

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

internal/exercises/catalog.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ concepts:
154154
test_regex: ".*"
155155
hints:
156156
- Use `sync.Mutex` to synchronize access to a shared resource.
157+
- slug: 32_sorting
158+
title: Sorting
159+
test_regex: ".*"
160+
hints:
161+
- Use `slice.Sort` for sorting slices.
157162
- slug: 33_string_formatting
158163
title: String Formatting
159164
test_regex: ".*"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sorting
2+
3+
import (
4+
"slices"
5+
)
6+
7+
var Years = []int{2017, 2003, 2026}
8+
var Pets = []string{"Dog", "Cat", "Parrot"}
9+
10+
func SortYears() []int {
11+
slices.Sort(Years)
12+
return Years
13+
}
14+
15+
func SortPets() []string {
16+
slices.Sort(Pets)
17+
return Pets
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package sorting
2+
3+
var Years = []int{2017, 2003, 2026}
4+
var Pets = []string{"Dog", "Cat", "Parrot"}
5+
6+
// TODO: Sort years before returning
7+
func SortYears() []int {
8+
return Years
9+
}
10+
11+
// TODO: Sort pets before returning
12+
func SortPets() []string {
13+
return Pets
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sorting
2+
3+
import (
4+
"slices"
5+
"testing"
6+
)
7+
8+
func TestSorting(t *testing.T) {
9+
t.Run("Sorting numbers", func(t *testing.T) {
10+
SortYears()
11+
if !slices.IsSorted(Years) {
12+
t.Fatal("Years are not sorted!")
13+
}
14+
})
15+
16+
t.Run("Sorting strings", func(t *testing.T) {
17+
SortPets()
18+
if !slices.IsSorted(Pets) {
19+
t.Fatal("Pets are not sorted!")
20+
}
21+
})
22+
}

0 commit comments

Comments
 (0)