Решение на Нормализация на пътища от Александър Иванов

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

Към профила на Александър Иванов

Резултати

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

Код

package main
import "strings"
const (
upperDirectory = ".."
currentDirectory = "."
directoryDelimiter = "/"
)
func parsePath(path string) string {
var (
parts = strings.Split(path, directoryDelimiter)
out = make([]string, len(parts), len(parts))
position = 1
)
for _, part := range parts {
if part == upperDirectory {
position -= 1
} else if part != currentDirectory && part != "" {
out[position] = part
position += 1
}
}
return strings.Join(append(out[:position], ""), directoryDelimiter) //because you can't just fucking concat strings
}

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

PASS
ok  	_/tmp/d20131015-29033-hvaj1o	0.013s
PASS
ok  	_/tmp/d20131015-29033-hvaj1o	0.015s
PASS
ok  	_/tmp/d20131015-29033-hvaj1o	0.014s
--- FAIL: TestBackPath (0.00 seconds)
	solution_test.go:33: Path is not root!
FAIL
exit status 1
FAIL	_/tmp/d20131015-29033-hvaj1o	0.020s
--- FAIL: TestUnevenPath (0.00 seconds)
	solution_test.go:41: Path is not root!
FAIL
exit status 1
FAIL	_/tmp/d20131015-29033-hvaj1o	0.021s
--- FAIL: TestUnorderedEvenPath (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-hvaj1o.parsePath(0x811f968, 0xe, 0x525d5d9f, 0x18)
	/tmp/d20131015-29033-hvaj1o/solution.go:23 +0x260
_/tmp/d20131015-29033-hvaj1o.TestUnorderedEvenPath(0x182561e0)
	/tmp/d20131015-29033-hvaj1o/solution_test.go:48 +0x34
testing.tRunner(0x182561e0, 0x81aaebc)
	/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(0x8139a64, 0x81aae80, 0x8, 0x8, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x8139a64, 0x81aae80, 0x8, 0x8, 0x81ae520, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131015-29033-hvaj1o/_test/_testmain.go:57 +0x81
exit status 2
FAIL	_/tmp/d20131015-29033-hvaj1o	0.031s
PASS
ok  	_/tmp/d20131015-29033-hvaj1o	0.012s
PASS
ok  	_/tmp/d20131015-29033-hvaj1o	0.023s

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

Александър обнови решението на 10.10.2013 19:56 (преди над 4 години)

+package hw0
+
+import "strings"
+
+func parsePath(path string) string {
+
+ var parts = strings.Split(path, "/")
+ var out = make([]string, len(parts), len(parts))
+ var pos = 1
+ for _, part := range parts {
+ if part == ".." {
+ pos -= 1
+ } else if part != "." && part != "" {
+ out[pos] = part
+ pos += 1
+ }
+ //fmt.Println(part, out[:pos])
+ }
+
+ return strings.Join(append(out[:pos], ""), "/") //because you can't just fucking concat strings
+}

Александър обнови решението на 10.10.2013 20:19 (преди над 4 години)

-package hw0
+package main
-import "strings"
+import ("strings"
+ "fmt")
func parsePath(path string) string {
- var parts = strings.Split(path, "/")
+ const upperDirectory = ".."
+ const currentDirectory = "."
+ const directoryDelimiter = "/"
+
+ var parts = strings.Split(path, directoryDelimiter)
var out = make([]string, len(parts), len(parts))
- var pos = 1
+ var position = 1
+
for _, part := range parts {
- if part == ".." {
- pos -= 1
- } else if part != "." && part != "" {
- out[pos] = part
- pos += 1
+ if part == upperDirectory {
+ position -= 1
+ } else if part != currentDirectory && part != "" {
+ out[position] = part
+ position += 1
}
- //fmt.Println(part, out[:pos])
}
- return strings.Join(append(out[:pos], ""), "/") //because you can't just fucking concat strings
+ return strings.Join(append(out[:position], ""), directoryDelimiter) //because you can't just fucking concat strings
+}
+
+func main() {
+ fmt.Println(parsePath("D/go/code/../src/warcluster/tests/first/../../"))
}

Александър обнови решението на 10.10.2013 20:20 (преди над 4 години)

-package main
+package hw0
-import ("strings"
- "fmt")
+import "strings"
func parsePath(path string) string {
const upperDirectory = ".."
const currentDirectory = "."
const directoryDelimiter = "/"
var parts = strings.Split(path, directoryDelimiter)
var out = make([]string, len(parts), len(parts))
var position = 1
for _, part := range parts {
if part == upperDirectory {
position -= 1
} else if part != currentDirectory && part != "" {
out[position] = part
position += 1
}
}
return strings.Join(append(out[:position], ""), directoryDelimiter) //because you can't just fucking concat strings
-}
-
-func main() {
- fmt.Println(parsePath("D/go/code/../src/warcluster/tests/first/../../"))
}

Александър обнови решението на 11.10.2013 00:39 (преди над 4 години)

-package hw0
+package main
import "strings"
func parsePath(path string) string {
const upperDirectory = ".."
const currentDirectory = "."
const directoryDelimiter = "/"
var parts = strings.Split(path, directoryDelimiter)
var out = make([]string, len(parts), len(parts))
var position = 1
for _, part := range parts {
if part == upperDirectory {
position -= 1
} else if part != currentDirectory && part != "" {
out[position] = part
position += 1
}
}
return strings.Join(append(out[:position], ""), directoryDelimiter) //because you can't just fucking concat strings
}

Александър обнови решението на 11.10.2013 12:23 (преди над 4 години)

package main
import "strings"
-func parsePath(path string) string {
- const upperDirectory = ".."
- const currentDirectory = "."
- const directoryDelimiter = "/"
+const (
+ upperDirectory = ".."
+ currentDirectory = "."
+ directoryDelimiter = "/"
+)
- var parts = strings.Split(path, directoryDelimiter)
- var out = make([]string, len(parts), len(parts))
- var position = 1
+func parsePath(path string) string {
+ var (
+ parts = strings.Split(path, directoryDelimiter)
+ out = make([]string, len(parts), len(parts))
+ position = 1
+ )
for _, part := range parts {
if part == upperDirectory {
position -= 1
} else if part != currentDirectory && part != "" {
out[position] = part
position += 1
}
}
return strings.Join(append(out[:position], ""), directoryDelimiter) //because you can't just fucking concat strings
}