From 84e79c473c2a02ab6b22506ede7d00764901eedd Mon Sep 17 00:00:00 2001 From: Fedor Korotkiy Date: Wed, 15 Apr 2020 23:29:34 +0300 Subject: [PATCH] Add reversemap task --- reversemap/README.md | 6 ++++ reversemap/reversemap.go | 7 ++++ reversemap/reversemap_test.go | 64 +++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 reversemap/README.md create mode 100644 reversemap/reversemap.go create mode 100644 reversemap/reversemap_test.go diff --git a/reversemap/README.md b/reversemap/README.md new file mode 100644 index 0000000..583f3ea --- /dev/null +++ b/reversemap/README.md @@ -0,0 +1,6 @@ +# reversemap + +Реализуйте функцию reversemap, которая принимает на вход любой `map[K]V`, +и возвращает обращённый `map[V]K`. + +Считайте, что все ключи и значения в исходном `map` уникальны. diff --git a/reversemap/reversemap.go b/reversemap/reversemap.go new file mode 100644 index 0000000..742844d --- /dev/null +++ b/reversemap/reversemap.go @@ -0,0 +1,7 @@ +// +build !solution + +package reversemap + +func ReverseMap(forward interface{}) interface{} { + panic("implement me") +} diff --git a/reversemap/reversemap_test.go b/reversemap/reversemap_test.go new file mode 100644 index 0000000..95a7df3 --- /dev/null +++ b/reversemap/reversemap_test.go @@ -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)) + }) +}