Last active
November 21, 2022 16:11
-
-
Save t-eckert/ff9f91c8245042556c31fd8f176ff7a5 to your computer and use it in GitHub Desktop.
Solution to Cassidoo 21 Nov 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
) | |
func main() { | |
in := os.Args[1] | |
formatted := format(in) | |
fmt.Print(formatted) | |
} | |
func format(slashes string) string { | |
formatted := "" | |
indent := 0 | |
for _, slash := range slashes { | |
if slash == '/' { | |
if indent > 0 { | |
indent-- | |
} | |
} | |
formatted += fmt.Sprintf("%s%c\n", strings.Repeat(" ", indent), slash) | |
if slash == '\\' { | |
indent++ | |
} | |
} | |
return formatted | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
func TestFormat(t *testing.T) { | |
cases := []struct { | |
in string | |
expected string | |
}{ | |
{ | |
in: `\\\//\/\\`, | |
expected: "\\\n \\\n \\\n /\n /\n \\\n /\n \\\n \\\n", | |
}, | |
{ | |
in: `\\\\`, | |
expected: "\\\n \\\n \\\n \\\n", | |
}, | |
} | |
for _, tc := range cases { | |
t.Run(tc.in, func(t *testing.T) { | |
actual := format(tc.in) | |
if tc.expected != actual { | |
t.Fail() | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment