Add reversemap task

This commit is contained in:
Fedor Korotkiy 2020-04-15 23:29:34 +03:00
parent bbab13a995
commit 84e79c473c
3 changed files with 77 additions and 0 deletions

6
reversemap/README.md Normal file
View file

@ -0,0 +1,6 @@
# reversemap
Реализуйте функцию reversemap, которая принимает на вход любой `map[K]V`,
и возвращает обращённый `map[V]K`.
Считайте, что все ключи и значения в исходном `map` уникальны.

7
reversemap/reversemap.go Normal file
View file

@ -0,0 +1,7 @@
// +build !solution
package reversemap
func ReverseMap(forward interface{}) interface{} {
panic("implement me")
}

View file

@ -0,0 +1,64 @@
package reversemap
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReverseMap(t *testing.T) {
data := []struct {
forward interface{}
backward interface{}
}{
{
forward: map[string]string{
"k1": "v1",
"k2": "v2",
"k3": "v3",
},
backward: map[string]string{
"v1": "k1",
"v2": "k2",
"v3": "k3",
},
},
{
forward: map[int]string{
1: "v1",
2: "v2",
3: "v3",
},
backward: map[string]int{
"v1": 1,
"v2": 2,
"v3": 3,
},
},
{
forward: map[int]int{
1: 4,
2: 5,
3: 6,
},
backward: map[int]int{
4: 1,
5: 2,
6: 3,
},
},
}
for _, d := range data {
t.Run(reflect.TypeOf(d.forward).String(), func(t *testing.T) {
assert.Equal(t, d.backward, ReverseMap(d.forward))
assert.Equal(t, d.forward, ReverseMap(d.backward))
})
}
}
func TestReverseInt(t *testing.T) {
assert.Panics(t, func() {
ReverseMap(new(int))
})
}