Skip to content

Commit 1127456

Browse files
committed
Refactored folder and package names, removed main func and added tests.
Signed-off-by: Jobin John <[email protected]>
1 parent 410db77 commit 1127456

File tree

2 files changed

+54
-34
lines changed

2 files changed

+54
-34
lines changed

strings/levenshteinDistance.go renamed to strings/levenshteindistance/levenshteinDistance.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ Parameters: two strings to compare and weights of insertion, substitution and de
44
Output: distance between both strings
55
*/
66

7-
package main
8-
9-
import (
10-
"fmt"
11-
)
7+
package levenshteindistance
128

139
func levenshteinDistance(str1, str2 string, icost, scost, dcost int) int {
1410
row1 := make([]int, len(str2)+1)
1511
row2 := make([]int, len(str2)+1)
16-
var tmp []int
1712

1813
for i := 1; i <= len(str2); i++ {
1914
row1[i] = i * icost
@@ -39,35 +34,8 @@ func levenshteinDistance(str1, str2 string, icost, scost, dcost int) int {
3934
}
4035
}
4136
}
42-
43-
tmp = row1
44-
row1 = row2
45-
row2 = tmp
37+
row1, row2 = row2, row1
4638
}
4739

4840
return row1[len(row1)-1]
4941
}
50-
51-
func min(str1, str2 int) int {
52-
if str1 < str2 {
53-
return str1
54-
}
55-
return str2
56-
}
57-
58-
func main() {
59-
str1 := "stingy"
60-
str2 := "ring"
61-
62-
// Using weight 1 for insertion, substitution and deletion
63-
strDistance1 := levenshteinDistance(str1, str2, 1, 1, 1)
64-
fmt.Printf("Distance between \"%s\" and \"%s\" is: %d.\n", str1, str2, strDistance1)
65-
// Output: Distance between "stingy" and "ring" is: 3.
66-
67-
// Using weight 1 for insertion/substitution and weight 3 for deletion
68-
strDistance2 := levenshteinDistance(str1, str2, 1, 1, 3)
69-
fmt.Printf("Distance between \"%s\" and \"%s\" is: %d.\n", str1, str2, strDistance2)
70-
// Output: Distance between "stingy" and "ring" is: 7.
71-
72-
return
73-
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package levenshteindistance
2+
3+
import "testing"
4+
5+
var testCases = []struct {
6+
name string
7+
string1 string
8+
string2 string
9+
insertionCost int
10+
substitutionCost int
11+
deletionCost int
12+
expected int
13+
}{
14+
{
15+
"strings with equal operation weights.",
16+
"stingy",
17+
"ring",
18+
1,
19+
1,
20+
1,
21+
3,
22+
},
23+
{
24+
"strings with different operation weights.",
25+
"stingy",
26+
"ring",
27+
1,
28+
1,
29+
3,
30+
7,
31+
},
32+
{
33+
"strings with different operation weights.",
34+
"kitten",
35+
"sitting",
36+
1,
37+
1,
38+
1,
39+
3,
40+
},
41+
}
42+
43+
func TestLevenshteinDistance(t *testing.T) {
44+
for _, tc := range testCases {
45+
t.Run(tc.name, func(t *testing.T) {
46+
actual := levenshteinDistance(tc.string1, tc.string2, tc.insertionCost, tc.substitutionCost, tc.deletionCost)
47+
if actual != tc.expected {
48+
t.Errorf("Expected Levenshtein distance between strings: '%s' and '%s' is %v, but got: %v", tc.string1, tc.string2, tc.expected, actual)
49+
}
50+
})
51+
}
52+
}

0 commit comments

Comments
 (0)