From 1f7ff8109e38944d9be22e99e71bd23239c5b049 Mon Sep 17 00:00:00 2001 From: Thomas <2121321@hs-mannheim.de> Date: Wed, 9 Nov 2022 12:31:46 +0100 Subject: [PATCH] Aufgabe 5.1 implemented --- Aufgabe 5.py | 2 -- s1_a51.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) delete mode 100644 Aufgabe 5.py create mode 100644 s1_a51.py diff --git a/Aufgabe 5.py b/Aufgabe 5.py deleted file mode 100644 index 78ce1f4..0000000 --- a/Aufgabe 5.py +++ /dev/null @@ -1,2 +0,0 @@ -if __name__ == '__main__': - print("test") \ No newline at end of file diff --git a/s1_a51.py b/s1_a51.py new file mode 100644 index 0000000..639d6a8 --- /dev/null +++ b/s1_a51.py @@ -0,0 +1,52 @@ +class MyMatrix: + # Constructor - parameterized + row = 0 + column = 0 + matrix = {} + + def __init__(self, list): + self.row = len(list) + self.column = len(list[0]) + test = len(list[0]) + for i in range(len(list)): + if len(list[i]) != test: + raise Exception('Not a matrix') + dictionary = {} + for i in range(self.column): + for j in range(self.row): + if (list[i][j] == 0): + continue + dictionary[i*self.column + j] = list[i][j] + + self.matrix = dictionary + + def __str__(self): + for i in range((self.row) *(self.column)): + if i % self.column == 0: + print("| ",end="") + if i in self.matrix: + print(f"{self.matrix[i]} ",end="") + else: + print("0 ",end="") + if i % self.column == self.column -1: + print("|\n",end="") + + def __getitem__(self, packing): + column2,row1 = packing + if column2 > self.column or row1 > self.row: + raise Exception('Outside of Bounds') + if column2*self.column + row1 in self.matrix: + return self.matrix[column2*self.column + row1] + else: + return 0 + + + + +m = [[3,0,-2,11],[0,0,9,0],[0,7,0,0],[0,0,0,-3]] +object = MyMatrix(m) +object.__str__() +#Wir haben keine Lösung mitm[0][0]= 3, da __getitem__ nur ein Argument nimmt +x = object[0,0] + +print(x)