master > master: code py - requirements kompakteres Display
This commit is contained in:
parent
61841a5368
commit
a536d16c1d
@ -52,6 +52,7 @@ def enter():
|
|||||||
# Y = 'apple',
|
# Y = 'apple',
|
||||||
# X = 'happily',
|
# X = 'happily',
|
||||||
verbose = True,
|
verbose = True,
|
||||||
|
just_moves = False,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -45,14 +45,16 @@ def hirschberg_algorithm_once(
|
|||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
|
just_moves: bool = False,
|
||||||
) -> Tuple[str, str]:
|
) -> Tuple[str, str]:
|
||||||
Costs, Moves = compute_cost_matrix(X = '-' + X, Y = '-' + Y);
|
Costs, Moves = compute_cost_matrix(X = '-' + X, Y = '-' + Y);
|
||||||
path = reconstruct_optimal_path(Moves=Moves);
|
path = reconstruct_optimal_path(Moves=Moves);
|
||||||
word_x, word_y = reconstruct_words(X = '-' + X, Y = '-' + Y, moves=[Moves[coord] for coord in path], path=path);
|
word_x, word_y = reconstruct_words(X = '-' + X, Y = '-' + Y, moves=[Moves[coord] for coord in path], path=path);
|
||||||
if verbose:
|
if verbose:
|
||||||
repr = display_cost_matrix(Costs=Costs, path=path, X = '-' + X, Y = '-' + Y);
|
repr = display_cost_matrix(Costs=Costs, path=path, X = '-' + X, Y = '-' + Y, just_moves=just_moves);
|
||||||
print(f'\n{repr}');
|
print(f'\n{repr}');
|
||||||
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
||||||
|
print('');
|
||||||
print(word_y);
|
print(word_y);
|
||||||
print(len(word_x) * '-');
|
print(len(word_x) * '-');
|
||||||
print(word_x);
|
print(word_x);
|
||||||
@ -63,14 +65,16 @@ def hirschberg_algorithm(
|
|||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
|
just_moves: bool = False,
|
||||||
) -> Tuple[str, str]:
|
) -> Tuple[str, str]:
|
||||||
alignments_x, alignments_y = hirschberg_algorithm_step(X=X, Y=Y, depth=1, verbose=verbose);
|
alignments_x, alignments_y = hirschberg_algorithm_step(X=X, Y=Y, depth=1, verbose=verbose, just_moves=just_moves);
|
||||||
word_x = ''.join(alignments_x);
|
word_x = ''.join(alignments_x);
|
||||||
word_y = ''.join(alignments_y);
|
word_y = ''.join(alignments_y);
|
||||||
if verbose:
|
if verbose:
|
||||||
display_x = '|'.join(alignments_x);
|
display_x = f'[{"][".join(alignments_x)}]';
|
||||||
display_y = '|'.join(alignments_y);
|
display_y = f'[{"][".join(alignments_y)}]';
|
||||||
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
||||||
|
print('');
|
||||||
print(display_y);
|
print(display_y);
|
||||||
print(len(display_x) * '-');
|
print(len(display_x) * '-');
|
||||||
print(display_x);
|
print(display_x);
|
||||||
@ -82,6 +86,7 @@ def hirschberg_algorithm_step(
|
|||||||
Y: str,
|
Y: str,
|
||||||
depth: int = 0,
|
depth: int = 0,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
|
just_moves: bool = False,
|
||||||
) -> Tuple[List[str], List[str]]:
|
) -> Tuple[List[str], List[str]]:
|
||||||
n = len(Y);
|
n = len(Y);
|
||||||
if n == 1:
|
if n == 1:
|
||||||
@ -118,6 +123,7 @@ def hirschberg_algorithm_step(
|
|||||||
X2 = '-' + X2,
|
X2 = '-' + X2,
|
||||||
Y1 = '-' + Y1,
|
Y1 = '-' + Y1,
|
||||||
Y2 = '-' + Y2,
|
Y2 = '-' + Y2,
|
||||||
|
just_moves = just_moves,
|
||||||
);
|
);
|
||||||
print(f'\n\x1b[1mRekursionstiefe: {depth}\x1b[0m\n\n{repr}')
|
print(f'\n\x1b[1mRekursionstiefe: {depth}\x1b[0m\n\n{repr}')
|
||||||
|
|
||||||
@ -125,8 +131,8 @@ def hirschberg_algorithm_step(
|
|||||||
coord1, coord2 = get_optimal_transition(Costs1=Costs1, Costs2=Costs2);
|
coord1, coord2 = get_optimal_transition(Costs1=Costs1, Costs2=Costs2);
|
||||||
p = coord1[0];
|
p = coord1[0];
|
||||||
# Divide and Conquer ausführen:
|
# Divide and Conquer ausführen:
|
||||||
alignments_x_1, alignments_y_1 = hirschberg_algorithm_step(X=X[:p], Y=Y[:n], depth=depth+1, verbose=verbose);
|
alignments_x_1, alignments_y_1 = hirschberg_algorithm_step(X=X[:p], Y=Y[:n], depth=depth+1, verbose=verbose, just_moves=just_moves);
|
||||||
alignments_x_2, alignments_y_2 = hirschberg_algorithm_step(X=X[p:], Y=Y[n:], depth=depth+1, verbose=verbose);
|
alignments_x_2, alignments_y_2 = hirschberg_algorithm_step(X=X[p:], Y=Y[n:], depth=depth+1, verbose=verbose, just_moves=just_moves);
|
||||||
|
|
||||||
# Resultate zusammensetzen:
|
# Resultate zusammensetzen:
|
||||||
alignments_x = alignments_x_1 + alignments_x_2;
|
alignments_x = alignments_x_1 + alignments_x_2;
|
||||||
@ -375,10 +381,10 @@ def represent_cost_matrix(
|
|||||||
|
|
||||||
table_costs = table.copy();
|
table_costs = table.copy();
|
||||||
table_moves = table.copy();
|
table_moves = table.copy();
|
||||||
table_costs[3:(3+m), 3:(3+n)] = Costs;
|
table_costs[3:(3+m), 3:(3+n)] = Costs.copy();
|
||||||
table_moves[3:(3+m), 3:(3+n)] = '·';
|
table_moves[3:(3+m), 3:(3+n)] = '·';
|
||||||
for (i, j) in path:
|
for (i, j) in path:
|
||||||
# table_costs[3 + i, 3 + j] = f'\x1b[92;1m{table_costs[3 + i, 3 + j]}\x1b[0m';
|
table_costs[3 + i, 3 + j] = f'{{{table_costs[3 + i, 3 + j]}}}';
|
||||||
table_moves[3 + i, 3 + j] = '*';
|
table_moves[3 + i, 3 + j] = '*';
|
||||||
|
|
||||||
return table_costs, table_moves;
|
return table_costs, table_moves;
|
||||||
@ -388,6 +394,7 @@ def display_cost_matrix(
|
|||||||
path: List[Tuple[int, int]],
|
path: List[Tuple[int, int]],
|
||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
|
just_moves: bool = False,
|
||||||
) -> str:
|
) -> str:
|
||||||
'''
|
'''
|
||||||
Zeigt Kostenmatrix + optimalen Pfad.
|
Zeigt Kostenmatrix + optimalen Pfad.
|
||||||
@ -402,12 +409,13 @@ def display_cost_matrix(
|
|||||||
'''
|
'''
|
||||||
table_costs, table_moves = represent_cost_matrix(Costs=Costs, path=path, X=X, Y=Y);
|
table_costs, table_moves = represent_cost_matrix(Costs=Costs, path=path, X=X, Y=Y);
|
||||||
# benutze pandas-Dataframe, um schöner darzustellen:
|
# benutze pandas-Dataframe, um schöner darzustellen:
|
||||||
h = table_costs.shape[0];
|
if just_moves:
|
||||||
costs_repr = pd.DataFrame(table_costs).to_string(index=False, header=False);
|
table = table_moves;
|
||||||
moves_repr = pd.DataFrame(table_moves).to_string(index=False, header=False);
|
else:
|
||||||
table = np.concatenate([table_costs, np.full(shape=(h, 1), dtype=object, fill_value=' '), table_moves], axis=1);
|
table = table_costs;
|
||||||
|
|
||||||
repr = pd.DataFrame(table).to_string(index=False, header=False);
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
||||||
|
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
||||||
return repr;
|
return repr;
|
||||||
|
|
||||||
def display_cost_matrix_halves(
|
def display_cost_matrix_halves(
|
||||||
@ -419,6 +427,7 @@ def display_cost_matrix_halves(
|
|||||||
X2: str,
|
X2: str,
|
||||||
Y1: str,
|
Y1: str,
|
||||||
Y2: str,
|
Y2: str,
|
||||||
|
just_moves: bool = False,
|
||||||
) -> str:
|
) -> str:
|
||||||
'''
|
'''
|
||||||
Zeigt Kostenmatrix + optimalen Pfad für Schritt im D & C Hirschberg-Algorithmus
|
Zeigt Kostenmatrix + optimalen Pfad für Schritt im D & C Hirschberg-Algorithmus
|
||||||
@ -435,14 +444,13 @@ def display_cost_matrix_halves(
|
|||||||
table_costs2, table_moves2 = represent_cost_matrix(Costs=Costs2, path=path2, X=X2, Y=Y2, pad=True);
|
table_costs2, table_moves2 = represent_cost_matrix(Costs=Costs2, path=path2, X=X2, Y=Y2, pad=True);
|
||||||
|
|
||||||
# merge Taellen:
|
# merge Taellen:
|
||||||
h = table_costs1.shape[0];
|
|
||||||
table_costs = np.concatenate([table_costs1, table_costs2[::-1, ::-1]], axis=1);
|
table_costs = np.concatenate([table_costs1, table_costs2[::-1, ::-1]], axis=1);
|
||||||
table_moves = np.concatenate([table_moves1, table_moves2[::-1, ::-1]], axis=1);
|
table_moves = np.concatenate([table_moves1, table_moves2[::-1, ::-1]], axis=1);
|
||||||
table = np.concatenate([table_costs, np.full(shape=(h, 1), dtype=object, fill_value=' '), table_moves], axis=1);
|
if just_moves:
|
||||||
|
table = table_moves;
|
||||||
|
else:
|
||||||
|
table = table_costs;
|
||||||
|
|
||||||
# benutze pandas-Dataframe, um schöner darzustellen:
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
||||||
# costs_repr = pd.DataFrame(table_costs).to_string(index=False, header=False);
|
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
||||||
# moves_repr = pd.DataFrame(table_moves).to_string(index=False, header=False);
|
|
||||||
# return costs_repr, moves_repr;
|
|
||||||
repr = pd.DataFrame(table).to_string(index=False, header=False);
|
|
||||||
return repr;
|
return repr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user