Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 362
Good morning! Here's your coding interview problem for today.

This problem was asked by Twitter.

A strobogrammatic number is a positive number that appears the same after being rotated 180 degrees. For example, `16891` is strobogrammatic.

Create a program that finds all strobogrammatic numbers with `N` digits.

Pull request description:
Day362
Resolves #714.

Patch:
diff --git a/README.md b/README.md
index 478ce59..953af36 100644
--- a/README.md
+++ b/README.md
@@ -343,3 +343,4 @@ problems from
 * [Day 359](https://github.com/vaskoz/dailycodingproblem-go/issues/708)
 * [Day 360](https://github.com/vaskoz/dailycodingproblem-go/issues/709)
 * [Day 361](https://github.com/vaskoz/dailycodingproblem-go/issues/712)
+* [Day 362](https://github.com/vaskoz/dailycodingproblem-go/issues/714)
diff --git a/day362/problem.go b/day362/problem.go
new file mode 100644
index 0000000..579ed5f
--- /dev/null
+++ b/day362/problem.go
@@ -0,0 +1,37 @@
+package day362
+
+import (
+	"fmt"
+	"sort"
+)
+
+// StrobogrammaticNumber returns all the strobogrammatic numbers
+// with the given number of digits.
+func StrobogrammaticNumber(digits int) []string {
+	result := strobogrammaticNumber(digits, digits)
+	sort.Strings(result) // for deterministic ordering of results
+	return result
+}
+
+func strobogrammaticNumber(digits, length int) []string {
+	if digits == 0 {
+		return nil
+	} else if digits == 1 {
+		return []string{"0", "1", "8"}
+	}
+	mid := strobogrammaticNumber(digits-2, length)
+	if mid == nil {
+		mid = []string{""}
+	}
+	result := make([]string, 0, len(mid)*5)
+	for _, m := range mid {
+		if digits != length {
+			result = append(result, fmt.Sprintf("0%s0", m))
+		}
+		result = append(result, fmt.Sprintf("1%s1", m))
+		result = append(result, fmt.Sprintf("6%s9", m))
+		result = append(result, fmt.Sprintf("8%s8", m))
+		result = append(result, fmt.Sprintf("9%s6", m))
+	}
+	return result
+}


Test patch:
diff --git a/day362/problem_test.go b/day362/problem_test.go
new file mode 100644
index 0000000..4d3c59e
--- /dev/null
+++ b/day362/problem_test.go
@@ -0,0 +1,36 @@
+package day362
+
+import (
+	"reflect"
+	"testing"
+)
+
+// nolint
+var testcases = []struct {
+	digits   int
+	expected []string
+}{
+	{0, nil},
+	{1, []string{"0", "1", "8"}},
+	{2, []string{"11", "69", "88", "96"}},
+	{3, []string{"101", "111", "181", "609", "619", "689", "808", "818", "888", "906", "916", "986"}},
+	{4, []string{"1001", "1111", "1691", "1881", "1961", "6009", "6119", "6699", "6889", "6969", "8008",
+		"8118", "8698", "8888", "8968", "9006", "9116", "9696", "9886", "9966"}},
+}
+
+func TestStrobogrammaticNumber(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := StrobogrammaticNumber(tc.digits); !reflect.DeepEqual(result, tc.expected) {
+			t.Errorf("Expected %v, got %v", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkStrobogrammaticNumber(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			StrobogrammaticNumber(tc.digits)
+		}
+	}
+}