Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 222
Good morning! Here's your coding interview problem for today.

This problem was asked by Quora.

Given an absolute pathname that may have . or .. as part of it, return the shortest standardized path.

For example, given `"/usr/bin/../bin/./scripts/../"`, return `"/usr/bin/"`.

Pull request description:
Day222
Resolves #458.

Patch:
diff --git a/README.md b/README.md
index 3b06fbe..33dcb98 100644
--- a/README.md
+++ b/README.md
@@ -232,3 +232,4 @@ problems from
 * [Day 219](https://github.com/vaskoz/dailycodingproblem-go/issues/452)
 * [Day 220](https://github.com/vaskoz/dailycodingproblem-go/issues/453)
 * [Day 221](https://github.com/vaskoz/dailycodingproblem-go/issues/454)
+* [Day 222](https://github.com/vaskoz/dailycodingproblem-go/issues/458)
diff --git a/day222/problem.go b/day222/problem.go
new file mode 100644
index 0000000..683b230
--- /dev/null
+++ b/day222/problem.go
@@ -0,0 +1,22 @@
+package day222
+
+import (
+	"fmt"
+	"strings"
+)
+
+// ShortestStandardizedPath returns the shortest standardized path.
+func ShortestStandardizedPath(path string) string {
+	parts := strings.Split(path, "/")
+	var result []string
+	for _, part := range parts {
+		if part == ".." {
+			result = result[:len(result)-1]
+		} else if part == "." {
+			continue
+		} else {
+			result = append(result, part)
+		}
+	}
+	return fmt.Sprintf("%s", strings.Join(result, "/"))
+}


Test patch:
diff --git a/day222/problem_test.go b/day222/problem_test.go
new file mode 100644
index 0000000..dc1eecf
--- /dev/null
+++ b/day222/problem_test.go
@@ -0,0 +1,28 @@
+package day222
+
+import "testing"
+
+var testcases = []struct {
+	path, expected string
+}{
+	{"/usr/bin/../bin/./scripts/../", "/usr/bin/"},
+	{"/usr/bin/../bin/./scripts/../foobar.txt", "/usr/bin/foobar.txt"},
+	{"/usr/bin/../../bin/./scripts/foobar.txt", "/bin/scripts/foobar.txt"},
+}
+
+func TestShortestStandardizedPath(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := ShortestStandardizedPath(tc.path); result != tc.expected {
+			t.Errorf("Expected %v, got %v", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkShortestStandardizedPath(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			ShortestStandardizedPath(tc.path)
+		}
+	}
+}