Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Certainly! Below is a Numba implementation of the Levenshtein distance algorithm that works with two numpy integer arrays. This implementation runs in nopython mode and considers character swaps to have a cost of 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import numpy as np import numba from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) # Create a distance matrix dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize the distance matrix for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j # Fill the distance matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 # Cost of substitution dp[i, j] = min( dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost # Substitution ) # Check for swap (transposition) if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap cost is 2 return dp[len1, len2] # Example usage arr1 = np.array([1, 2, 3, 4], dtype=np.int32) arr2 = np.array([1, 3, 4, 2], dtype=np.int32) print(levenshtein(arr1, arr2)) # Output: 3 |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import numpy as np import numba from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) # Create a distance matrix dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize the distance matrix for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j # Fill the distance matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 # Cost of substitution dp[i, j] = min( dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost # Substitution ) # Check for swap (transposition) if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap cost is 2 return dp[len1, len2] # Example usage arr1 = np.array([1, 2, 3, 4], dtype=np.int32) arr2 = np.array([1, 3, 4, 2], dtype=np.int32) print(levenshtein(arr1, arr2)) # Output: 3 |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import numpy as np import numba from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) # Create a distance matrix dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize the distance matrix for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j # Fill the distance matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 # Cost of substitution dp[i, j] = min( dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost # Substitution ) # Check for swap (transposition) if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap cost is 2 return dp[len1, len2] # Example usage arr1 = np.array([1, 2, 3, 4], dtype=np.int32) arr2 = np.array([1, 3, 4, 2], dtype=np.int32) print(levenshtein(arr1, arr2)) # Output: 3 answer = levenshtein(np.array([3, 5, 2, 4, 8, 7]), np.array([3, 4, 2, 4, 8, 7, 9])) expected = 3 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
2
Traceback (most recent call last):
File "/usr/src/app/main.py", line 52, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got 2 instead of 3
Testing if the previous output contains the string All tests passed
: False
Testing if the previous output contains the string njit
: True
Testing if the previous output contains the string nopython
: False
Testing if the previous output contains the string import numba
: True
Testing if the previous output contains the string from numba
: True
Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Certainly! The Levenshtein distance is a metric for measuring the difference between two sequences, and it can be efficiently implemented using Numba to speed up the computation. Below is a Numba implementation of the Levenshtein distance function, which takes two NumPy integer arrays as input and runs in nopython mode (`@njit`). In this implementation, character swaps are given a cost of 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) # Create a distance matrix dist_matrix = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize the distance matrix for i in range(len1 + 1): dist_matrix[i, 0] = i for j in range(len2 + 1): dist_matrix[0, j] = j # Compute the Levenshtein distance for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dist_matrix[i, j] = min(dist_matrix[i - 1, j] + 1, # Deletion dist_matrix[i, j - 1] + 1, # Insertion dist_matrix[i - 1, j - 1] + cost) # Substitution # Check for swap if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dist_matrix[i, j] = min(dist_matrix[i, j], dist_matrix[i - 2, j - 2] + 2) # Swap cost return dist_matrix[len1, len2] # Example usage if __name__ == "__main__": arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([1, 3, 2, 4]) print(levenshtein(arr1, arr2)) # Output should be 2, accounting for the swap cost |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) # Create a distance matrix dist_matrix = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize the distance matrix for i in range(len1 + 1): dist_matrix[i, 0] = i for j in range(len2 + 1): dist_matrix[0, j] = j # Compute the Levenshtein distance for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dist_matrix[i, j] = min(dist_matrix[i - 1, j] + 1, # Deletion dist_matrix[i, j - 1] + 1, # Insertion dist_matrix[i - 1, j - 1] + cost) # Substitution # Check for swap if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dist_matrix[i, j] = min(dist_matrix[i, j], dist_matrix[i - 2, j - 2] + 2) # Swap cost return dist_matrix[len1, len2] # Example usage if __name__ == "__main__": arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([1, 3, 2, 4]) print(levenshtein(arr1, arr2)) # Output should be 2, accounting for the swap cost |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) # Create a distance matrix dist_matrix = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize the distance matrix for i in range(len1 + 1): dist_matrix[i, 0] = i for j in range(len2 + 1): dist_matrix[0, j] = j # Compute the Levenshtein distance for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dist_matrix[i, j] = min(dist_matrix[i - 1, j] + 1, # Deletion dist_matrix[i, j - 1] + 1, # Insertion dist_matrix[i - 1, j - 1] + cost) # Substitution # Check for swap if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dist_matrix[i, j] = min(dist_matrix[i, j], dist_matrix[i - 2, j - 2] + 2) # Swap cost return dist_matrix[len1, len2] # Example usage if __name__ == "__main__": arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([1, 3, 2, 4]) print(levenshtein(arr1, arr2)) # Output should be 2, accounting for the swap cost answer = levenshtein(np.array([3, 5, 2, 4, 8, 7]), np.array([3, 4, 2, 4, 8, 7, 9])) expected = 3 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
2
Traceback (most recent call last):
File "/usr/src/app/main.py", line 49, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got 2 instead of 3
Testing if the previous output contains the string All tests passed
: False
Testing if the previous output contains the string njit
: True
Testing if the previous output contains the string nopython
: False
Testing if the previous output contains the string import numba
: False
Testing if the previous output contains the string from numba
: True
Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Certainly! To create a Numba-optimized implementation of the Levenshtein distance that works with two NumPy integer arrays and considers character swaps as costing 2, we can proceed as follows: First, ensure you have `Numba` and `NumPy` installed. If you don't have them installed, you can install them using:
1 | pip install numba numpy |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1, len2 = len(arr1), len(arr2) dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dp[i, j] = min(dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost) # Substitution if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap return dp[len1, len2] # Example usage: arr1 = np.array([1, 2, 3, 4], dtype=np.int32) arr2 = np.array([1, 3, 2, 4], dtype=np.int32) distance = levenshtein(arr1, arr2) print(f"The Levenshtein distance is: {distance}") |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1, len2 = len(arr1), len(arr2) dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dp[i, j] = min(dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost) # Substitution if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap return dp[len1, len2] |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1, len2 = len(arr1), len(arr2) dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dp[i, j] = min(dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost) # Substitution if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap return dp[len1, len2] answer = levenshtein(np.array([3, 5, 2, 4, 8, 7]), np.array([3, 4, 2, 4, 8, 7, 9])) expected = 3 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 37, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got 2 instead of 3
Testing if the previous output contains the string All tests passed
: False
Testing if the previous output contains the string njit
: True
Testing if the previous output contains the string nopython
: False
Testing if the previous output contains the string import numba
: False
Testing if the previous output contains the string from numba
: True
Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Certainly! The Levenshtein distance is a measure of the difference between two sequences. To implement this using Numba for optimized performance, we can use the `@njit` decorator to enable nopython mode. In this implementation, we will consider that character swaps have a cost of 2. Here is the Numba implementation of the Levenshtein distance for two numpy integer arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) # Create a distance matrix dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize the distance matrix for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j # Compute the Levenshtein distance with character swap cost 2 for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dp[i, j] = min(dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost) # Substitution # Check for character swap if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap cost return dp[len1, len2] # Example usage arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([1, 3, 4, 2]) distance = levenshtein(arr1, arr2) print(f'Levenshtein distance: {distance}') |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) # Create a distance matrix dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize the distance matrix for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j # Compute the Levenshtein distance with character swap cost 2 for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dp[i, j] = min(dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost) # Substitution # Check for character swap if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap cost return dp[len1, len2] # Example usage arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([1, 3, 4, 2]) distance = levenshtein(arr1, arr2) print(f'Levenshtein distance: {distance}') |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) # Create a distance matrix dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize the distance matrix for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j # Compute the Levenshtein distance with character swap cost 2 for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dp[i, j] = min(dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost) # Substitution # Check for character swap if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap cost return dp[len1, len2] # Example usage arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([1, 3, 4, 2]) distance = levenshtein(arr1, arr2) print(f'Levenshtein distance: {distance}') answer = levenshtein(np.array([3, 5, 2, 4, 8, 7]), np.array([3, 4, 2, 4, 8, 7, 9])) expected = 3 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Levenshtein distance: 2
Traceback (most recent call last):
File "/usr/src/app/main.py", line 49, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got 2 instead of 3
Testing if the previous output contains the string All tests passed
: False
Testing if the previous output contains the string njit
: True
Testing if the previous output contains the string nopython
: False
Testing if the previous output contains the string import numba
: False
Testing if the previous output contains the string from numba
: True
Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Write me a numba implementation of levensthin distance, taking two numpy int arrays. Call it levenshtein. Run it in nopython mode. Character swaps are cost 2.
Certainly! Levenshtein distance is a metric for measuring the difference between two sequences. Here is a Numba implementation of Levenshtein distance for two numpy integer arrays, with a character swap cost of 2. The function will be run in nopython mode for optimal performance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) if len1 == 0: return len2 if len2 == 0: return len1 dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dp[i, j] = min(dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost) # Substitution if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap return dp[len1, len2] # Example usage: arr1 = np.array([1, 2, 3, 4], dtype=np.int32) arr2 = np.array([1, 3, 2, 4], dtype=np.int32) distance = levenshtein(arr1, arr2) print("Levenshtein distance:", distance) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) if len1 == 0: return len2 if len2 == 0: return len1 dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dp[i, j] = min(dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost) # Substitution if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap return dp[len1, len2] # Example usage: arr1 = np.array([1, 2, 3, 4], dtype=np.int32) arr2 = np.array([1, 3, 2, 4], dtype=np.int32) distance = levenshtein(arr1, arr2) print("Levenshtein distance:", distance) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import numpy as np from numba import njit @njit def levenshtein(arr1, arr2): len1 = len(arr1) len2 = len(arr2) if len1 == 0: return len2 if len2 == 0: return len1 dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j for i in range(1, len1 + 1): for j in range(1, len2 + 1): if arr1[i - 1] == arr2[j - 1]: cost = 0 else: cost = 1 dp[i, j] = min(dp[i - 1, j] + 1, # Deletion dp[i, j - 1] + 1, # Insertion dp[i - 1, j - 1] + cost) # Substitution if i > 1 and j > 1 and arr1[i - 1] == arr2[j - 2] and arr1[i - 2] == arr2[j - 1]: dp[i, j] = min(dp[i, j], dp[i - 2, j - 2] + 2) # Swap return dp[len1, len2] # Example usage: arr1 = np.array([1, 2, 3, 4], dtype=np.int32) arr2 = np.array([1, 3, 2, 4], dtype=np.int32) distance = levenshtein(arr1, arr2) print("Levenshtein distance:", distance) answer = levenshtein(np.array([3, 5, 2, 4, 8, 7]), np.array([3, 4, 2, 4, 8, 7, 9])) expected = 3 assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Levenshtein distance: 2
Traceback (most recent call last):
File "/usr/src/app/main.py", line 51, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got 2 instead of 3
Testing if the previous output contains the string All tests passed
: False
Testing if the previous output contains the string njit
: True
Testing if the previous output contains the string nopython
: False
Testing if the previous output contains the string import numba
: False
Testing if the previous output contains the string from numba
: True