Last active
February 13, 2022 22:54
-
-
Save CollinShoop/cd070f261b582b567551aa64862c3e09 to your computer and use it in GitHub Desktop.
Cracking the Coding Interview: URLIfy (unsolved)
This file contains 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" | |
/** | |
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