mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
add types for local variables
This commit is contained in:
parent
a2c790e490
commit
971c15673b
@ -14,13 +14,13 @@ The only allowed operations are
|
|||||||
def compute_transform_tables(
|
def compute_transform_tables(
|
||||||
X: str, Y: str, cC: int, cR: int, cD: int, cI: int
|
X: str, Y: str, cC: int, cR: int, cD: int, cI: int
|
||||||
) -> Tuple[List[int], List[str]]:
|
) -> Tuple[List[int], List[str]]:
|
||||||
X = list(X)
|
X: List[str] = list(X)
|
||||||
Y = list(Y)
|
Y: List[str] = list(Y)
|
||||||
m = len(X)
|
m: int = len(X)
|
||||||
n = len(Y)
|
n: int = len(Y)
|
||||||
|
|
||||||
costs = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
costs: List[List[int]] = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
||||||
ops = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
ops: List[List[int]] = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
||||||
|
|
||||||
for i in range(1, m + 1):
|
for i in range(1, m + 1):
|
||||||
costs[i][0] = i * cD
|
costs[i][0] = i * cD
|
||||||
@ -52,19 +52,19 @@ def compute_transform_tables(
|
|||||||
|
|
||||||
def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]:
|
def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]:
|
||||||
if i == 0 and j == 0:
|
if i == 0 and j == 0:
|
||||||
seq = []
|
seq: List = []
|
||||||
return seq
|
return seq
|
||||||
else:
|
else:
|
||||||
if ops[i][j][0] == "C" or ops[i][j][0] == "R":
|
if ops[i][j][0] == "C" or ops[i][j][0] == "R":
|
||||||
seq = assemble_transformation(ops, i - 1, j - 1)
|
seq: List = assemble_transformation(ops, i - 1, j - 1)
|
||||||
seq.append(ops[i][j])
|
seq.append(ops[i][j])
|
||||||
return seq
|
return seq
|
||||||
elif ops[i][j][0] == "D":
|
elif ops[i][j][0] == "D":
|
||||||
seq = assemble_transformation(ops, i - 1, j)
|
seq: List = assemble_transformation(ops, i - 1, j)
|
||||||
seq.append(ops[i][j])
|
seq.append(ops[i][j])
|
||||||
return seq
|
return seq
|
||||||
else:
|
else:
|
||||||
seq = assemble_transformation(ops, i, j - 1)
|
seq: List = assemble_transformation(ops, i, j - 1)
|
||||||
seq.append(ops[i][j])
|
seq.append(ops[i][j])
|
||||||
return seq
|
return seq
|
||||||
|
|
||||||
@ -72,13 +72,13 @@ def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]:
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
_, operations = compute_transform_tables("Python", "Algorithms", -1, 1, 2, 2)
|
_, operations = compute_transform_tables("Python", "Algorithms", -1, 1, 2, 2)
|
||||||
|
|
||||||
m = len(operations)
|
m: int = len(operations)
|
||||||
n = len(operations[0])
|
n: int = len(operations[0])
|
||||||
sequence = assemble_transformation(operations, m - 1, n - 1)
|
sequence: List = assemble_transformation(operations, m - 1, n - 1)
|
||||||
|
|
||||||
string = list("Python")
|
string: List[str] = list("Python")
|
||||||
i = 0
|
i: int = 0
|
||||||
cost = 0
|
cost: int = 0
|
||||||
|
|
||||||
with open("min_cost.txt", "w") as file:
|
with open("min_cost.txt", "w") as file:
|
||||||
for op in sequence:
|
for op in sequence:
|
||||||
|
Loading…
Reference in New Issue
Block a user