Живко обнови решението на 13.10.2013 14:17 (преди над 4 години)
+package main
+
+var clearPath string
+
+const (
+ unknownDir = iota
+ normalDir
+ oneDot
+ twoDots
+)
+
+var (
+ numberOfDirsToSkip int8
+ directoryDotsUpToNow int8 = unknownDir
+)
+
+func reverseString(str string) (reversedStr string) {
+ for _, character := range str {
+ reversedStr = string(character) + reversedStr
+ }
+ return
+}
+
+func addToClearPath(character rune) {
+ clearPath = string(character) + clearPath
+}
+
+func goBackInClearPath() {
+ numberOfDirsToSkip++
+}
+
+func finalizeDirectory() {
+ addDots()
+ if numberOfDirsToSkip > 0 {
+ numberOfDirsToSkip--
+ } else {
+ addToClearPath('/')
+ }
+ directoryDotsUpToNow = unknownDir
+}
+
+func addDots() {
+ if directoryDotsUpToNow == twoDots {
+ addToClearPath('.')
+ addToClearPath('.')
+ } else if directoryDotsUpToNow == oneDot {
+ addToClearPath('.')
+ }
+}
+
+func getAndRemoveContent(variable *string) string {
+ var_content := *variable
+ *variable = ""
+ return var_content
+}
+
+func newDot(character rune) {
+ switch {
+ case directoryDotsUpToNow == unknownDir:
+ directoryDotsUpToNow = oneDot
+ case directoryDotsUpToNow == normalDir:
+ if numberOfDirsToSkip == 0 {
+ addToClearPath(character)
+ }
+ case directoryDotsUpToNow == oneDot:
+ directoryDotsUpToNow = twoDots
+ case directoryDotsUpToNow == twoDots:
+ addDots()
+ directoryDotsUpToNow = normalDir
+ }
+}
+
+func parsePath(weirdPath string) string {
+ var character rune
+ reversedWeirdPath := reverseString(weirdPath)
+ for _, character = range reversedWeirdPath {
+ if character == '/' {
+ if directoryDotsUpToNow == twoDots {
+ goBackInClearPath()
+ directoryDotsUpToNow = unknownDir
+ } else {
+ finalizeDirectory()
+ }
+ } else if character == '.' {
+ newDot(character)
+ } else {
+ if numberOfDirsToSkip == 0 {
+ if directoryDotsUpToNow != normalDir {
+ addDots()
+ }
+ addToClearPath(character)
+ }
+ directoryDotsUpToNow = normalDir
+ }
+ }
+ if character != '/' && character != '.' {
+ clearPath = "/" + clearPath
+ }
+ if clearPath == "" {
+ clearPath = "/"
+ }
+ numberOfDirsToSkip = 0
+ directoryDotsUpToNow = unknownDir
+ return getAndRemoveContent(&clearPath)
+}
В момента, в който напишеш 105 реда за решение на първо домашно, би трябвало да ти светне лампичка, че вършиш нещо грешно.
Освен това, имплементацията на getAndRemoveContent
ме разби.