diff --git a/woche 42/08_10.py b/woche 42/08_10.py deleted file mode 100644 index 3a3b670..0000000 --- a/woche 42/08_10.py +++ /dev/null @@ -1,59 +0,0 @@ -def matmul(a: list[list[float | int]], b: list[list[float | int]]) -> list[list[float | int]]: - if not a or not a[0] or not b or not b[0]: - raise ValueError("Matrizen dürfen nicht leer sein.") - - rows_a = len(a) - cols_a = len(a[0]) - rows_b = len(b) - cols_b = len(b[0]) - - # Auf unregelmäßige (jagged) Matrizen prüfen - if any(len(row) != cols_a for row in a): - raise ValueError("Matrix a enthält Zeilen unterschiedlicher Länge.") - if any(len(row) != cols_b for row in b): - raise ValueError("Matrix b enthält Zeilen unterschiedlicher Länge.") - - # Dimensionenprüfung für die Multiplikation - if cols_a != rows_b: - raise ValueError( - f"Inkompatible Dimensionen: Spalten von a ({cols_a}) " - f"müssen gleich Zeilen von b ({rows_b}) sein." - ) - - # Ergebnis-Matrix mit Nullen initialisieren (rows_a x cols_b) - result = [[0.0 for _ in range(cols_b)] for _ in range(rows_a)] - - for i in range(rows_a): - for j in range(cols_b): - for k in range(cols_a): - result[i][j] += a[i][k] * b[k][j] - - return result - - -if __name__ == "__main__": - # Beispiel 1: Erfolgreiche Multiplikation (2x3 mal 3x2) - mat1 = [ - [1, 2, 3], - [4, 5, 6] - ] - mat2 = [ - [7, 8], - [9, 1], - [2, 3] - ] - print("Ergebnis Beispiel 1:") - print(matmul(mat1, mat2)) - # Erwartetes Ergebnis: [[31, 19], [85, 55]] - - # Beispiel 2: Fehlerfall (Dimensionen passen nicht) - try: - mat3 = [[1, 2]] - matmul(mat1, mat3) - except ValueError as Error: - print(f"Erfolgreich abgefangener Fehler (Beispiel 2): {Error}") - - -"""Docstring und Type Hints sind vorhanden und stimmen mit dem Code überein. -• Namen sind aussagekräftig. -• Fehlerfälle werden behandelt, z. B. nicht passende Dimensionen""" \ No newline at end of file