Skip to content

Commit

Permalink
Travis CI: Identify our ten slowest pytests (TheAlgorithms#2350)
Browse files Browse the repository at this point in the history
* Travis CI: Identify our ten slowest tests

https://howchoo.com/g/mtblodnjzjc/how-to-measure-unit-test-execution-times-in-pytest helps us to find the individual tests that are slowing down our Travis CI checks.

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
cclauss and github-actions authored Aug 25, 2020
1 parent 402ba7f commit e776006
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ before_script:
- pip install -r requirements.txt # fast fail on black, flake8, validate_filenames
script:
- mypy --ignore-missing-imports .
- pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=. .
- pytest --doctest-modules --durations=10 --cov-report=term-missing:skip-covered --cov=. .
after_success:
- scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
40 changes: 21 additions & 19 deletions matrix/matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ def subtract(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
>>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]])
[[-1, -0.5], [-1, -1.5]]
"""
if _check_not_integer(matrix_a)\
and _check_not_integer(matrix_b)\
and _verify_matrix_sizes(matrix_a, matrix_b):
if (
_check_not_integer(matrix_a)
and _check_not_integer(matrix_b)
and _verify_matrix_sizes(matrix_a, matrix_b)
):
return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]


Expand Down Expand Up @@ -60,8 +62,9 @@ def multiply(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
f"Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) "
f"and ({rows[1]},{cols[1]})"
)
return [[sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)]
for i in matrix_a]
return [
[sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)] for i in matrix_a
]


def identity(n: int) -> List[list]:
Expand Down Expand Up @@ -95,8 +98,8 @@ def minor(matrix: List[list], row: int, column: int) -> List[list]:
>>> minor([[1, 2], [3, 4]], 1, 1)
[[1]]
"""
minor = matrix[: row] + matrix[row + 1:]
return [row[:column] + row[column + 1:] for row in minor]
minor = matrix[:row] + matrix[row + 1 :]
return [row[:column] + row[column + 1 :] for row in minor]


def determinant(matrix: List[list]) -> int:
Expand All @@ -109,8 +112,10 @@ def determinant(matrix: List[list]) -> int:
if len(matrix) == 1:
return matrix[0][0]

return sum(x * determinant(minor(matrix, 0, i)) * (-1) ** i
for i, x in enumerate(matrix[0]))
return sum(
x * determinant(minor(matrix, 0, i)) * (-1) ** i
for i, x in enumerate(matrix[0])
)


def inverse(matrix: List[list]) -> List[list]:
Expand All @@ -124,9 +129,10 @@ def inverse(matrix: List[list]) -> List[list]:
if det == 0:
return None

matrix_minor = [[determinant(minor(matrix, i, j))
for j in range(len(matrix))]
for i in range(len(matrix))]
matrix_minor = [
[determinant(minor(matrix, i, j)) for j in range(len(matrix))]
for i in range(len(matrix))
]

cofactors = [
[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])]
Expand Down Expand Up @@ -159,14 +165,10 @@ def _verify_matrix_sizes(matrix_a: List[list], matrix_b: List[list]) -> Tuple[li
def main():
matrix_a = [[12, 10], [3, 9]]
matrix_b = [[3, 4], [7, 4]]
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24],
[31, 32, 33, 34], [41, 42, 43, 44]]
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
print(
f"Add Operation, {add(matrix_a, matrix_b) = } \n")
print(
f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",
)
print(f"Add Operation, {add(matrix_a, matrix_b) = } \n")
print(f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",)
print(f"Identity: {identity(5)}\n")
print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n")
print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n")
Expand Down

0 comments on commit e776006

Please sign in to comment.