Style Fixes

main
Alexander Obwandner 2022-11-08 16:11:58 +01:00
parent 32e95c68f7
commit 5b70aa1ac7
2 changed files with 17 additions and 16 deletions

View File

@ -6,21 +6,24 @@ from scipy import sparse
class Matrix:
def __init__(self):
self.matrix = collections.defaultdict(dict)
self.__matrix = collections.defaultdict(dict)
def set(self, col, ro, value):
def set(self, column, row, value):
if value == 0:
if self.matrix.get(col) and self.matrix.get(col).get(ro):
del self.matrix[col][ro]
if self.__matrix.get(column) and self.__matrix.get(column).get(row):
del self.__matrix[column][row]
else:
self.matrix[col][ro] = value
self.__matrix[column][row] = value
def get(self, col, ro):
if self.matrix.get(col) and self.matrix.get(col).get(row):
return self.matrix[col][row]
def get(self, column, row):
if self.__matrix.get(column) and self.__matrix.get(column).get(row):
return self.__matrix[column][row]
else:
return 0
def print(self):
print(self.__matrix)
if __name__ == "__main__":
# Aufgabe 1b
@ -28,12 +31,12 @@ if __name__ == "__main__":
[0, 0, 9, 0],
[0, 7, 0, 0],
[0, 0, 0, 0]]
mar = Matrix()
for row in range(len(input_matrix)):
for column in range(len(input_matrix[0])):
mar.set(row, column, input_matrix[row][column])
sparse_matrix = Matrix()
for column in range(len(input_matrix)):
for row in range(len(input_matrix[0])):
sparse_matrix.set(column, row, input_matrix[row][column])
print(mar.matrix)
sparse_matrix.print()
# Aufgabe 1c
# print("\nThe sparce matrix computed without numpy\n" + str(sparse_matrix))

View File

@ -24,9 +24,7 @@ if __name__ == "__main__":
for row in range(len(list_to_sort)):
for next_row in range(row + 1, len(list_to_sort)):
if list_to_sort[row][1] > list_to_sort[next_row][1]:
tmp = list_to_sort[row]
list_to_sort[row] = list_to_sort[next_row]
list_to_sort[next_row] = tmp
list_to_sort[row], list_to_sort[next_row] = list_to_sort[next_row], list_to_sort[row]
print("The sorted list" + str(list_to_sort))