Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 213
Good morning! Here's your coding interview problem for today.

This problem was asked by Snapchat.

Given a string of digits, generate all possible valid IP address combinations.

IP addresses must follow the format A.B.C.D, where A, B, C, and D are numbers between 0 and 255. Zero-prefixed numbers, such as 01 and 065, are not allowed, except for 0 itself.

For example, given "2542540123", you should return ['254.25.40.123', '254.254.0.123'].

Pull request description:
Day213
Resolves #439. 

Patch:
diff --git a/README.md b/README.md
index ff4e2f2..baaae24 100644
--- a/README.md
+++ b/README.md
@@ -223,3 +223,4 @@ problems from
 * [Day 210](https://github.com/vaskoz/dailycodingproblem-go/issues/432)
 * [Day 211](https://github.com/vaskoz/dailycodingproblem-go/issues/434)
 * [Day 212](https://github.com/vaskoz/dailycodingproblem-go/issues/436)
+* [Day 213](https://github.com/vaskoz/dailycodingproblem-go/issues/439)
diff --git a/day213/problem.go b/day213/problem.go
new file mode 100644
index 0000000..63636b0
--- /dev/null
+++ b/day213/problem.go
@@ -0,0 +1,43 @@
+package day213
+
+import (
+	"fmt"
+	"strconv"
+)
+
+// ValidIPAddresses returns all the possible valid
+// IPv4 addresses using all the digits in the string.
+func ValidIPAddresses(digits string) []string {
+	return validIPAddresses(digits, 0)
+}
+
+func validIPAddresses(digits string, octet int) []string {
+	if octet == 4 {
+		if len(digits) == 0 {
+			return []string{""}
+		}
+		return nil
+	}
+	var one, two, three, result []string
+	if len(digits) > 0 {
+		one = validIPAddresses(digits[1:], octet+1)
+	}
+	if len(digits) > 1 && digits[0] != byte('0') {
+		two = validIPAddresses(digits[2:], octet+1)
+	}
+	if len(digits) > 2 && digits[0] != byte('0') {
+		if third, _ := strconv.Atoi(digits[:3]); third < 256 && third > 99 {
+			three = validIPAddresses(digits[3:], octet+1)
+		}
+	}
+	for i, ips := range [][]string{one, two, three} {
+		for _, ip := range ips {
+			if len(ip) != 0 {
+				result = append(result, fmt.Sprintf("%s.%s", digits[:i+1], ip))
+			} else {
+				result = append(result, digits[:i+1])
+			}
+		}
+	}
+	return result
+}


Test patch:
diff --git a/day213/problem_test.go b/day213/problem_test.go
new file mode 100644
index 0000000..a4365bf
--- /dev/null
+++ b/day213/problem_test.go
@@ -0,0 +1,30 @@
+package day213
+
+import (
+	"reflect"
+	"testing"
+)
+
+var testcases = []struct {
+	digits string
+	ips    []string
+}{
+	{"2542540123", []string{"254.25.40.123", "254.254.0.123"}},
+}
+
+func TestValidIPAddresses(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := ValidIPAddresses(tc.digits); !reflect.DeepEqual(tc.ips, result) {
+			t.Errorf("Expected %v, got %v", tc.ips, result)
+		}
+	}
+}
+
+func BenchmarkValidIPAddresses(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			ValidIPAddresses(tc.digits)
+		}
+	}
+}