Skip to content

Commit d182419

Browse files
author
Allen Goodman
committed
added newline; repositioned parentheses to make algorithm slightly more idiomatic
1 parent 675b085 commit d182419

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

chapters/strings/matching-strings.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,34 @@ You want to match two or more strings.
1212
Calculate the edit distance, or number of operations required to transform one string into the other.
1313

1414
{% highlight coffeescript %}
15+
1516
Levenshtein =
1617
(str1, str2) ->
17-
18+
1819
l1 = str1.length
1920
l2 = str2.length
20-
21+
2122
Math.max l1, l2 if Math.min l1, l2 == 0
22-
23+
2324
i = 0; j = 0; distance = []
2425

2526
for i in [0...l1 + 1]
26-
distance[i] = []
27-
distance[i][0] = i
27+
distance[i] = []
28+
distance[i][0] = i
2829

2930
distance[0][j] = j for j in [0...l2 + 1]
30-
31+
3132
for i in [1...l1 + 1]
32-
for j in [1...l2 + 1]
33-
distance[i][j] = Math.min distance[i - 1][j] + 1,
34-
distance[i][j - 1] + 1,
35-
distance[i - 1][j - 1] + if str1.charAt(i - 1) == str2.charAt(j - 1) then 0 else 1
33+
for j in [1...l2 + 1]
34+
distance[i][j] = Math.min distance[i - 1][j] + 1,
35+
distance[i][j - 1] + 1,
36+
distance[i - 1][j - 1] +
37+
if (str1.charAt i - 1) == (str2.charAt j - 1) then 0 else 1
3638

3739
distance[l1][l2]
40+
3841
{% endhighlight %}
3942

4043
## Discussion
4144

42-
You can use either Hirschberg or Wagner–Fischer's algorithm to calculate a Levenshtein distance. This example uses Wagner–Fischer's algorithm.
45+
You can use either Hirschberg or Wagner–Fischer's algorithm to calculate a Levenshtein distance. This example uses Wagner–Fischer's algorithm.

0 commit comments

Comments
 (0)