document.write('<div class="gytha-paste"><div id="gytha-paste-formatted-3"><div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">dameraulevenshtein</span>(seq1, seq2):\n    <span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;Calculate the Damerau-Levenshtein distance between sequences.</span>\n\n<span style="color: #BA2121; font-style: italic">    This distance is the number of additions, deletions, substitutions,</span>\n<span style="color: #BA2121; font-style: italic">    and transpositions needed to transform the first sequence into the</span>\n<span style="color: #BA2121; font-style: italic">    second. Although generally used with strings, any sequences of</span>\n<span style="color: #BA2121; font-style: italic">    comparable objects will work.</span>\n\n<span style="color: #BA2121; font-style: italic">    Transpositions are exchanges of *consecutive* characters; all other</span>\n<span style="color: #BA2121; font-style: italic">    operations are self-explanatory.</span>\n\n<span style="color: #BA2121; font-style: italic">    This implementation is O(N*M) time and O(M) space, for N and M the</span>\n<span style="color: #BA2121; font-style: italic">    lengths of the two sequences.</span>\n\n<span style="color: #BA2121; font-style: italic">    &gt;&gt;&gt; dameraulevenshtein(&#39;ba&#39;, &#39;abc&#39;)</span>\n<span style="color: #BA2121; font-style: italic">    2</span>\n<span style="color: #BA2121; font-style: italic">    &gt;&gt;&gt; dameraulevenshtein(&#39;fee&#39;, &#39;deed&#39;)</span>\n<span style="color: #BA2121; font-style: italic">    2</span>\n\n<span style="color: #BA2121; font-style: italic">    It works with arbitrary sequences too:</span>\n<span style="color: #BA2121; font-style: italic">    &gt;&gt;&gt; dameraulevenshtein(&#39;abcd&#39;, [&#39;b&#39;, &#39;a&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;])</span>\n<span style="color: #BA2121; font-style: italic">    2</span>\n<span style="color: #BA2121; font-style: italic">    &quot;&quot;&quot;</span>\n    <span style="color: #408080; font-style: italic"># codesnippet:D0DE4716-B6E6-4161-9219-2903BF8F547F</span>\n    <span style="color: #408080; font-style: italic"># Conceptually, this is based on a len(seq1) + 1 * len(seq2) + 1 matrix.</span>\n    <span style="color: #408080; font-style: italic"># However, only the current and two previous rows are needed at once,</span>\n    <span style="color: #408080; font-style: italic"># so we only store those.</span>\n    oneago <span style="color: #666666">=</span> <span style="color: #008000">None</span>\n    thisrow <span style="color: #666666">=</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>, <span style="color: #008000">len</span>(seq2) <span style="color: #666666">+</span> <span style="color: #666666">1</span>) <span style="color: #666666">+</span> [<span style="color: #666666">0</span>]\n    <span style="color: #008000; font-weight: bold">for</span> x <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">xrange</span>(<span style="color: #008000">len</span>(seq1)):\n        <span style="color: #408080; font-style: italic"># Python lists wrap around for negative indices, so put the</span>\n        <span style="color: #408080; font-style: italic"># leftmost column at the *end* of the list. This matches with</span>\n        <span style="color: #408080; font-style: italic"># the zero-indexed strings and saves extra calculation.</span>\n        twoago, oneago, thisrow <span style="color: #666666">=</span> oneago, thisrow, [<span style="color: #666666">0</span>] <span style="color: #666666">*</span> <span style="color: #008000">len</span>(seq2) <span style="color: #666666">+</span> [x <span style="color: #666666">+</span> <span style="color: #666666">1</span>]\n        <span style="color: #008000; font-weight: bold">for</span> y <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">xrange</span>(<span style="color: #008000">len</span>(seq2)):\n            delcost <span style="color: #666666">=</span> oneago[y] <span style="color: #666666">+</span> <span style="color: #666666">1</span>\n            addcost <span style="color: #666666">=</span> thisrow[y <span style="color: #666666">-</span> <span style="color: #666666">1</span>] <span style="color: #666666">+</span> <span style="color: #666666">1</span>\n            subcost <span style="color: #666666">=</span> oneago[y <span style="color: #666666">-</span> <span style="color: #666666">1</span>] <span style="color: #666666">+</span> (seq1[x] <span style="color: #666666">!=</span> seq2[y])\n            thisrow[y] <span style="color: #666666">=</span> <span style="color: #008000">min</span>(delcost, addcost, subcost)\n            <span style="color: #408080; font-style: italic"># This block deals with transpositions</span>\n            <span style="color: #008000; font-weight: bold">if</span> (x <span style="color: #666666">&gt;</span> <span style="color: #666666">0</span> <span style="color: #AA22FF; font-weight: bold">and</span> y <span style="color: #666666">&gt;</span> <span style="color: #666666">0</span> <span style="color: #AA22FF; font-weight: bold">and</span> seq1[x] <span style="color: #666666">==</span> seq2[y <span style="color: #666666">-</span> <span style="color: #666666">1</span>]\n                <span style="color: #AA22FF; font-weight: bold">and</span> seq1[x<span style="color: #666666">-1</span>] <span style="color: #666666">==</span> seq2[y] <span style="color: #AA22FF; font-weight: bold">and</span> seq1[x] <span style="color: #666666">!=</span> seq2[y]):\n                thisrow[y] <span style="color: #666666">=</span> <span style="color: #008000">min</span>(thisrow[y], twoago[y <span style="color: #666666">-</span> <span style="color: #666666">2</span>] <span style="color: #666666">+</span> <span style="color: #666666">1</span>)\n    <span style="color: #008000; font-weight: bold">return</span> thisrow[<span style="color: #008000">len</span>(seq2) <span style="color: #666666">-</span> <span style="color: #666666">1</span>]\n</pre></div>\n</div><p><a href="http://gytha.org/paste/3">paste from gytha.org</a>. <a href="#gytha-paste-3" name="gytha-paste-3" onclick="t=document.getElementById(\'gytha-paste-raw-3\'); t.style.display=\'block\'; t=document.getElementById(\'gytha-paste-formatted-3\'); t.style.display=\'none\';">Raw text</a>.</p><textarea id="gytha-paste-raw-3" style="display: none;" rows="47" cols=81>def dameraulevenshtein(seq1, seq2):\n    """Calculate the Damerau-Levenshtein distance between sequences.\n\n    This distance is the number of additions, deletions, substitutions,\n    and transpositions needed to transform the first sequence into the\n    second. Although generally used with strings, any sequences of\n    comparable objects will work.\n\n    Transpositions are exchanges of *consecutive* characters; all other\n    operations are self-explanatory.\n\n    This implementation is O(N*M) time and O(M) space, for N and M the\n    lengths of the two sequences.\n\n    >>> dameraulevenshtein(\'ba\', \'abc\')\n    2\n    >>> dameraulevenshtein(\'fee\', \'deed\')\n    2\n\n    It works with arbitrary sequences too:\n    >>> dameraulevenshtein(\'abcd\', [\'b\', \'a\', \'c\', \'d\', \'e\'])\n    2\n    """\n    # codesnippet:D0DE4716-B6E6-4161-9219-2903BF8F547F\n    # Conceptually, this is based on a len(seq1) + 1 * len(seq2) + 1 matrix.\n    # However, only the current and two previous rows are needed at once,\n    # so we only store those.\n    oneago = None\n    thisrow = range(1, len(seq2) + 1) + [0]\n    for x in xrange(len(seq1)):\n        # Python lists wrap around for negative indices, so put the\n        # leftmost column at the *end* of the list. This matches with\n        # the zero-indexed strings and saves extra calculation.\n        twoago, oneago, thisrow = oneago, thisrow, [0] * len(seq2) + [x + 1]\n        for y in xrange(len(seq2)):\n            delcost = oneago[y] + 1\n            addcost = thisrow[y - 1] + 1\n            subcost = oneago[y - 1] + (seq1[x] != seq2[y])\n            thisrow[y] = min(delcost, addcost, subcost)\n            # This block deals with transpositions\n            if (x > 0 and y > 0 and seq1[x] == seq2[y - 1]\n                and seq1[x-1] == seq2[y] and seq1[x] != seq2[y]):\n                thisrow[y] = min(thisrow[y], twoago[y - 2] + 1)\n    return thisrow[len(seq2) - 1]\n</textarea></div>');
