Skip to content

Commit 2b64b21

Browse files
author
openset
committed
Add: Defanging an IP Address
1 parent 5fcca36 commit 2b64b21

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

internal/leetcode/problems_status.go

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ var problemStatus = map[int]bool{
217217
1046: true,
218218
1047: true,
219219
1051: true,
220+
1108: true,
220221
1154: true,
221222
1163: true,
222223
1185: true,
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package defanging_an_ip_address
1+
package problem_1108
22

33
import "strings"
44

55
func defangIPaddr(address string) string {
6-
return strings.Replace(address, ".", "[.]", -1)
6+
return strings.ReplaceAll(address, ".", "[.]")
77
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package problem_1108
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected string
8+
}
9+
10+
func TestDefangIPaddr(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "1.1.1.1",
14+
expected: "1[.]1[.]1[.]1",
15+
},
16+
{
17+
input: "255.100.50.0",
18+
expected: "255[.]100[.]50[.]0",
19+
},
20+
}
21+
for _, tc := range tests {
22+
output := defangIPaddr(tc.input)
23+
if output != tc.expected {
24+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)