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.
Here's a Numba implementation of the Levenshtein distance algorithm that takes two numpy integer arrays and treats character swaps with 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 | import numpy as np from numba import njit @njit(nogil=True) def levenshtein(seq1, seq2): m, n = len(seq1), len(seq2) # Create distance matrix d = np.zeros((m + 1, n + 1), dtype=np.int32) # Initialize first row and column for i in range(m + 1): d[i, 0] = i for j in range(n + 1): d[0, j] = j # Fill in the rest of the matrix for i in range(1, m + 1): for j in range(1, n + 1): if seq1[i-1] == seq2[j-1]: substitution_cost = 0 else: # Check if it's a swap if (i > 1 and j > 1 and seq1[i-1] == seq2[j-2] and seq1[i-2] == seq2[j-1]): substitution_cost = 2 # Swap cost else: substitution_cost = 1 # Regular substitution # Calculate minimum cost operation deletion = d[i-1, j] + 1 insertion = d[i, j-1] + 1 substitution = d[i-1, j-1] + substitution_cost # Take the minimum of the three operations d[i, j] = min(deletion, insertion, substitution) return d[m, n] |
1 2 3 4 5 6 | # Example usage seq1 = np.array([1, 2, 3, 4], dtype=np.int32) seq2 = np.array([1, 2, 4, 3], dtype=np.int32) distance = levenshtein(seq1, seq2) print(f"Levenshtein distance: {distance}") |
1 | d = np.zeros((m + 1, n + 1), dtype=np.int64) |
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 | import numpy as np from numba import njit @njit(nogil=True) def levenshtein(seq1, seq2): m, n = len(seq1), len(seq2) # Create distance matrix d = np.zeros((m + 1, n + 1), dtype=np.int32) # Initialize first row and column for i in range(m + 1): d[i, 0] = i for j in range(n + 1): d[0, j] = j # Fill in the rest of the matrix for i in range(1, m + 1): for j in range(1, n + 1): if seq1[i-1] == seq2[j-1]: substitution_cost = 0 else: # Check if it's a swap if (i > 1 and j > 1 and seq1[i-1] == seq2[j-2] and seq1[i-2] == seq2[j-1]): substitution_cost = 2 # Swap cost else: substitution_cost = 1 # Regular substitution # Calculate minimum cost operation deletion = d[i-1, j] + 1 insertion = d[i, j-1] + 1 substitution = d[i-1, j-1] + substitution_cost # Take the minimum of the three operations d[i, j] = min(deletion, insertion, substitution) return d[m, n] |
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 | import numpy as np from numba import njit @njit(nogil=True) def levenshtein(seq1, seq2): m, n = len(seq1), len(seq2) # Create distance matrix d = np.zeros((m + 1, n + 1), dtype=np.int32) # Initialize first row and column for i in range(m + 1): d[i, 0] = i for j in range(n + 1): d[0, j] = j # Fill in the rest of the matrix for i in range(1, m + 1): for j in range(1, n + 1): if seq1[i-1] == seq2[j-1]: substitution_cost = 0 else: # Check if it's a swap if (i > 1 and j > 1 and seq1[i-1] == seq2[j-2] and seq1[i-2] == seq2[j-1]): substitution_cost = 2 # Swap cost else: substitution_cost = 1 # Regular substitution # Calculate minimum cost operation deletion = d[i-1, j] + 1 insertion = d[i, j-1] + 1 substitution = d[i-1, j-1] + substitution_cost # Take the minimum of the three operations d[i, j] = min(deletion, insertion, substitution) return d[m, n] 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 47, 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.
Here's a Numba implementation of the Levenshtein distance algorithm with character swaps costing 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 numba import numpy as np @numba.jit(nopython=True) def levenshtein(s1, s2): # Get lengths of both sequences len1 = len(s1) len2 = len(s2) # Create matrix of size (len1+1) x (len2+1) matrix = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize first row and column for i in range(len1 + 1): matrix[i, 0] = i for j in range(len2 + 1): matrix[0, j] = j # Fill in the rest of the matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): if s1[i-1] == s2[j-1]: cost = 0 else: cost = 1 # Regular Levenshtein operations (insertion, deletion, substitution) matrix[i, j] = min( matrix[i-1, j] + 1, # deletion matrix[i, j-1] + 1, # insertion matrix[i-1, j-1] + cost # substitution ) # Handle transposition (swap) if possible if (i > 1 and j > 1 and s1[i-1] == s2[j-2] and s1[i-2] == s2[j-1]): matrix[i, j] = min( matrix[i, j], matrix[i-2, j-2] + 2 # swap cost is 2 ) return matrix[len1, len2] |
1 2 3 4 5 6 | # Example usage s1 = np.array([1, 2, 3, 4]) s2 = np.array([1, 4, 3, 2]) distance = levenshtein(s1, s2) 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 41 42 43 | import numba import numpy as np @numba.jit(nopython=True) def levenshtein(s1, s2): # Get lengths of both sequences len1 = len(s1) len2 = len(s2) # Create matrix of size (len1+1) x (len2+1) matrix = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize first row and column for i in range(len1 + 1): matrix[i, 0] = i for j in range(len2 + 1): matrix[0, j] = j # Fill in the rest of the matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): if s1[i-1] == s2[j-1]: cost = 0 else: cost = 1 # Regular Levenshtein operations (insertion, deletion, substitution) matrix[i, j] = min( matrix[i-1, j] + 1, # deletion matrix[i, j-1] + 1, # insertion matrix[i-1, j-1] + cost # substitution ) # Handle transposition (swap) if possible if (i > 1 and j > 1 and s1[i-1] == s2[j-2] and s1[i-2] == s2[j-1]): matrix[i, j] = min( matrix[i, j], matrix[i-2, j-2] + 2 # swap cost is 2 ) return matrix[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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import numba import numpy as np @numba.jit(nopython=True) def levenshtein(s1, s2): # Get lengths of both sequences len1 = len(s1) len2 = len(s2) # Create matrix of size (len1+1) x (len2+1) matrix = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize first row and column for i in range(len1 + 1): matrix[i, 0] = i for j in range(len2 + 1): matrix[0, j] = j # Fill in the rest of the matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): if s1[i-1] == s2[j-1]: cost = 0 else: cost = 1 # Regular Levenshtein operations (insertion, deletion, substitution) matrix[i, j] = min( matrix[i-1, j] + 1, # deletion matrix[i, j-1] + 1, # insertion matrix[i-1, j-1] + cost # substitution ) # Handle transposition (swap) if possible if (i > 1 and j > 1 and s1[i-1] == s2[j-2] and s1[i-2] == s2[j-1]): matrix[i, j] = min( matrix[i, j], matrix[i-2, j-2] + 2 # swap cost is 2 ) return matrix[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 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
: False
Testing if the previous output contains the string nopython
: True
Testing if the previous output contains the string import numba
: True
Testing if the previous output contains the string from numba
: False
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.
Here's a Numba implementation of the Levenshtein distance algorithm with character swaps costing 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 numba import numpy as np @numba.jit(nopython=True) def levenshtein(s1, s2): # Get lengths of both sequences len1, len2 = len(s1), len(s2) # Create matrix of size (len1+1) x (len2+1) matrix = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize first row and column for i in range(len1 + 1): matrix[i, 0] = i for j in range(len2 + 1): matrix[0, j] = j # Fill in the rest of the matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): if s1[i-1] == s2[j-1]: # If characters match, no additional cost matrix[i, j] = matrix[i-1, j-1] else: # Calculate costs for different operations deletion = matrix[i-1, j] + 1 insertion = matrix[i, j-1] + 1 substitution = matrix[i-1, j-1] + 1 # Handle transposition (swap) with cost 2 if (i > 1 and j > 1 and s1[i-1] == s2[j-2] and s1[i-2] == s2[j-1]): swap = matrix[i-2, j-2] + 2 matrix[i, j] = min(deletion, insertion, substitution, swap) else: matrix[i, j] = min(deletion, insertion, substitution) # Return the bottom-right cell of the matrix return matrix[len1, len2] |
1 2 3 4 5 6 | # Example usage s1 = np.array([1, 2, 3, 4]) s2 = np.array([1, 2, 4, 3]) distance = levenshtein(s1, s2) 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 numba import numpy as np @numba.jit(nopython=True) def levenshtein(s1, s2): # Get lengths of both sequences len1, len2 = len(s1), len(s2) # Create matrix of size (len1+1) x (len2+1) matrix = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize first row and column for i in range(len1 + 1): matrix[i, 0] = i for j in range(len2 + 1): matrix[0, j] = j # Fill in the rest of the matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): if s1[i-1] == s2[j-1]: # If characters match, no additional cost matrix[i, j] = matrix[i-1, j-1] else: # Calculate costs for different operations deletion = matrix[i-1, j] + 1 insertion = matrix[i, j-1] + 1 substitution = matrix[i-1, j-1] + 1 # Handle transposition (swap) with cost 2 if (i > 1 and j > 1 and s1[i-1] == s2[j-2] and s1[i-2] == s2[j-1]): swap = matrix[i-2, j-2] + 2 matrix[i, j] = min(deletion, insertion, substitution, swap) else: matrix[i, j] = min(deletion, insertion, substitution) # Return the bottom-right cell of the matrix return matrix[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 38 39 40 41 42 43 44 45 46 47 48 49 | import numba import numpy as np @numba.jit(nopython=True) def levenshtein(s1, s2): # Get lengths of both sequences len1, len2 = len(s1), len(s2) # Create matrix of size (len1+1) x (len2+1) matrix = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize first row and column for i in range(len1 + 1): matrix[i, 0] = i for j in range(len2 + 1): matrix[0, j] = j # Fill in the rest of the matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): if s1[i-1] == s2[j-1]: # If characters match, no additional cost matrix[i, j] = matrix[i-1, j-1] else: # Calculate costs for different operations deletion = matrix[i-1, j] + 1 insertion = matrix[i, j-1] + 1 substitution = matrix[i-1, j-1] + 1 # Handle transposition (swap) with cost 2 if (i > 1 and j > 1 and s1[i-1] == s2[j-2] and s1[i-2] == s2[j-1]): swap = matrix[i-2, j-2] + 2 matrix[i, j] = min(deletion, insertion, substitution, swap) else: matrix[i, j] = min(deletion, insertion, substitution) # Return the bottom-right cell of the matrix return matrix[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 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
: False
Testing if the previous output contains the string nopython
: True
Testing if the previous output contains the string import numba
: True
Testing if the previous output contains the string from numba
: False
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.
Here's a Numba implementation of the Levenshtein distance algorithm that takes two numpy integer arrays and uses a cost of 2 for character swaps:
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 53 54 | import numpy as np from numba import jit @jit(nopython=True) def levenshtein(s1, s2): """ Calculate the Levenshtein distance between two integer arrays. Character swaps cost 2, insertions and deletions cost 1. Parameters: s1, s2 : numpy arrays of integers Returns: int : The Levenshtein distance """ # Get lengths of both arrays len1, len2 = len(s1), len(s2) # Create matrix of size (len1+1) x (len2+1) dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize first row and column for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j # Fill the matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): # If characters are same, no operation needed if s1[i-1] == s2[j-1]: dp[i, j] = dp[i-1, j-1] else: # Calculate costs for different operations deletion = dp[i-1, j] + 1 insertion = dp[i, j-1] + 1 substitution = dp[i-1, j-1] + 2 # Swap cost is 2 # Take minimum of all operations dp[i, j] = min(deletion, insertion, substitution) # Return the bottom-right cell of the matrix return dp[len1, len2] # Example usage: if __name__ == "__main__": # Example arrays arr1 = np.array([1, 2, 3, 4], dtype=np.int32) arr2 = np.array([1, 2, 4, 3], dtype=np.int32) # Calculate distance distance = levenshtein(arr1, arr2) print(f"Levenshtein distance: {distance}") |
1 2 3 4 5 | # Example usage arr1 = np.array([1, 2, 3, 4], dtype=np.int32) arr2 = np.array([1, 2, 4, 3], dtype=np.int32) distance = levenshtein(arr1, arr2) print(distance) # Will output 2 because one swap is needed (cost of 2) |
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 44 | import numpy as np from numba import jit @jit(nopython=True) def levenshtein(s1, s2): """ Calculate the Levenshtein distance between two integer arrays. Character swaps cost 2, insertions and deletions cost 1. Parameters: s1, s2 : numpy arrays of integers Returns: int : The Levenshtein distance """ # Get lengths of both arrays len1, len2 = len(s1), len(s2) # Create matrix of size (len1+1) x (len2+1) dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize first row and column for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j # Fill the matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): # If characters are same, no operation needed if s1[i-1] == s2[j-1]: dp[i, j] = dp[i-1, j-1] else: # Calculate costs for different operations deletion = dp[i-1, j] + 1 insertion = dp[i, j-1] + 1 substitution = dp[i-1, j-1] + 2 # Swap cost is 2 # Take minimum of all operations dp[i, j] = min(deletion, insertion, substitution) # Return the bottom-right cell of the matrix 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import numpy as np from numba import jit @jit(nopython=True) def levenshtein(s1, s2): """ Calculate the Levenshtein distance between two integer arrays. Character swaps cost 2, insertions and deletions cost 1. Parameters: s1, s2 : numpy arrays of integers Returns: int : The Levenshtein distance """ # Get lengths of both arrays len1, len2 = len(s1), len(s2) # Create matrix of size (len1+1) x (len2+1) dp = np.zeros((len1 + 1, len2 + 1), dtype=np.int32) # Initialize first row and column for i in range(len1 + 1): dp[i, 0] = i for j in range(len2 + 1): dp[0, j] = j # Fill the matrix for i in range(1, len1 + 1): for j in range(1, len2 + 1): # If characters are same, no operation needed if s1[i-1] == s2[j-1]: dp[i, j] = dp[i-1, j-1] else: # Calculate costs for different operations deletion = dp[i-1, j] + 1 insertion = dp[i, j-1] + 1 substitution = dp[i-1, j-1] + 2 # Swap cost is 2 # Take minimum of all operations dp[i, j] = min(deletion, insertion, substitution) # Return the bottom-right cell of the matrix 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') |
All tests passed
Testing if the previous output contains the string All tests passed
: True
Testing if the previous output contains the string njit
: False
Testing if the previous output contains the string nopython
: True
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.
Here's a Numba implementation of the Levenshtein distance algorithm with character swaps costing 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 | import numba import numpy as np @numba.jit(nopython=True) def levenshtein(s1, s2): # Create matrix of size (len(s1) + 1) x (len(s2) + 1) m, n = len(s1), len(s2) dp = np.zeros((m + 1, n + 1), dtype=np.int32) # Initialize first row and column for i in range(m + 1): dp[i, 0] = i for j in range(n + 1): dp[0, j] = j # Fill the matrix for i in range(1, m + 1): for j in range(1, n + 1): if s1[i-1] == s2[j-1]: dp[i, j] = dp[i-1, j-1] else: # Regular operations (insertion, deletion, substitution) dp[i, j] = min( dp[i-1, j] + 1, # deletion dp[i, j-1] + 1, # insertion dp[i-1, j-1] + 1 # substitution ) # Check for transposition (swap) if (i > 1 and j > 1 and s1[i-1] == s2[j-2] and s1[i-2] == s2[j-1]): dp[i, j] = min(dp[i, j], dp[i-2, j-2] + 2) return dp[m, n] |
1 2 3 4 5 | # Example usage s1 = np.array([1, 2, 3, 4, 5]) s2 = np.array([1, 2, 4, 3, 5]) distance = levenshtein(s1, s2) 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 | import numba import numpy as np @numba.jit(nopython=True) def levenshtein(s1, s2): # Create matrix of size (len(s1) + 1) x (len(s2) + 1) m, n = len(s1), len(s2) dp = np.zeros((m + 1, n + 1), dtype=np.int32) # Initialize first row and column for i in range(m + 1): dp[i, 0] = i for j in range(n + 1): dp[0, j] = j # Fill the matrix for i in range(1, m + 1): for j in range(1, n + 1): if s1[i-1] == s2[j-1]: dp[i, j] = dp[i-1, j-1] else: # Regular operations (insertion, deletion, substitution) dp[i, j] = min( dp[i-1, j] + 1, # deletion dp[i, j-1] + 1, # insertion dp[i-1, j-1] + 1 # substitution ) # Check for transposition (swap) if (i > 1 and j > 1 and s1[i-1] == s2[j-2] and s1[i-2] == s2[j-1]): dp[i, j] = min(dp[i, j], dp[i-2, j-2] + 2) return dp[m, n] |
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 | import numba import numpy as np @numba.jit(nopython=True) def levenshtein(s1, s2): # Create matrix of size (len(s1) + 1) x (len(s2) + 1) m, n = len(s1), len(s2) dp = np.zeros((m + 1, n + 1), dtype=np.int32) # Initialize first row and column for i in range(m + 1): dp[i, 0] = i for j in range(n + 1): dp[0, j] = j # Fill the matrix for i in range(1, m + 1): for j in range(1, n + 1): if s1[i-1] == s2[j-1]: dp[i, j] = dp[i-1, j-1] else: # Regular operations (insertion, deletion, substitution) dp[i, j] = min( dp[i-1, j] + 1, # deletion dp[i, j-1] + 1, # insertion dp[i-1, j-1] + 1 # substitution ) # Check for transposition (swap) if (i > 1 and j > 1 and s1[i-1] == s2[j-2] and s1[i-2] == s2[j-1]): dp[i, j] = min(dp[i, j], dp[i-2, j-2] + 2) return dp[m, n] 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 42, 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
: False
Testing if the previous output contains the string nopython
: True
Testing if the previous output contains the string import numba
: True
Testing if the previous output contains the string from numba
: False