Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CollinShoop/cd070f261b582b567551aa64862c3e09 to your computer and use it in GitHub Desktop.
Save CollinShoop/cd070f261b582b567551aa64862c3e09 to your computer and use it in GitHub Desktop.
Cracking the Coding Interview: URLIfy (unsolved)
package main
import "fmt"
/**
URLIfy: Write a method to replace all spaces in a string with '%20'. You may assume that the string
has sufficient space at the end to hold the additional characters, and that you are given the "true" length
of the string.
*/
func main() {
cases := []*testCase{
{
input: "",
len: 0,
expected: "",
},
{
input: "Hello World ",
len: 11,
expected: "Hello%20World",
},
{
input: "Mr John Smith ",
len: 13,
expected: "Mr%20John%20Smith",
},
}
for _, c := range cases {
mutatedCopy := []rune(c.input)
URLify(mutatedCopy, c.len)
if string(mutatedCopy) != c.expected {
fmt.Printf("Failed:\n\tExpected %s\n\tActual: '%s'\n", c, string(mutatedCopy))
} else {
fmt.Printf("Passed!\n\t%s\n", c)
}
}
}
type testCase struct {
input string
len int
expected string
}
func (tc *testCase) String() string {
return fmt.Sprintf("URLify('%s', %d)='%s'", tc.input, tc.len, tc.expected)
}
func URLify(input []rune, l int) {
// TODO
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment