Skip to content

Instantly share code, notes, and snippets.

@t-eckert
Last active November 21, 2022 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-eckert/ff9f91c8245042556c31fd8f176ff7a5 to your computer and use it in GitHub Desktop.
Save t-eckert/ff9f91c8245042556c31fd8f176ff7a5 to your computer and use it in GitHub Desktop.
Solution to Cassidoo 21 Nov 2022
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
}
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