Challenge - 5 Problems
Levenshtein Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Levenshtein Distance Calculation
What is the output of the following Python code that calculates the Levenshtein distance between two strings?
DSA Python
def levenshtein(s1, s2): if len(s1) < len(s2): return levenshtein(s2, s1) if len(s2) == 0: return len(s1) previous_row = range(len(s2) + 1) for i, c1 in enumerate(s1): current_row = [i + 1] for j, c2 in enumerate(s2): insertions = previous_row[j + 1] + 1 deletions = current_row[j] + 1 substitutions = previous_row[j] + (c1 != c2) current_row.append(min(insertions, deletions, substitutions)) previous_row = current_row return previous_row[-1] print(levenshtein("kitten", "sitting"))
Attempts:
2 left
💡 Hint
Think about the minimum number of edits (insertions, deletions, substitutions) to convert 'kitten' to 'sitting'.
✗ Incorrect
The Levenshtein distance between 'kitten' and 'sitting' is 3: substitute 'k' with 's', substitute 'e' with 'i', and insert 'g' at the end.
🧠 Conceptual
intermediateUnderstanding Levenshtein Distance Operations
Which of the following operations are considered when calculating the Levenshtein distance between two strings?
Attempts:
2 left
💡 Hint
Levenshtein distance counts the minimal number of single-character edits.
✗ Incorrect
Levenshtein distance counts insertions, deletions, and substitutions only. Transposition is not counted in Levenshtein but in other distances like Damerau-Levenshtein.
🔧 Debug
advancedIdentify the Error in Levenshtein Distance Code
What error will the following code raise when executed?
DSA Python
def levenshtein_bug(s1, s2): previous_row = [0] * (len(s2) + 1) for i, c1 in enumerate(s1): current_row = [i + 1] for j, c2 in enumerate(s2): insertions = previous_row[j + 1] + 1 deletions = current_row[j] + 1 substitutions = previous_row[j] + (c1 != c2) current_row.append(min(insertions, deletions, substitutions)) previous_row = current_row return previous_row[-1] print(levenshtein_bug("abc", ""))
Attempts:
2 left
💡 Hint
Check how previous_row is initialized and accessed when s2 is empty.
✗ Incorrect
When s2 is empty, previous_row has length 1. Accessing previous_row[j + 1] with j=0 causes IndexError.
🚀 Application
advancedMinimum Edit Distance for Spell Correction
Given the words 'flaw' and 'lawn', what is the minimum edit distance calculated by the Levenshtein algorithm?
Attempts:
2 left
💡 Hint
Consider the minimal edits to convert 'flaw' into 'lawn'.
✗ Incorrect
The minimal edits are: delete 'f' and substitute 'w' with 'n', total 2 edits.
❓ Predict Output
expertOutput of Levenshtein Distance with Unicode Characters
What is the output of this code calculating Levenshtein distance between two Unicode strings?
DSA Python
def levenshtein_unicode(s1, s2): if len(s1) < len(s2): return levenshtein_unicode(s2, s1) if len(s2) == 0: return len(s1) previous_row = range(len(s2) + 1) for i, c1 in enumerate(s1): current_row = [i + 1] for j, c2 in enumerate(s2): insertions = previous_row[j + 1] + 1 deletions = current_row[j] + 1 substitutions = previous_row[j] + (c1 != c2) current_row.append(min(insertions, deletions, substitutions)) previous_row = current_row return previous_row[-1] print(levenshtein_unicode("café", "café"))
Attempts:
2 left
💡 Hint
Note that the second string uses 'e' plus combining accent, which is two characters.
✗ Incorrect
The strings differ by one character because 'é' in first string is a single character, while in second string it's 'e' plus combining accent, so distance is 1.
