-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fix lists by calculating the alignment of the changed values
- Loading branch information
Showing
6 changed files
with
171 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from itertools import groupby | ||
|
||
|
||
def align(seq_a, seq_b) -> str: | ||
|
||
matrix: list = [[(0, "e")] + [(0, "i")] * len(seq_b)] | ||
|
||
for a in seq_a: | ||
last = matrix[-1] | ||
|
||
new_line = [(0, "d")] | ||
for bi, b in enumerate(seq_b, 1): | ||
la, lc, lb = new_line[-1], last[bi - 1], last[bi] | ||
values = [(la[0], "i"), (lb[0], "d")] | ||
if a == b: | ||
values.append((lc[0] + 1, "m")) | ||
|
||
new_line.append(max(values)) | ||
matrix.append(new_line) | ||
|
||
# backtrack | ||
|
||
ai = len(seq_a) | ||
bi = len(seq_b) | ||
d = "" | ||
track = "" | ||
|
||
while d != "e": | ||
_, d = matrix[ai][bi] | ||
if d == "m": | ||
ai -= 1 | ||
bi -= 1 | ||
elif d == "i": | ||
bi -= 1 | ||
elif d == "d": | ||
ai -= 1 | ||
if d != "e": | ||
track += d | ||
|
||
return track[::-1] | ||
|
||
|
||
def add_x(track): | ||
"""Replaces an `id` with the same number of insertions and deletions with | ||
x.""" | ||
groups = [(c, len(list(v))) for c, v in groupby(track)] | ||
i = 0 | ||
result = "" | ||
while i < len(groups): | ||
g = groups[i] | ||
if i == len(groups) - 1: | ||
result += g[0] * g[1] | ||
break | ||
|
||
ng = groups[i + 1] | ||
if g[0] == "d" and ng[0] == "i" and g[1] == ng[1]: | ||
result += "x" * g[1] | ||
i += 1 | ||
else: | ||
result += g[0] * g[1] | ||
|
||
i += 1 | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from inline_snapshot import snapshot | ||
from inline_snapshot._align import add_x | ||
from inline_snapshot._align import align | ||
|
||
|
||
def test_align(): | ||
assert align("iabc", "abcd") == snapshot("dmmmi") | ||
|
||
assert align("abbc", "axyc") == snapshot("mddiim") | ||
assert add_x(align("abbc", "axyc")) == snapshot("mxxm") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters