Refactor Levenshtein distance implementation (#5138)

* ref: refactor Levenshtein distance implementation
- Rewrite the original levenshtein distance implementation in functional style
- Add optimized version of levenshtein distance

* ref: make `LevenshteinDistance` class a proper utility

* ref: remove duplicated test data

* ref: update tests

---

Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
SOZEL
2024-05-04 16:13:30 +07:00
committed by GitHub
parent b3903f5768
commit dda3c9cb59
2 changed files with 107 additions and 43 deletions

View File

@ -2,15 +2,44 @@ package com.thealgorithms.dynamicprogramming;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.function.ToIntBiFunction;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class LevenshteinDistanceTests {
@ParameterizedTest
@CsvSource({"dog,cat,3", "sunday,saturday,3", "cat,cats,1", "rain,train,1"})
void levenshteinDistanceTest(String str1, String str2, int distance) {
int result = LevenshteinDistance.calculateLevenshteinDistance(str1, str2);
assertEquals(distance, result);
@MethodSource("testCases")
public void testLevenshteinDistance(final int expected, final String str1, final String str2, final ToIntBiFunction<String, String> dist) {
assertEquals(expected, dist.applyAsInt(str1, str2));
assertEquals(expected, dist.applyAsInt(str2, str1));
assertEquals(0, dist.applyAsInt(str1, str1));
assertEquals(0, dist.applyAsInt(str2, str2));
}
private static Stream<Arguments> testCases() {
final Object[][] testData = {
{0, "", ""},
{0, "Hello, World!", "Hello, World!"},
{4, "", "Rust"},
{3, "horse", "ros"},
{6, "tan", "elephant"},
{8, "execute", "intention"},
{1, "a", "b"},
{1, "a", "aa"},
{1, "a", ""},
{1, "a", "ab"},
{1, "a", "ba"},
{2, "a", "bc"},
{2, "a", "cb"},
};
final List<ToIntBiFunction<String, String>> methods = Arrays.asList(LevenshteinDistance::naiveLevenshteinDistance, LevenshteinDistance::optimizedLevenshteinDistance);
return Stream.of(testData).flatMap(input -> methods.stream().map(method -> Arguments.of(input[0], input[1], input[2], method)));
}
}