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: class Matrix:
def __init__(self): 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 value == 0:
if self.matrix.get(col) and self.matrix.get(col).get(ro): if self.__matrix.get(column) and self.__matrix.get(column).get(row):
del self.matrix[col][ro] del self.__matrix[column][row]
else: else:
self.matrix[col][ro] = value self.__matrix[column][row] = value
def get(self, col, ro): def get(self, column, row):
if self.matrix.get(col) and self.matrix.get(col).get(row): if self.__matrix.get(column) and self.__matrix.get(column).get(row):
return self.matrix[col][row] return self.__matrix[column][row]
else: else:
return 0 return 0
def print(self):
print(self.__matrix)
if __name__ == "__main__": if __name__ == "__main__":
# Aufgabe 1b # Aufgabe 1b
@ -28,12 +31,12 @@ if __name__ == "__main__":
[0, 0, 9, 0], [0, 0, 9, 0],
[0, 7, 0, 0], [0, 7, 0, 0],
[0, 0, 0, 0]] [0, 0, 0, 0]]
mar = Matrix() sparse_matrix = Matrix()
for row in range(len(input_matrix)): for column in range(len(input_matrix)):
for column in range(len(input_matrix[0])): for row in range(len(input_matrix[0])):
mar.set(row, column, input_matrix[row][column]) sparse_matrix.set(column, row, input_matrix[row][column])
print(mar.matrix) sparse_matrix.print()
# Aufgabe 1c # Aufgabe 1c
# print("\nThe sparce matrix computed without numpy\n" + str(sparse_matrix)) # 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 row in range(len(list_to_sort)):
for next_row in range(row + 1, 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]: 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], list_to_sort[row]
list_to_sort[row] = list_to_sort[next_row]
list_to_sort[next_row] = tmp
print("The sorted list" + str(list_to_sort)) print("The sorted list" + str(list_to_sort))