Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 218
Good morning! Here's your coding interview problem for today.

This problem was asked by Yahoo.

Write an algorithm that computes the reversal of a directed graph. For example, if a graph consists of `A -> B -> C`, it should become `A <- B <- C`.

Pull request description:
Day218
Resolves #449.

Patch:
diff --git a/README.md b/README.md
index 8e47b1a..5c0e2a2 100644
--- a/README.md
+++ b/README.md
@@ -228,3 +228,4 @@ problems from
 * [Day 215](https://github.com/vaskoz/dailycodingproblem-go/issues/443)
 * [Day 216](https://github.com/vaskoz/dailycodingproblem-go/issues/445)
 * [Day 217](https://github.com/vaskoz/dailycodingproblem-go/issues/447)
+* [Day 218](https://github.com/vaskoz/dailycodingproblem-go/issues/449)
diff --git a/day218/problem.go b/day218/problem.go
new file mode 100644
index 0000000..92186ec
--- /dev/null
+++ b/day218/problem.go
@@ -0,0 +1,20 @@
+package day218
+
+// AdjacencyMatrix is a directed graph adjacency matrix.
+type AdjacencyMatrix map[rune]map[rune]struct{}
+
+// ReverseDirectedGraph reverses all the edges in a given
+// directed graph.
+// Runs in O(N) time and produces a new graph using O(N) space.
+func ReverseDirectedGraph(g AdjacencyMatrix) AdjacencyMatrix {
+	reversed := make(AdjacencyMatrix, len(g))
+	for a := range g {
+		for b := range g[a] {
+			if _, found := reversed[b]; !found {
+				reversed[b] = make(map[rune]struct{})
+			}
+			reversed[b][a] = struct{}{}
+		}
+	}
+	return reversed
+}


Test patch:
diff --git a/day218/problem_test.go b/day218/problem_test.go
new file mode 100644
index 0000000..0f77b9f
--- /dev/null
+++ b/day218/problem_test.go
@@ -0,0 +1,38 @@
+package day218
+
+import (
+	"reflect"
+	"testing"
+)
+
+var testcases = []struct {
+	g, reversed AdjacencyMatrix
+}{
+	{
+		AdjacencyMatrix{
+			'A': {
+				'B': struct{}{},
+			},
+			'B': {
+				'C': struct{}{},
+			},
+		},
+		AdjacencyMatrix{
+			'C': {
+				'B': struct{}{},
+			},
+			'B': {
+				'A': struct{}{},
+			},
+		},
+	},
+}
+
+func TestReverseDirectedGraph(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if reversed := ReverseDirectedGraph(tc.g); !reflect.DeepEqual(reversed, tc.reversed) {
+			t.Errorf("Expected %v, got %v", tc.reversed, reversed)
+		}
+	}
+}