1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fix bug in levenshtein distance & add tests

This commit is contained in:
joe-conigliaro 2020-02-04 14:21:40 +11:00 committed by GitHub
parent b6a62cf20e
commit 75eebb516f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -4,6 +4,9 @@ module strings
// the distance between between two strings (lower is closer) // the distance between between two strings (lower is closer)
pub fn levenshtein_distance(a, b string) int { pub fn levenshtein_distance(a, b string) int {
mut f := [0].repeat(b.len + 1) mut f := [0].repeat(b.len + 1)
for j in 0..f.len {
f[j] = j
}
for ca in a { for ca in a {
mut j := 1 mut j := 1
mut fj1 := f[0] mut fj1 := f[0]

View File

@ -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
}