fix bug in levenshtein distance & add tests
parent
b6a62cf20e
commit
75eebb516f
|
@ -4,6 +4,9 @@ module strings
|
|||
// the distance between between two strings (lower is closer)
|
||||
pub fn levenshtein_distance(a, b string) int {
|
||||
mut f := [0].repeat(b.len + 1)
|
||||
for j in 0..f.len {
|
||||
f[j] = j
|
||||
}
|
||||
for ca in a {
|
||||
mut j := 1
|
||||
mut fj1 := f[0]
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
import strings
|
||||
|
||||
fn test_levenshtein_distance() {
|
||||
assert strings.levenshtein_distance('', '') == 0
|
||||
assert strings.levenshtein_distance('one', 'one') == 0
|
||||
assert strings.levenshtein_distance('', 'two') == 3
|
||||
assert strings.levenshtein_distance('three', '') == 5
|
||||
assert strings.levenshtein_distance('bananna', '') == 7
|
||||
assert strings.levenshtein_distance('cats', 'hats') == 1
|
||||
assert strings.levenshtein_distance('hugs', 'shrugs') == 2
|
||||
assert strings.levenshtein_distance('broom', 'shroom') == 2
|
||||
assert strings.levenshtein_distance('flomax', 'volmax') == 3
|
||||
}
|
Loading…
Reference in New Issue