Решение на Нормализация на пътища от Иван Божилов

Обратно към всички решения

Към профила на Иван Божилов

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 5 успешни тест(а)
  • 3 неуспешни тест(а)

Код

package main
import "strings"
const str string = "/.."
func parsePath(path string) string {
var index int
index = strings.Index(path, str)
if index == -1 {
normalize(&path)
return path
}
path = strings.Replace(path, path[findPrevious(path, index):index+3], "", -1)
return parsePath(path)
}
func findPrevious(path string, index int) int {
i := index - 1
for ; i > 0; i-- {
if path[i] == str[0] {
break
}
}
return i
}
func normalize(path *string) {
if (*path)[0] != "/"[0] {
*path = "/" + *path
}
if (*path)[len(*path)-1] != "/"[0] {
*path = *path + "/"
}
}

Лог от изпълнението

PASS
ok  	_/tmp/d20131015-29033-1nbo41m	0.011s
PASS
ok  	_/tmp/d20131015-29033-1nbo41m	0.011s
--- FAIL: TestEmptyPath (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20131015-29033-1nbo41m.parsePath(0x8115e28, 0x0, 0x525d5da3, 0x18)
	/tmp/d20131015-29033-1nbo41m/solution.go:11 +0x10a
_/tmp/d20131015-29033-1nbo41m.TestEmptyPath(0x18255120)
	/tmp/d20131015-29033-1nbo41m/solution_test.go:24 +0x34
testing.tRunner(0x18255120, 0x81aae98)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x8139bc4, 0x81aae80, 0x8, 0x8, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x8139bc4, 0x81aae80, 0x8, 0x8, 0x81ae520, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131015-29033-1nbo41m/_test/_testmain.go:57 +0x81
exit status 2
FAIL	_/tmp/d20131015-29033-1nbo41m	0.012s
--- FAIL: TestBackPath (0.00 seconds)
	solution_test.go:33: Path is not root!
FAIL
exit status 1
FAIL	_/tmp/d20131015-29033-1nbo41m	0.011s
--- FAIL: TestUnevenPath (0.00 seconds)
panic: runtime error: slice bounds out of range [recovered]
	panic: runtime error: slice bounds out of range

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20131015-29033-1nbo41m.parsePath(0x18247270, 0x24, 0x18247210, 0x5)
	/tmp/d20131015-29033-1nbo41m/solution.go:14 +0x1bd
_/tmp/d20131015-29033-1nbo41m.parsePath(0x18247270, 0x24, 0x18250202, 0x7)
	/tmp/d20131015-29033-1nbo41m/solution.go:15 +0x1a4
_/tmp/d20131015-29033-1nbo41m.parsePath(0x18247210, 0x29, 0x81383ca, 0x8)
	/tmp/d20131015-29033-1nbo41m/solution.go:15 +0x1a4
_/tmp/d20131015-29033-1nbo41m.parsePath(0x18250200, 0x30, 0x525d5da5, 0x18)
	/tmp/d20131015-29033-1nbo41m/solution.go:15 +0x1a4
_/tmp/d20131015-29033-1nbo41m.TestUnevenPath(0x18255120)
	/tmp/d20131015-29033-1nbo41m/solution_test.go:40 +0x34
testing.tRunner(0x18255120, 0x81aaeb0)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x8139bc4, 0x81aae80, 0x8, 0x8, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x8139bc4, 0x81aae80, 0x8, 0x8, 0x81ae520, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131015-29033-1nbo41m/_test/_testmain.go:57 +0x81
exit status 2
FAIL	_/tmp/d20131015-29033-1nbo41m	0.011s
PASS
ok  	_/tmp/d20131015-29033-1nbo41m	0.011s
PASS
ok  	_/tmp/d20131015-29033-1nbo41m	0.012s
PASS
ok  	_/tmp/d20131015-29033-1nbo41m	0.011s

История (1 версия и 1 коментар)

Иван обнови решението на 11.10.2013 12:35 (преди над 4 години)

+package main
+
+import "strings"
+
+const str string = "/.."
+
+func parsePath(path string) string {
+ var index int
+ index = strings.Index(path, str)
+ if index == -1 {
+ normalize(&path)
+ return path
+ }
+ path = strings.Replace(path, path[findPrevious(path, index):index+3], "", -1)
+ return parsePath(path)
+}
+
+func findPrevious(path string, index int) int {
+ i := index - 1
+ for ; i > 0; i-- {
+ if path[i] == str[0] {
+ break
+ }
+ }
+ return i
+}
+
+func normalize(path *string) {
+ if (*path)[0] != "/"[0] {
+ *path = "/" + *path
+ }
+ if (*path)[len(*path)-1] != "/"[0] {
+ *path = *path + "/"
+ }
+}